Question

One of the things I don't like about WYSIWYG/visual programming, is when you get to a point where you need to step outside of the box, you're left scratching your head. I'm at such a point right now along my iOS learning curve.

I have a custom UICollectionView thing. In two places (create and edit points), I need to present a list to the user to enable/disable (select/deselect) items from a list. So I go to the storyboard and whip up something like this:

enter image description here

In the past, following tutorials, I would control-drag a link from some control to the NavigationController show in the middle, I would tell it was a modal segue, tune a couple of methods, and get an arrow connecting the two for my efforts.

But in this case, I don't have obvious points to start that action from. I do have a + button. But it needs to do some other things first, and if all is well, then programmatically initiate the open, and somehow get notified of the state when it returns. Same for the individual cells, they might want to configure my table controller, and then open it, and be notified when it closes.

So I'm looking for a recipe of how one does this, what the key methods I should be looking for are. Apple's docs are great for reference, but I find figuring out how to do something from scratch difficult.

Was it helpful?

Solution

After creating a segue in your storyboard, you can initiate a segue any time programmatically by calling

[self performSegueWithIdentifier:@"segueID" sender:person];

Where "segueID" is a string you set for your segue in interface builder in the Identifier field in the identity inspector (right menu panel, 4th tab).

The segue does not need to be created from a control, you can just create one directly from one view controller to another. I usually do this on the right side menu by right-clicking on one view controller object and dragging to another one. This way, it acts as a segue that you can initiate programmatically any time you want.

As for getting notified when you come back to a view controller, (unless I'm misunderstanding your question) you can use either:

  • (void)viewWillAppear:(BOOL)animated
  • (void)viewDidAppear:(BOOL)animated

OTHER TIPS

Create a UINavigationController programmatically with a desired view controller set as a root view controller. Here is an example of what you could put in a method invoked when user taps the plus button:

UIViewController *vc = [[UIStoryboard storyboardWithName:@"YourStoryboardName" bundle:nil] instantiateViewControllerWithIdentifier:@"YourViewControllerID"];

UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];

[self presentViewController:nc
                   animated:YES completion:nil];

To get a state, or information about the selected items you can use Delegation and declare a protocol. Example:

@protocol YourSampleDelegate <NSObject>

- (void)didSelectItem:(NSObject *)item;

@end

Then, your view controller (the one with the plus sign) should implement this protocol:

@interface ViewController : UIViewController<YourSampleDelegate>
...
@end

@implementation ViewController

...

#pragma mark - YourSampleDelegate conformance

- (void)didSelectItem:(NSObject *)item;
{
    // Do something with the item.
}

@end

You also have to create a delegate property in a view controller with collection view and set the view controller with a plus as a delegate. There are tons of examples on the Internet. I hope this shows you the right direction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top