Clarification on segues needed (prepare for segue never gets called)

Discussion in 'iOS Programming' started by chrono1081, Jul 9, 2014.

  1. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #1
    Hi guys,

    Can someone clarify something for me?

    From my understanding anytime a segue is about to happen the prepareForSegue method is called.

    However, If I have a UIPopover with a popover segue that method never gets called when going from the popover view, back to the main view and because of this I can't figure out how to send data back.

    Is there a different method that gets called for popovers? No one online seems to address this issue they all go from the main view to the popover but never in reverse.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Segues only go one direction. But, if I were you, I would look into a feature added in iOS 6: unwind segues.
     
  3. chrono1081 thread starter macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #3
    Thanks! I'll take a look at that and see if that solves the issue.
     
  4. chrono1081, Jul 10, 2014
    Last edited: Jul 10, 2014

    chrono1081 thread starter macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #4
    Ok I think I may be doing something wrong here. I was following this tutorial: http://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them

    Which says to use this method in the view controller that initializes the segue:

    Code:
    - (IBAction)unwindToThisViewController:(UIStoryboardPopoverSegue *)unwindSegue
    {
        NSLog(@"Unwind!");
    }
    I have just a single main view with a button that calls the UIPopover (that is using a segue). The UIPopover has its exit set to the "unwindToThisViewController" method.

    unwindToThisViewController gets called but it gets called a split second after I touch the button that triggers the popover. I hit the button, the date picker appears, and then I may get to turn one of the wheels but then it disappears and unwindToThisViewController is executed.

    Did I do something wrong? I would like the date to stay up there until the user clicks away from the popover.


    EDIT: I figured out how to do the rest of what I need to do, I just can't figure out how to solve the early dismissing issue.

    My code currently reads the date from the date picker and displays it in the label. For that I have:

    This method in the ViewController.m file (This is the main view controller)
    Code:
    - (IBAction)unwindToThisViewController:(UIStoryboardPopoverSegue *)unwindSegue
    {
        if([[unwindSegue identifier] isEqual: @"DatePickerVCUnwindSegue"])
        {
            DatePickerViewController *datePickerViewController = unwindSegue.sourceViewController;
            
            //Grab the date from the date picker
            NSDate *date = [datePickerViewController.datePicker date];
            
            //Create a date formatter object and set the format
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
            [dateFormatter setDateFormat:@"MM/dd/yyyy"];
            
            //Set the label to display the formatted date
            self.myLabel.text = [dateFormatter stringFromDate:date];
        }
    
    }
    This connects to my DatePickerPopover through a segue. The popover works, I can read the data from the date and set the label, but here's what I think is going wrong and I'm not quite sure how to fix it.

    I think that when I drag from the UIDatePickerView to the Exit and choose my unwind segue that the program is seeing the UIDatePickerView as a button and dismissing the popover (almost like I had a button there). What I need to do is find a way to do that when the user taps the screen instead to dismiss the view.

    Is there a method or something I am overlooking that allows me to that?
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Instead of having the popover dismissed when a selection is made (in case the user wants to change their selection), I would add a Done button to the UI and have that trigger the unwind-segue.
     
  6. chrono1081 thread starter macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #6
    i currently did this as a work around but the testers at work are whining about having to push an extra button :/

    Do you happen to know offhand if there is a way to trigger the unwind when the view is dismissed?

    (Also thank you for pointing out the unwind segue, it worked like a charm!)
     

Share This Page