【问题标题】:Present from one VC's button to another VC makes SWrevealviewcontroller found nil从一个 VC 的按钮呈现给另一个 VC 使 SWrevealviewcontroller 发现为零
【发布时间】:2018-11-21 12:19:32
【问题描述】:

我在 SWRevealViewController 获得 nil 时遇到问题,
请看我下面的解释。

说明:

我的 2 个屏幕 View1View2 有 2 个菜单。 View1 是我使用自定义单元格的表格视图,我在自定义单元格中添加了一个按钮。
因此,当我单击该按钮时,它会跳转到 View2,但滑块导航不起作用。
因为 SWRevealViewController *revealViewController = self.revealViewController;View2

中获得 nil

这是我在UITableViewCell 类中编写的代码

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;
}

如果有人能帮我解决这个问题,那就太好了。

提前致谢。
米希尔·奥扎

【问题讨论】:

  • SWRevealViewController 仅在您将该 VC 作为前视图时才能获得。为什么你在展示的 VC 中需要 SWRevealViewController 对象?
  • 我在 View1 中添加了一个按钮来跳转到 Veiw2 并提供一些数据。
  • 那么如何将我的数据从一个 VC 传递到另一个 VC 呢?
  • 传递你不需要 SWRevealViewController 对象的数据,你可以通过你的 segue 传递它。顺便说一句,您想在下一个视图中传递什么?
  • View 2 是一个地图屏幕,我正在通过 view1 的 tableviewcell 传递我的坐标。

标签: ios objective-c swrevealviewcontroller presentviewcontroller


【解决方案1】:

迟到的答案,
在帕特里克的帮助下,我解决了这个问题。

在我的表格视图控制器中:

    - (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];
}  

以下是完整链接,可能对某人有用:
Present from one VC to another

【讨论】:

    【解决方案2】:

    您可以在 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:^{
    
        }];
    
    }
    

    或者如果您的表格单元格中有按钮,请在按钮中分配索引,如下所示:

    - (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。

    【讨论】:

    • presentViewController 似乎不起作用。再见了,我的按钮在 uitableviewcell 中而不是在 viewcontroller 中。
    猜你喜欢
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 2020-03-15
    • 2021-09-11
    • 2016-02-04
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多