从一个 VC 的按钮呈现到另一个 VC 使得 SWrevealviewcontroller 发现为零
Present from one VC's button to another VC makes SWrevealviewcontroller found nil
我遇到 SWRevealViewController 获取 nil,
的问题
请看我下面的解释。
解释:
我的 2 个屏幕有 2 个菜单 View1,View2。
View1 是一个 table 视图,我在其中使用了自定义单元格,我在自定义单元格中添加了一个按钮。
因此,当我单击该按钮时,它会跳转到 View2,但滑块导航不起作用。
因为
SWRevealViewController *revealViewController = self.revealViewController;
在 View2
中得到 nil
这是我在 UITableViewCell
class
中编写的代码
UIViewController *view = [[[UIApplication sharedApplication] keyWindow] rootViewController];
View2* mapviewController = [view.storyboard instantiateViewControllerWithIdentifier:@"MapView"];
[view presentViewController:mapviewController animated:NO completion:^{}];
我在 View2
中编写的代码
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController ) {
[self.btnMenu setTarget: self.revealViewController];
[self.btnMenu setAction: @selector( revealToggle: )];
[self.view addGestureRecognizer:self.revealViewController.tapGestureRecognizer];
self.revealViewController.rearViewRevealWidth = self.view.frame.size.width - 100;
}
如果有人能帮我解决这个问题就太好了。
提前致谢。
米希尔·奥扎
您可以在 tableViewDidSelectRow 方法中传递您的坐标,如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MapViewController *objMapVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MapViewController"];
objMapVC.currentCord = CLLocationCoordinate2DMake(123.12345, 23.2345);
[self presentViewController:objMapVC animated:YES completion:^{
}];
}
或者如果您的 table 单元格中有按钮,那么请在您的按钮中分配索引,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.yourButton.tag = row;
return cell;
}
现在在您的按钮操作中执行以下操作:
-(IBAction)btnMapClick:(UIButton *)sender{
MapViewController *objMapVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MapViewController"];
objMapVC.currentCord = Array[sender.tag];
[self presentViewController:objMapVC animated:YES completion:^{
}];
}
还在您的 MapViewController 中定义一个 属性,如下所示:
@interface MapViewController : UIViewController
@property (nonatomic) CLLocationCoordinate2D currentCord;
@end
通过执行以上 2 步,您可以将您选择的坐标传递给下一个 MapVC。
回答晚了,
在帕特里克的帮助下,我解决了这个问题。
在我的 table 视图控制器中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
// Configure the cell...
cell.rvc = self.revealViewController;
cell.title.text = @"Your title";
return cell;
}
在我的自定义单元格中:
@interface CustomTableViewCell : UITableViewCell
@property (nonatomic,strong) SWRevealViewController *rvc;
@property (nonatomic,strong) IBOutlet UILabel *title;
@property (nonatomic,strong) IBOutlet UIButton *button;
@end
@implementation CustomTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
[self.button addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonTaped:(void *)sender {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"MapView"];
[self.rvc pushFrontViewController:vc animated:YES];
}
下面是完整的link可能对某些人有用:
Present from one VC to another
我遇到 SWRevealViewController 获取 nil,
的问题
请看我下面的解释。
解释:
我的 2 个屏幕有 2 个菜单 View1,View2。
View1 是一个 table 视图,我在其中使用了自定义单元格,我在自定义单元格中添加了一个按钮。
因此,当我单击该按钮时,它会跳转到 View2,但滑块导航不起作用。
因为
SWRevealViewController *revealViewController = self.revealViewController;
在 View2
这是我在 UITableViewCell
class
UIViewController *view = [[[UIApplication sharedApplication] keyWindow] rootViewController];
View2* mapviewController = [view.storyboard instantiateViewControllerWithIdentifier:@"MapView"];
[view presentViewController:mapviewController animated:NO completion:^{}];
我在 View2
中编写的代码SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController ) {
[self.btnMenu setTarget: self.revealViewController];
[self.btnMenu setAction: @selector( revealToggle: )];
[self.view addGestureRecognizer:self.revealViewController.tapGestureRecognizer];
self.revealViewController.rearViewRevealWidth = self.view.frame.size.width - 100;
}
如果有人能帮我解决这个问题就太好了。
提前致谢。
米希尔·奥扎
您可以在 tableViewDidSelectRow 方法中传递您的坐标,如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MapViewController *objMapVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MapViewController"];
objMapVC.currentCord = CLLocationCoordinate2DMake(123.12345, 23.2345);
[self presentViewController:objMapVC animated:YES completion:^{
}];
}
或者如果您的 table 单元格中有按钮,那么请在您的按钮中分配索引,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.yourButton.tag = row;
return cell;
}
现在在您的按钮操作中执行以下操作:
-(IBAction)btnMapClick:(UIButton *)sender{
MapViewController *objMapVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MapViewController"];
objMapVC.currentCord = Array[sender.tag];
[self presentViewController:objMapVC animated:YES completion:^{
}];
}
还在您的 MapViewController 中定义一个 属性,如下所示:
@interface MapViewController : UIViewController
@property (nonatomic) CLLocationCoordinate2D currentCord;
@end
通过执行以上 2 步,您可以将您选择的坐标传递给下一个 MapVC。
回答晚了,
在帕特里克的帮助下,我解决了这个问题。
在我的 table 视图控制器中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
// Configure the cell...
cell.rvc = self.revealViewController;
cell.title.text = @"Your title";
return cell;
}
在我的自定义单元格中:
@interface CustomTableViewCell : UITableViewCell
@property (nonatomic,strong) SWRevealViewController *rvc;
@property (nonatomic,strong) IBOutlet UILabel *title;
@property (nonatomic,strong) IBOutlet UIButton *button;
@end
@implementation CustomTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
[self.button addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonTaped:(void *)sender {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"MapView"];
[self.rvc pushFrontViewController:vc animated:YES];
}
下面是完整的link可能对某些人有用:
Present from one VC to another