Search code examples
iosobjective-cswrevealviewcontrollerpresentviewcontroller

Present from one VC's button to another VC makes SWrevealviewcontroller found nil


I am having an issue with SWRevealViewController getting nil,
Please see my below explanation.

Explanation:

I have 2 menus for my 2 screens View1, View2. View1 is a table view in which I have used custom cell, I have added one button in custom cell.
So, when I click on that button It jumps to the View2, But the slider navigation doesn't work.
Because SWRevealViewController *revealViewController = self.revealViewController; getting nil in View2

Here is my code which I have written in my UITableViewCell class

UIViewController *view = [[[UIApplication sharedApplication] keyWindow] rootViewController];
 View2* mapviewController = [view.storyboard instantiateViewControllerWithIdentifier:@"MapView"];

[view presentViewController:mapviewController animated:NO completion:^{}];

Code which I have written in my 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;
}

It would be great if someone can help me to resolve this issue.

Thanks in advance.
Mihir Oza


Solution

  • Late answer,
    With the help of Patrick I resolve that issue.

    In my table view controller:

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

    In my custom 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];
    }  
    

    Below is the full link may be it will useful for someone :
    Present from one VC to another