質問

my problem is that my NSMutableArray always get the last element with the objectatindex-method.

I have an array with some classes derived from UIViewController. I want to show one View after another.

first i fill the array:

ContactViewController *ContactView = [[ContactViewController alloc]init];
QuestionViewController *QuesView = [[QuestionViewController alloc]init];;
ContactView.mydelegate = self;
[QuesView setDelegate:self];


[Views addObject:ContactView];

Views =[[NSMutableArray alloc]initWithCapacity:11];
for (int i = 0; i < 11; i++) {
    [QuesView setParam:@"text" :i ];
    [Views addObject:QuesView];
}

after that i want to get the actual view and jump to the next like that:

-(IBAction)Button:(id)sender
{

    UIViewController *newView = [[UIViewController alloc]init];
    newView =  (UIViewController*)[Views objectAtIndex:enumerator];
    [self presentViewController:newView animated:YES completion: nil];
    enumerator++;
     //dismiss old view here....

    NSLog(@"number %d",[newView getNumber]);
}

the new view is not shown and the number in the log is always the last number of the array. I tried to go with a for loop through all element in "Views" but there is always the last number in every object....

any hints?

役に立ちましたか?

解決

You probably want to create multiple QuestionViewController instances. But you actually create only one object and call setParam on it 11 times.

Also this line [Views addObject:ContactView] has no effect, because you create a new array object and assign it to Views in the next line. The same thing with UIViewController *newView = [[UIViewController alloc]init]. Hope you are using ARC, otherwise this would create a memory leak!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top