【问题标题】:How can I switch views programmatically in a view controller? (Xcode, iPhone)如何在视图控制器中以编程方式切换视图? (Xcode,iPhone)
【发布时间】:2012-06-11 15:11:37
【问题描述】:

已经为此苦苦挣扎了一段时间,似乎永远无法得到直接的答案。

感谢任何帮助!

【问题讨论】:

    标签: swift uiviewcontroller swift3 storyboard


    【解决方案1】:

    如果您在导航控制器中:

    ViewController *viewController = [[ViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
    

    或者如果您只是想呈现一个新视图:

    ViewController *viewController = [[ViewController alloc] init];    
    [self presentViewController:viewController animated:YES completion:nil];
    

    【讨论】:

    • 请注意 presentModalViewController:animated:UIViewController docs 中已被标记为已弃用。应该改用presentViewController:animated:completion:
    • 我只是在这个问题上,如果我使用第二种解决方案,我是否能够回到以前的视图,以及如何?
    • 不用说,您现在很少通过调用allocinit 来实例化视图控制器。对于情节提要,您可以使用[self.storyboard instantiateViewControllerWithIdentifier@:"storyboard id"]。或者,如果使用带有 segue 的故事板,您只需直接使用 [self performSegueWithIdentifier:@"storyboard id" sender:self]; 执行 segue,这将实例化新的视图控制器并一举过渡到它。如果使用 NIB,如果 NIB 名称不同,您将使用 [[NSBundle mainBundle] loadNibNamed:@"nibname" owner:self options:nil] 实例化视图控制器。
    • @Rob。行代码 [self.storyboard instantiateViewControllerWithIdentifier@:"storyboard id"];执行多次然后每次都创建新的 *vc 对象?较早的 *vc 对象呢?这是内存泄漏吗?
    • 是的,每次它都会创建新实例。较早的实例将持续存在,直到您将它们从堆栈中解散/弹出。
    【解决方案2】:

    如果您想在同一个故事板中呈现新视图,

    在 CurrentViewController.m 中,

    #import "YourViewController.h"
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    YourViewController *viewController = (YourViewController *)[storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerIdentifier"];
    [self presentViewController:viewController animated:YES completion:nil];
    

    要将标识符设置为视图控制器, 打开 MainStoryBoard.storyboard。 选择你的视图控制器 查看-> 实用程序-> ShowIdentityInspector。 在那里你可以指定标识符。

    【讨论】:

    【解决方案3】:

    Swift 版本:

    如果您在导航控制器中:

    let viewController: ViewController = self.storyboard?.instantiateViewControllerWithIdentifier("VC") as ViewController
    self.navigationController?.pushViewController(viewController, animated: true)
    

    或者,如果您只是想呈现一个新视图:

    let viewController: ViewController = self.storyboard?.instantiateViewControllerWithIdentifier("VC") as ViewController
    self.presentViewController(viewController, animated: true, completion: nil)
    

    【讨论】:

      【解决方案4】:

      instantiateViewControllerWithIdentifierStoryboard ID

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

      【讨论】:

        【解决方案5】:

        通过 CmdSft 关闭使用先前答案代码调用的 Viewcontroller

        ViewController *viewController = [[ViewController alloc] init];    
        [self presentViewController:viewController animated:YES completion:nil];
        

        你可以使用

        [self dismissViewControllerAnimated:YES completion: nil];
        

        【讨论】:

          【解决方案6】:

          Swift 3.0 版本

          如果你想展示新的控制器。

          let storyboard = UIStoryboard(name: "Main", bundle: nil)
          let viewController = storyboard.instantiateViewController(withIdentifier: "controllerIdentifier") as! YourController
          self.present(viewController, animated: true, completion: nil)
          

          如果你想推送到另一个控制器(如果它在导航中)

          let storyboard = UIStoryboard(name: "Main", bundle: nil)
          let viewController = storyboard.instantiateViewController(withIdentifier: "controllerIdentifier") as! YourController
          self.navigationController?.pushViewController(viewController, animated: true)
          

          【讨论】:

          • 标识符 = 故事板 ID。要设置 Identifier,请在 Storyboard > Identity inspector > Identity 下的 Storyboard ID 中选择视图控制器。
          【解决方案7】:
          #import "YourViewController.h"
          

          推送包含导航栏和/或标签栏的视图:

          UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryboard" bundle:nil];
          YourViewController *viewController = (YourViewcontroller *)[storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerIdentifier"];
          [self.navigationController pushViewController:viewController animated:YES];
          

          要将标识符设置为视图控制器,请打开 YourStoryboard.storyboard。选择 YourViewController 视图-> 实用程序-> ShowIdentityInspector。在那里你可以指定标识符。

          【讨论】:

          • 由于 UX 变化,最后一句话不适用于 Xcode 10。
          【解决方案8】:

          这对我有用:

          NSTimer *switchTo = [NSTimer scheduledTimerWithTimeInterval:0.1
                     target:selfselector:@selector(switchToTimer)userInfo:nil repeats:NO];
          
          - (void) switchToTimer {
          UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
          UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"MyViewControllerID"]; // Storyboard ID
          [self presentViewController:vc animated:FALSE completion:nil];
          }
          

          【讨论】:

            【解决方案9】:
            [self.navigationController pushViewController:someViewController animated:YES];
            

            【讨论】:

            • 这对我不起作用,我在 tableView:didSelectRowAtIndexPath: 方法中这样做有关系吗?
            【解决方案10】:

            Swift 3.0 中

            一个视图控制器到第二个视图控制器去:

            let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MsgViewController") as! MsgViewController
                    self.navigationController?.pushViewController(loginVC, animated: true)
            

            第二个视图控制器到第一个视图控制器(后退):动作事件上的后退按钮:-

            self.navigationController?.popViewController(animated:true)
            

            3rdviewcontroller 到 1st viewcontroller 跳转

            self.navigationController?.popToRootViewController(animated:true)
            

            导航栏中最重要的故事板未点击,请确保执行此后退操作

            【讨论】:

              【解决方案11】:

              我认为 OP 是在询问如何在不更改 viewCONTROLLER 的情况下交换 VIEW。我是不是误解了他的问题?

              在伪代码中,他想做:

              let myController = instantiate(someParentController)
              
              let view1 = Bundle.main.loadNib(....) as... blah
              myController.setThisViewTo( view1 )
              
              let view2 = Bundle.main.loadNib(....) as... blah
              myController.setThisViewTo( view2 )
              

              我是不是把他的问题弄错了?

              【讨论】:

                猜你喜欢
                • 2010-10-29
                • 2014-09-30
                • 1970-01-01
                • 2011-05-09
                • 1970-01-01
                • 1970-01-01
                • 2017-11-30
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多