Rob
Rob

Reputation: 1065

viewDidLoad: Checking if from a segue?

Okay, so basically I have a main view controller - the main menu of my application. I have a button that brings a user to a table view, where they select something that needs to apply to the main view. The problem is, I don't know of a way to tell if the main view controller is being created from a segue or from the start of the application. Is there a way to check this? Should I just set a boolean or string for the viewDidLoad method to check, then modify it in prepareForSegue?

Upvotes: 2

Views: 586

Answers (2)

Firo
Firo

Reputation: 15566

So now that I better understand what you are after I can give you a more thorough answer. What you are really looking for is a pattern related to delegates and protocols. This is a way to send data between viewControllers without needing to know any real details about the parent (or delegate) controller. This is what you will want to do.

For the same of clarity I will be using two names for your controllers, mainViewController for your root controller and tableViewController for the instance of your UITableViewController subclass.

In the .h. of your tableViewController you will want to setup a protocol:

@protocol SingleSelectionDelegate
- (void)selectionHasBeenChosenWithOption:(NSString *)selection;
@end

@interface MyTableViewControllerSubclass : UITableViewController
// properties and method declarations

// this is your property you will use to send data back to your delegate (in this case your mainViewController)
@property (weak, nonatomic) id<SingleSelectionDelegate> selectionDelegate;
@end

Then in the .m (or .h) of your mainViewController you will need to add this:

// this imports the interface and also the protocol that you want
#import "MyTableViewControllerSubclass.h"

// the <Single...> part says you conform to this protocol and implement the methods it requires (in this case selectionHasBeenChosenWithOption:)
@interface MainViewController <SingleSelectionDelegate>
@end

@implementation MainViewController

// here is where you need to implement the required method
- (void)selectionHasBeenChosenWithOption:(NSString *)selection {
    // do what you want with the selection, assign it to a property, call other methods, pass it to other delegates, or whatever else.

    // now that you have your information you want the focus to come back to you so you
    [self.navigationController popToViewController:self animated:YES]; // works even if not the root
}


// you also need to set yourself as tableViewController's selectionDelegate, if you are using Storyboards you will do this in the prepareForSegue.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.destinationViewController isKindOfClass:[MyTableViewControllerSubclass class]]) {
    // you could also check a specific segue's identifier but this makes sense because if it is this subclass then it will have a selectionDelegate property

    // assign yourself as the delegate
    MyTableViewControllerSubclass *destination = (MyTableViewControllerSubclass *)segue.destinationViewController
    destination.selectionDelegate = self;
}

@end

Then the last step is calling the delegate method when you have information for your selectionDelegate. This is done in your tableViewController's .m. In this case I will do it in tableView:didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // getting the data you want to send back
    NSString dataToSendBack = [self.myArray[indexPath.row] description];

    // sending the data to your delegate (in this case mainViewController)
    [self.selectionDelegate selectionHasBeenChosenWithOption:dataToSendBack];

    // mainViewController is handling the popping so we do not need to do anything with the navigation stack
}

So that is basically what you are after. I typed almost all of this up in the text editor here so there could be some syntactical errors. Let me know and I can fix them. This is a heavily taught concept for iOS Development as you will end up using it all over the place. Learn it well and you could look for some other tutorials on it too. Like I said, you will find a ton of them! I remember it felt overwhelming for myself when I first learned it but now it is just second nature.

Upvotes: 3

Scott Berrevoets
Scott Berrevoets

Reputation: 16946

You could also check [UIApplication sharedApplication] delegate] window] rootViewController], but I would probably just go with a property.

Upvotes: 1

Related Questions