Resolved UIAlertView Unwind Segue

Discussion in 'iOS Programming' started by Blakeasd, Mar 20, 2014.

  1. Blakeasd, Mar 20, 2014
    Last edited: Mar 21, 2014

    Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello,

    I am working on a project using storyboards. The initial scene is a UITableViewController embedded in a Navigation Controller. When a button is pressed on this scene, it segues to a UIViewController that is embedded in another Navigation Controller. Upon the ViewController loading, a UIAlertView appears with a cancel button. This button is handled by my view controller. When is it pressed it should segue back to the TableView controller.

    Code:
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        if (buttonIndex == 0) {
            NSLog(@"Canceled.");
            [self.navigationController popViewControllerAnimated:YES];
            NSLog(@"The number of view controllers: %i", [[self.navigationController viewControllers] count]);
        }else{
                
        }
    }
    
    The NSLog fires as expected letting me know that the cancel button has been pressed. Then the popViewControllerAnimated: does nothing. According to the documentation it should set the previous view controller as the current one (in this case the TableViewController). When I log the number of view controllers in the stack, the value is 1.

    Is this because the TableViewController is being handled by a separate Navigation Controller?

    And if so, what is the best way to unwind the segue? Or, am I completely botching something?

    Could someone please help me understand my mistake(s)?

    Thanks!
     
  2. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #2
    You are popping one UINavigationController expecting it to have an effect on another UINavigationController.

    Is there a particular reason you're using two separate UINavigationControllers?
     
  3. Blakeasd, Mar 20, 2014
    Last edited: Mar 20, 2014

    Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #3
    I suspected that was the case.

    I am using two separate UINavigationControllers as Apple's Storyboards tutorial I read recently embedded each ViewController with a separate UINavigationController.

    This is my first time using storyboards as I am usually working on OS X programs where Storyboards are not an option.

    I know if I wasn't doing this through a UIAlertView I could connect a button in Interface Builder to exit: , but as far as I know, UIAlertView buttons can't be connected in IB.


    Is there a way I can still go backwards if I am using two navigation controllers?

    Thanks
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Which tutorial is this? Please be as specific as possible. That is, please provide a URL, at least. Thanks.
     
  5. Blakeasd, Mar 21, 2014
    Last edited by a moderator: Mar 21, 2014

    Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #5
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Does the segue to the second view present it modally or push it onto the navigation stack? Because you'll want to reverse this in your button action, either using dismissViewControllerAnimated:completion: or popViewControllerAnimated:
     
  7. Blakeasd, Mar 21, 2014
    Last edited: Mar 21, 2014

    Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #7
    It is presented modally. The methods you suggested do not work. I don't know why though. I suspect it is because my two view controllers are handled by two different navigation controllers. How should I move backwards with that being the case?

    Thanks.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Can you show me the code you used in your alertView:clickedButtonAtIndex: that calls dismissViewControllerAnimated:completion:?

    I tried the following and it works fine for me:
    Code:
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (buttonIndex == 0) {
            NSLog(@"Canceled.");
            [self dismissViewControllerAnimated:YES completion:^{}];
        }
    }
     
  9. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #9
    That works! I misread the method you suggested and thought it was one of the ones I already tried.

    Thanks for your help!
     

Share This Page