John-Lluch / SWRevealViewController

A UIViewController subclass for presenting side view controllers inspired on the FaceBook and Wunderlist apps, done right !
Other
4.52k stars 987 forks source link

Present from one VC to another VC makes SWrevealviewcontroller found nil #788

Closed ozamihir1990 closed 6 years ago

ozamihir1990 commented 6 years ago

Hello, 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 you can help me to resolve this issue.

Thanks in advance. Mihir Oza

iDevelopper commented 6 years ago

Your mapviewController is not presented with SWRevealViewController, so it does not know it.

Use:

self.revealViewController().pushFrontViewController(mapviewController, animated: true)

Or setMainViewController:animated

ozamihir1990 commented 6 years ago

It is not working see my updated code.

 SWRevealViewController *revealViewController;
    UIViewController *view = [[[UIApplication sharedApplication] keyWindow] rootViewController];
    MapVC* mapviewController = [view.storyboard instantiateViewControllerWithIdentifier:@"MapView"];
    [revealViewController pushFrontViewController:mapviewController animated:YES];

I am callig from tableviewcell class.

iDevelopper commented 6 years ago

When you write:

SWRevealViewController *revealViewController; it is nil

You have to retrieve the instance of SWRevealViewController in your table view controller or pass it as a reference to the table view cell class.

ozamihir1990 commented 6 years ago

Could you please give me an example.

iDevelopper commented 6 years ago

In your 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 your 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];
}
ozamihir1990 commented 6 years ago

It works. Thank you so much to spend your valuable time for me and gave me the solution.

ozamihir1990 commented 6 years ago

small doubt, if I have one detail view of View1 and if I want to jump from the detail of View1 to View2 at that scenario what should I do?

iDevelopper commented 6 years ago

You can replace the front view controller like that:

        [self.revealViewController setFrontViewController:vc animated:YES];
ozamihir1990 commented 6 years ago

It is not working because it's View1's detail screen. did I miss something?

iDevelopper commented 6 years ago

How do you present your detail view from view 1?

ozamihir1990 commented 6 years ago

I am calling from uitableviewcell class.

 UIViewController *view = [[[UIApplication sharedApplication] keyWindow] rootViewController];
    DetailVC* detail = [view.storyboard instantiateViewControllerWithIdentifier:@"DetailView"];
    //passing data from View1 to DetailVC.
    detail.revealViewController = _rvc;
    // for iPad issue : 
    detail.modalPresentationStyle = UIModalPresentationFullScreen;
    [view presentViewController:detail animated:true completion:nil];
iDevelopper commented 6 years ago

The controller who present the detailVC must be the front view one not the reveal view controller itself:

    SWRevealViewController *view = (SWRevealViewController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
    DetailVC *detail = [view.storyboard instantiateViewControllerWithIdentifier:@"DetailView"];
    //passing data from View1 to DetailVC.
    //detail.revealViewController = _rvc;
    // for iPad issue :
    detail.modalPresentationStyle = UIModalPresentationFullScreen;
    [view.frontViewController presentViewController:detail animated:YES completion:nil];
}
ozamihir1990 commented 6 years ago

It is not working In Uitableviewcell class :

SWRevealViewController *view = (SWRevealViewController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
DetailViewController *detail = [view.storyboard instantiateViewControllerWithIdentifier:@"iPhoneDetailView"];

//passing data from View1 to DetailVC.
detail.modalPresentationStyle = UIModalPresentationFullScreen;
[view.frontViewController presentViewController:detail animated:YES completion:nil];

In details screen button pressed :

MapViewController* mapviewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MapView"];
//passing data from DetailVC to View2.

[self.revealViewController setFrontViewController:mapviewController animated:YES];

I have added breakpoint in setFrontViewController but it didn't call. I think it is because my self.revealViewController is nil. Any suggestions?

iDevelopper commented 6 years ago

In your UITableViewCell subclass:

- (void)buttonTaped:(void *)sender {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    DetailVC *detail = [sb instantiateViewControllerWithIdentifier:@"Detail"];
    detail.modalPresentationStyle = UIModalPresentationFullScreen;
    [_rvc presentViewController:detail animated:YES completion:nil];
}

In your DetailVC:

- (IBAction)goToView2ButtonTaped:(UIButton *)sender {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"View2"];
    [self dismissViewControllerAnimated:NO completion:nil];
    SWRevealViewController *rvc = (SWRevealViewController *)self.presentingViewController;
    [rvc setFrontViewController:vc animated:NO];
    [rvc setFrontViewPosition:FrontViewPositionLeft animated:NO];
}
ozamihir1990 commented 6 years ago

Thank you It's working. I think you are master of SWRevealViewController

iDevelopper commented 6 years ago

I wrote mine too:

https://github.com/iDevelopper/PBRevealViewController

inspired from this one but with more functionalities.

ozamihir1990 commented 6 years ago

Is it Include those functionalities which I stuck?