9to5ios
9to5ios

Reputation: 5545

Back button handling from a view in both present and push

I have a UIViewControllerC which is call from two another UIViewcontroller

ViewControllerA ViewControllerB

From ViewControllerA to go to UIViewControllerC i have to make it presentviewcontroller

 UIViewControllerC *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"UIViewControllerC"];
    [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    vc.tagfrom=@"present";
    [self presentViewController:vc animated:NO completion:nil];

From ViewControllerB to go to UIViewControllerC i have to make it push view

UIViewControllerC *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"UIViewControllerC"];
        [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        vc.tagfrom=@"push";
        [self.navigationController pushViewController:vc animated:YES];

Now i have to back from both view on back button i check tagfrom condition and handle it

-(IBAction)backBtn:(id)sender
{
 if ([tagfrom isEqualToString:@"present"])
  {
    [self dismissViewControllerAnimated:NO completion:nil];
}
else
{
[self.navigationController popViewControllerAnimated:YES];
}

} 

Which working fine in both senerio, but sometimes my push view behaves like presentmodelveiw, and having no transition effects in it, please help me to resolve it

enter image description here

Upvotes: 0

Views: 226

Answers (1)

Tarun Tyagi
Tarun Tyagi

Reputation: 10102

First of all, you don't need tagfrom property on your UIViewController subclass.

A UIViewController instance has a property called presentingViewController which will let you know about the viewController ( A in your case ) that presented current viewController ( C in your case ).

-(IBAction)backBtn:(id)sender
{
   if (nil != self.presentingViewController) {
      [self dismissViewControllerAnimated:NO completion:nil];
   }
   else {
      [self.navigationController popViewControllerAnimated:YES];
   }
} 

Second, modalTransitionStyle is supposed to work with only presentation and NOT push / pop transitions. If you are using this with push / pop, the behavior is undefined because it is not meant to be used there.

Upvotes: 3

Related Questions