2014-04-08 111 views
1

下一個按鈕不起作用。它會拋出一個錯誤Application tried to present modally an active controller當我按next按鈕時,我應該怎樣通過ViewControllers數組來枚舉。我已經閱讀了許多有關同一問題的線索,但無法找出解決方案。提前致謝!枚舉位於堆棧中的ViewControllers

.h文件中

@property(nonatomic, strong) NSMutableArray *tab; 

.m文件

- (IBAction)newTab:(id)sender { 
    UIStoryboard *st = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]]; 
    myViewController *newTab = [st instantiateViewControllerWithIdentifier:@"myViewController"]; 
    if (![newTab isBeingPresented]) { 
     [self presentViewController:newTab animated:YES completion:nil]; 

     [tab addObject:newTab]; 
} 
} 
- (IBAction)next:(id)sender { 

    [tab enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) { 


     [self presentViewController:tab[idx] animated:YES completion:nil]; 
    }]; } 




- (IBAction)prev:(id)sender { 

    for (i=[tab count]; i>0; i--) { 
    if (![self isBeingDismissed]){ 
    [tab[i-1] dismissViewControllerAnimated:YES completion:nil]; 
     ; 
}}} 
+0

您不能再次展示相同的視圖控制器而不會解除它。將創建新實例成爲您的問題或那是什麼你不想要? –

回答

0

您可以枚舉出現在堆棧

NSArray * controllerArray = [[self navigationController] viewControllers]; 

for (UIViewController *controller in controllerArray){ 
    //Code here.. e.g. print their titles to see the array setup; 
    NSLog(@"%@",controller.title); 
    if ([controller.title isEqual:@"Title1"]) { 
     [self.navigationController popToViewController:controller animated:YES]; 
    } 
} 

的ViewControllers在選擇下一個按鈕,通過目前在堆棧視圖枚舉並檢查標題並進行該視圖所需的特定操作。以上我添加了代碼到popViewController如果標題匹配。

希望它有幫助。