I am creating an app where I have a UICollectionView with 8 different items plugged in programmatically in my viewDidLoad method, like so:

- (void)viewDidLoad
{
    [super viewDidLoad];
    navItems = [[NSMutableArray alloc]init];
    [navItems addObject:@"personnel"];
    [navItems addObject:@"equipment"];
    [navItems addObject:@"tasks"];
    [navItems addObject:@"triage"];
    [navItems addObject:@"logs"];
    [navItems addObject:@"mapping"];
    [navItems addObject:@"headsup"];
    [navItems addObject:@"messenger"];
}

I want to attach a segue to each of these, to bring each to a new page in my app. Some ways I thought about doing it:

A: Manually enter in 8 items into the storyboard and choose a unique tag for each that I can connect the segue to OR ctrl + click into the segue (I basically tried this and it seemed to get really confused... couldn't get the code to compile)

B: Set a key on the items when I add the objects, so something like [navItems addObject:@"personnel" setKey:@"1"];, then in the segue, call forKey.

I'm having trouble figuring out the right way to phrase arguments and I'm not sure if this is even possible... so:

What is the best way to add a separate segue to each of my navigation items in the collectionView and how can I go about this? Thanks

有帮助吗?

解决方案

I'm sure this is asked and answered elsewhere, but...

You'll need to define your segues in Interface Builder by ctrl-dragging from the view controller (in the bar below the view) to the destination view controller. Make sure you give each of them a unique identifier.

To invoke the segues, override collectionView:didSelectItemAtIndexPath: and call [self performSegueWithIdentifier:<segue identifier here> sender:self], probably in a big if...else... statement.

其他提示

I think you need to do a modal segue based on the object that was selected. For example:

in the implementation of this delegate method for collection view controllers:

collectionView:didSelectItemAtIndexPath:

{
    //get the array item using index path: you probably have 1 section
    NSString *object=[navItems objectAtIndex:[indexPath.row]];

   //now use switch case to check what string presents which view controller
   switch (indexPath.row){
       case 0:
              [self presentViewController:[MYVC1 new] animated:YES completion:NULL];
       break;

       //do this case for whatever objects you want to modally segue
   }

}

Unfortunately you can't case on NSString as of iOS7. If you set the array, you know which index has what string and you refer to that view controller inside case block.

I know its tedious but try to improve on it. Its way better than hooking 8 segues on storyboard. Make sure you aren't doing Push Segue else you need to do [self.naviagtionController pushVC:Animated];

Hope it helps!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top