【问题标题】:iOS: present view controller programmaticallyiOS:以编程方式呈现视图控制器
【发布时间】:2013-04-15 16:30:12
【问题描述】:

我正在使用presentViewController:animated:completion: 方法转到另一个视图控制器。

这是我的代码:

AddTaskViewController *add = [[AddTaskViewController alloc] init];
[self presentViewController:add animated:YES completion:nil];

此代码转到另一个UIViewController,但另一个控制器为空。我一直在使用故事板,但现在我需要在代码中完成。

【问题讨论】:

  • 这不是 Xcode 问题。另外,我无法推断您所说的“另一个控制器什么都不是”的意思,所以我保持原样。

标签: ios objective-c uiviewcontroller uistoryboard presentviewcontroller


【解决方案1】:

您需要从故事板身份检查器设置故事板 ID

 AddTaskViewController *add=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboard_id"];
 [self presentViewController:add animated:YES completion:nil];

【讨论】:

    【解决方案2】:

    最好的办法是

    AddTaskViewController * add = [self.storyboard instantiateViewControllerWithIdentifier:@"addID"];
    [self presentViewController:add animated:YES completion:nil];
    

    【讨论】:

      【解决方案3】:

      试试这个代码:

      [self.navigationController presentViewController:controller animated:YES completion:nil];
      

      【讨论】:

        【解决方案4】:
        LandingScreenViewController *nextView=[self.storyboard instantiateViewControllerWithIdentifier:@"nextView"];
        [self presentViewController:nextView animated:YES completion:^{}];
        

        【讨论】:

        【解决方案5】:

        尝试以下方法:

        NextViewController *nextView = [self.storyboard instantiateViewControllerWithIdentifier:@"nextView"];
        [self presentViewController:nextView animated:YES completion:NULL];
        

        【讨论】:

          【解决方案6】:

          你的代码:

           AddTaskViewController *add = [[AddTaskViewController alloc] init];
           [self presentViewController:add animated:YES completion:nil];
          

          这段代码可以转到另一个控制器,但是你得到一个新的 viewController,而不是你故事板的控制器,你可以这样做:

          AddTaskViewController *add = [self.storyboard instantiateViewControllerWithIdentifier:@"YourStoryboardID"];
          [self presentViewController:add animated:YES completion:nil];
          

          【讨论】:

            【解决方案7】:

            在 Swift 中你可以这样做:

            let vc = UIViewController()
            self.presentViewController(vc, animated: true, completion: nil)
            

            【讨论】:

            • 这只是加载一个空白的 UIViewController
            • 当然可以。你必须用你自己的viewcontroller替换它,或者在uiviewcontroller的view中添加某种颜色或对象
            【解决方案8】:

            如果您正在使用 Storyboard 并且您的“添加”视图控制器在情节提要中,则在设置中为您的“添加”视图控制器设置一个标识符,以便您可以执行以下操作:

            UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"NameOfYourStoryBoard" 
                                                                 bundle:nil];
            AddTaskViewController *add = 
                       [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentifier"];
            
            [self presentViewController:add 
                               animated:YES 
                             completion:nil];
            

            如果您在情节提要或 nib 文件中没有“添加”视图控制器,并且想要以编程方式创建整个内容,那么 appDocs 会说:

            如果您无法在情节提要或 nib 文件中定义视图,请覆盖 loadView 方法以手动实例化视图层次结构并将其分配给视图属性。

            【讨论】:

              【解决方案9】:

              如果您使用情节提要,您可能不应该使用allocinit 来创建新的视图控制器。相反,请查看您的故事板并找到您想要执行的segue;它应该有一个唯一的标识符(如果没有,您可以在右侧边栏中设置一个)。

              一旦你找到了那个 segue 的标识符,就给你的 current 视图控制器发送一个-performSegueWithIdentifier:sender 消息:

              [self performSegueWithIdentifier:@"mySegueIdentifier" sender:self];
              

              这将导致情节提要实例化一个 AddTaskViewController 并以您为该 segue 定义的方式呈现它。


              另一方面,如果您根本不使用情节提要,那么您需要为 AddTaskViewController 提供某种用户界面。最常见的做法是使用 nib 初始化控制器:您将调用 -initWithNibName:bundle: 并提供包含添加任务 UI 的 .xib 文件的名称,而不是仅调用 init

              AddTaskViewController *add = [[AddTaskViewController alloc]
                                            initWithNibName:@"AddTaskView" bundle:nil];
              [self presentViewController:add animated:YES completion:nil];
              

              (还有其他(不太常见的)方法可以获取与新视图控制器关联的视图,但这可能会给您带来最少的工作麻烦。)

              【讨论】:

              • 如果它与一个按钮结合使用它的工作。但没有合并。
              • 那么你如何在非故事板场景中“取消呈现”新的视图控制器?我正在谷歌搜索试图找到类似于 unwindForSegue:towardsViewController: 的东西
              • 其实,我刚刚找到了“dismissViewController”,我认为这是正确的答案。
              • 谢谢。为我工作。
              猜你喜欢
              • 2021-04-28
              • 1970-01-01
              • 2014-01-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-05-09
              • 2011-01-20
              相关资源
              最近更新 更多