根据条件从同一按钮调用两个不同的控制器

时间:2012-11-24 05:51:15

标签: ios6 storyboard segue controllers

我有以下代码在iOS6之前工作,但我找不到一个简单的方法来使用iOS6,故事板和Xcode 4.5

-(IBAction) ButtonPressed:(id)sender{

    if(condition == 1]) {

        FirstController *firstController = [[FirstController alloc]initWithNibName:nil bundle:nil];
        firstController.delegate = self;
        firstController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:firstController animated:NO completion:nil];
        [firstController release];
    }

    SecondController *secondController = [[SecondController alloc]initWithNibName:nil bundle:nil];
        secondController.delegate = self;
        secondController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:secondController animated:NO completion:nil];
        [secondController release];

}

这曾经很好用。如果我将代码保留在iOS6中,则没有一个控制器会正确显示。尝试将控制器导航转换到故事板也无济于事,因为看起来我只能使用一个动作来触发使用从ButtonPressed到其中一个或另一个的模态segue的转换。 将第一个控制器嵌入第二个控制器的viewdiload中有其自身的问题。如果它在viewdidload上将无法正常工作,因为其他人已经经历过。如果我将它放在viewdidappear上,第二个控制器将首先显示创建一个令人不快的UI。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

视图控制器一次只能显示一个视图控制器。需要采用不同的方法。例如,您可以使用导航视图控制器来完成您想要完成的任务。如果用户已登录,则将上载视图控制器设置为导航视图控制器的根视图控制器。如果用户未登录,则将根视图控制器设置为登录视图控制器。当用户登录时,登录视图控制器将在其委托中通知用户已登录,然后您可以将上载视图控制器推送到导航堆栈中。

- (IBAction)buttonPressed:(id)sender{

    UINavigationController* nvc;

    if(condition == 1) {
        FirstController *firstController = [[FirstController alloc]initWithNibName:nil bundle:nil];
        firstController.delegate = self;
        firstController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        nvc = [[UINavigationController alloc] initWithRootViewController:firstController];
        [firstController release];
    }
    else {
        SecondController *secondController = [[SecondController alloc]initWithNibName:nil bundle:nil];
        secondController.delegate = self;
        secondController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        nvc = [[UINavigationController alloc] initWithRootViewController:secondController];
        [secondController release];
    }

    [self presentViewController:nvc animated:YES completion:nil];

    [nvc release];
}

//Login view controller delegate method
- (void)loginViewControllerDidLoginSuccessfully:(FirstController*)loginViewController{
    SecondController *secondController = [[SecondController alloc]initWithNibName:nil bundle:nil];
    secondController.delegate = self;
    secondController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [secondController.navigationItem setHidesBackButton:YES];
    [loginViewController.navigationController pushViewController:secondController animated:YES];
    [secondController release];
}