简体   繁体   中英

send an array to next view controller

In my app, I have 4 view controllers, (login,welcome,game view and results view). In the game view, I have two additional views (like this)[screen shot link to the views]. I am using a web api to get data, and my data is consist of items(each item has 1 image and 4 string). Item number is not constant so based on the amount of items(which I store it inside an array), I transition my views from one to another until I ran out of items.(The way I present is really similar to view transitions sample from apple. After displaying the last item, I use perform segue to go to next view.

The problem that I am trying to solve

I am trying to create an additional view/ or view controller that uses some part of the information from my items array.

my question

Why I am getting null for container views array ?(_results array is the array that has the items)

this is what I tried:

based on this question :

GameView

ContainerViewController *container = [[ContainerViewController alloc]init];
container.array = _results;  
[self performSegueWithIdentifier:@"changetoResults" sender:self];

ContainerViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"items in the array - containerviewcontroller.m %@",array);   
}

this is the console:

2013-11-26 19:11:29.157 GuessTheImage[3664:70b] there are 8 , of items in the current index -gameviewcontroller.m
2013-11-26 19:12:44.439 GuessTheImage[3664:70b] items in the array - containerviewcontroller.m (null)

Thank you.

Because the ViewController that you create isn't the ViewController that you go to via the segue. If you'd like to use a segue, you can get the destination ViewController (it already exists) in a prepareForSegue method and tune it however you want (in your case - add an array).

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"changetoResults"])
    {
        ContainerViewController *vc = (ContainerViewController *)[segue destinationViewController];
        vc.array = _results;
    }
}

There are two things that you are not doing correctly:

  • You are setting the array on a temporary view controller different from the instance activated by your segue, and
  • You are logging the array too early: viewDidLoad: is called before the segue code manages to put the array into the new view controller.

To fix the first problem, remove the two lines above the "perform segue" call, and add this method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"changetoResults"]) {
        ContainerViewController *container = segue.destinationViewController;
        container.array = _results;
    }
}

To fix the second problem, move the logging code into the viewDidAppear: . NSLog should start showing the correct number of items.

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