2015-12-03 25 views
0

我试图用prepareForSegue方法在同一时间推控制器的某些电话号码,但得到这个错误:执行SEGUE和推2个viewcontrollers

嵌套推动画可以导致损坏的导航栏 和 完成意外状态下的导航转换。导航栏子视图树可能会损坏。

这里的代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {  
    if ([[segue identifier] isEqualToString:@"rawSegue"]){ 
     MyController * aController = [segue destinationViewController]; 
     [aController setText:theText]; 
     [aController setType:Type]; 
     [aController setCountry:country]; 
    } 
} 

的prepareForSegue方法被称为对包含在一个NSMutableArray每个对象。我怎么能调用多个视图控制器没有得到这个错误? Segue在故事板中连接了正确的标识符。

+1

你能否粘贴你用来推送视图控制器的确切代码 – Burhan

+0

问题不明确,你想用segue同时推两个控制器吗?同时显示多个ViewController或打开控制器排队? – Atika

+0

警告说明了这一切。如果在结束一个转换之前推送多个视图控制器,可能会导致导航栏损坏。也就是说,如果按下后退按钮,它可能会弹出顶视图控制器,并且您可能无法弹出其他视图控制器,或者您可以弹出一个视图控制器,并且可能有后退按钮,因此无法起作用等。 – Johnykutty

回答

0

如果要将多个UIViewControllers推入导航控制器,则应添加除最后一个以外的所有动画。例如

/** 
A method to push multiple UIViewControllers into UINavigationController. 
You can push multiple controller, but the trick is you push them without animation, 
only last is pushed with animation. 
@param arrayControllers An array of UIViewController to be pushed at once in Navigation Controller 
*/ 
-(void)pushMultipleViewControllers:(NSMutableArray<UIViewController *> *)arrayControllers 
{ 
    int index = 0; 
    for (UIViewController *controller in arrayControllers) { 
     BOOL isAnimated = NO; 
     if(index==arrayControllers.count-1){ 
      //Only last view controller should be inserted with animation 
      isAnimated=YES; 
     } 
     [self.navigationController pushViewController:controller animated:isAnimated]; 
     index++; 
    } 
} 
+0

那么,我应该写这个方法?最后但并非最不重要的是:viewController的数量是动态的,因为它们来自一个NSMutableArray ... – yorh

+0

我更新了代码,以便您可以将NSMutableArray传递到单个方法中。当你想推控制器而不是继续时,你应该手动调用这个方法。例如,在按钮单击或cellSelectedAtIndex等上调用此方法。 –

+0

可以在prepareFor Segue方法中执行相同的操作吗? – yorh

0

有做上面的回答更简洁的方式:

- (void)pushMultipleViewControllers:(NSMutableArray<UIViewController *> *)arrayControllers { 

    NSMutableArray *navigationControllerStack = self.navigationController.viewControllers.mutableCopy; 

    [navigationControllerStack addObjectsFromArray:arrayControllers]; 

    [self.navigationController setViewControllers:navigationControllerStack animated:YES]; 

} 

注意,这仍然没有解决如何用SEGUE做到这一点,但同样没有上述问题的答案。