【问题标题】:Calling two different controllers from the same button based on a condition根据条件从同一个按钮调用两个不同的控制器
【发布时间】:2012-11-12 09:15:17
【问题描述】:

我在 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。有什么想法吗?

【问题讨论】:

  • 一个仅供参考:Objective C 中的最佳实践表明您应该以小写字母开头方法名称。换句话说,将您的“ButtonPressed”操作声明更改为“buttonPressed”。
  • 您是否在这里缺少其他条件?如果条件 == 1,您是否同时执行?
  • 不,如果条件为 1,那么它将首先运行第一个控制器,然后运行第二个。如果条件不是一个,那么它将只运行第二个控制器。我想做的事情是将文件上传到互联网。如果用户没有注册,那么他必须通过第一个控制器(注册),然后是第二个控制器(上传)。如果用户已注册,则可以直接进入上传控制器。

标签: ios6 storyboard segue controllers


【解决方案1】:

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

- (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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    相关资源
    最近更新 更多