执行segue并推送2个viewcontrollers

时间:2015-12-03 09:56:20

标签: ios objective-c segue

我试图使用prepareForSegue方法同时推送一定数量的控制器,但是出现了这个错误:

嵌套推送动画可能导致导航栏损坏 和 在意外状态下完成导航过渡。导航栏子视图树可能已损坏。

这里是代码:

    require 'net/http'
    require 'json'
    require 'uri'
    uri = URI.parse('http://ashish-1:9090/csm/login')
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Post.new(uri.request_uri)
    request.set_form_data({"username" => 'test', "password" => 'test'})
    request.add_field("Authentication-Token", "")
    request.add_field("Authorization", "")
    request.add_field("Content-Type", "application/json")
    response = http.request(request)
    puts response
    puts response.code
    puts "Headers: #{response.to_hash}" #prints all headers except Authentication-Token
    puts response["session-id"] # get printed
    puts response["Authentication-Token"] # blank

为NSMutableArray中包含的每个对象调用prepareForSegue方法。如何在不收到此错误的情况下调用多个视图控制器? Segue在故事板中连接了正确的标识符。

2 个答案:

答案 0 :(得分:0)

如果你想将多个UIViewControllers推入导航控制器,你应该添加除了last之外的所有动画。 e.g。

/**
 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++;
    }
}

答案 1 :(得分:0)

有一种更简洁的方法来做上述答案:

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

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

    [navigationControllerStack addObjectsFromArray:arrayControllers];

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

}

注意,这仍然没有解决如何使用segue,但上面的答案也没有。