简体   繁体   中英

IOS how to segue an array to another view controller

I have an one initial view controller(A) and one navigation controller link to another view controller(B). How do i pass in the array from A to Big through segue?

This is my code for the segue.

  -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"prepareForSegue: %@", segue.identifier);

    if ([segue.identifier isEqualToString:@"severityInfo"]) {
        UINavigationController *segueNavigation = [segue destinationViewController];
        NSLog(@"%@",[[segueNavigation viewControllers]objectAtIndex:0]);
        IncidentOverviewViewController *B = (IncidentOverviewViewController *)[[segueNavigation viewControllers]objectAtIndex:0];
        B.BArray = feeds;
    }
}

I have this error after running this code: reason: '-reason: 'Could not find a navigation controller for segue 'severityInfo'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'

I have declare the method in my second view controller(incident view controller) which looks like this:

-(void)setFeeds:(NSMutableArray*)received
{
    BArray = received;

    NSLog(@"feeds are%@", BArray);

}

I have insert the identifier in the storyboard for the segue from my initial view controller call "view controller" this initial view controller is connected to a navigation view controller then from the navigation view controller connection to my destination view controller call "incidentOverviewViewController"

This is how this is done

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"prepareForSegue: %@", segue.identifier);

    if ([segue.identifier isEqualToString:@"severityInfo"]) {
        UINavigationController *segueNavigation = [segue destinationViewController];
        ViewController *B = (ViewController *)[segueNavigation topViewController];
        [B setBArray:feeds];

    }
}

Yes, you need to define the method in the view controller you are calling. How else could you call it? You define the method as any other method.

You should also cast it to the IncidentOverviewViewController , if that is the type, not into ViewController .

Method will be something like this

-(void)setBArray:(whatevertype)array
{
    Dowhatever
}

Depending on your types and storage methods.

Create an array as a property in your B view controller and in the segue method do following:-

if ([segue.identifier isEqualToString:@"severityInfo"])
    UINavigationController *segueNavigation = [segue destinationViewController];
    ViewController *B = (ViewController *)[segueNavigation topViewController];
    B.array = feeds;
}

Try this

  -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
     if ([segue.identifier isEqualToString:@"severityInfo"]) {
        UINavigationController *segueNavigation = [segue destinationViewController];
       NSLog(@"%@",[[segueNavigation viewControllers]objectAtIndex:0]);
        IncidentViewControllerClassName *B = (IncidentViewControllerClassName *)[[segueNavigation viewControllers]objectAtIndex:0];
        B.BArray = feeds;
    }
}

And in the IncidentViewControllerClassName.h file

 @property(nonatomic,strong) NSArray *BArray;

To operate Push segue you need to Embedd a navigation controller to the View Controller From which you are segueing to another View Controller. So embedd your First VC in a Navigation Controller. Your storyboard should look like this

NavController -->(RootVC)--->VC1 (segue:severityInfo)---NavigationControlelr--(root)VC2.

Hope could make you understanding why the error is persisting. To clear it off, Embedd VC1 or the view controller you are pushing from as the Root VC of a Navigation controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM