【问题标题】:Navigation Controller Push View Controller导航控制器推送视图控制器
【发布时间】:2014-01-11 15:00:07
【问题描述】:

问题

如何简单地使用按钮的 touch up inside 事件从一个视图控制器导航到另一个视图控制器?

更多信息

我在示例项目中尝试的步骤是:

  1. 创建示例单视图应用程序。

  2. 添加一个新文件 -> 带有 XIB 的 Objective-C 类用于用户界面 (ViewController2)。

  3. 在 ViewController.xib 中添加一个按钮并控制单击按钮到 ViewController.h 以创建 touch up inside 事件。

  4. 去ViewController.m中新建的IBAction改成这个...

    - (IBAction)GoToNext:(id)sender 
    {
        ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
    
        [[self navigationController] pushViewController:vc2 animated:YES];
    }
    

代码运行没有错误,我用 NSLog 测试了按钮的功能。但是它仍然没有将我导航到第二个视图控制器。任何帮助将不胜感激。

【问题讨论】:

  • 您是否真的创建了导航控制器并将其添加到 UI 中?您的 [self navigationController] 引用返回为零吗?

标签: ios objective-c iphone swift


【解决方案1】:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"storyBoardName" bundle:nil];
MemberDetailsViewController* controller = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentiferInStoryBoard"];
[self.navigationController pushViewController:controller animated:YES];

斯威夫特 4:

let storyBoard = UIStoryboard(name: "storyBoardName", bundle:nil)
let memberDetailsViewController = storyBoard.instantiateViewController(withIdentifier: "viewControllerIdentiferInStoryBoard") as! MemberDetailsViewController
self.navigationController?.pushViewController(memberDetailsViewController, animated:true)

【讨论】:

    【解决方案2】:

    假设 如果你想从 ViewController A --> B 那么

    1. 确保您的 ViewControllerA 嵌入到导航控制器中

    2. 在 ViewControllerA 的按钮单击中,您应该有这样的代码。

    @IBAction func goToViewController(_ sender: Any) {

        if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
    
            if let navigator = navigationController {
                navigator.pushViewController(viewControllerB, animated: true)
            }
        }
    }
    
    1. 请仔细检查您的情节提要名称,以及情节提要中为 ViewControllerB 的视图提到的 ViewControllerB 的标识符

    StoryboardID = ViewControllerB

    【讨论】:

      【解决方案3】:

      Swift 4 & 5

      let storyboard = UIStoryboard(name: "Main", bundle: nil)
              let vc = storyboard.instantiateViewController(withIdentifier: "yourController") as! AlgoYoViewController
              navigationController?.pushViewController(vc,
                                                       animated: true)
      

      【讨论】:

        【解决方案4】:

        AppDelegate 到 ViewController:

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let loginPageView = mainStoryboard.instantiateViewControllerWithIdentifier("leadBidderPagerID") as! LeadBidderPage
        var rootViewController = self.window!.rootViewController as! UINavigationController
        rootViewController.pushViewController(loginPageView, animated: true)
        

        视图控制器之间:

        let loginPageView = self.storyboard?.instantiateViewControllerWithIdentifier("scoutPageID") as! ScoutPage
        self.navigationController?.pushViewController(loginPageView, animated: true)
        

        【讨论】:

          【解决方案5】:
          UIViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboardId"];
          [self.navigationController pushViewController:vc animated:YES];
          

          【讨论】:

            【解决方案6】:

            在您的按钮操作中使用此代码 (Swift 3.0.1):

            let vc = self.storyboard?.instantiateViewController(
                withIdentifier: "YourSecondVCIdentifier") as! SecondVC
            
            navigationController?.pushViewController(vc, animated: true)
            

            【讨论】:

            • @EricAya 我对这个 stackOverFlow 很陌生,所以刚刚发布了一些我看到并为自己申请的内容:)
            • @EricAya 好的先生
            【解决方案7】:

            Swift3

             **Push**
            

            喜欢

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as NewsDetailsViewController 
             vc.newsObj = newsObj
             navigationController?.pushViewController(vc,
             animated: true)
            

            或者更安全

              if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController {
                    viewController.newsObj = newsObj
                    if let navigator = navigationController {
                        navigator.pushViewController(viewController, animated: true)
                    }
                }
            

            呈现

               let storyboard = UIStoryboard(name: "Main", bundle: nil)
               let vc = self.storyboard?.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as! NewsDetailsViewController
                  vc.newsObj = newsObj
                       present(vc!, animated: true, completion: nil)  
            

            或者更安全

               if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController
                 {
            
                 vc.newsObj = newsObj
                present(vc, animated: true, completion: nil)
                }
            
            
            
            
            
            //Appdelegate.m
            
            - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
            {
                self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
                // Override point for customization after application launch.
                self.viewController = [[ViewController alloc] initWithNibName:@"ViewController"
                                                                   bundle:nil];
                UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController];
                self.window.rootViewController = navigation;
                [self.window makeKeyAndVisible];
                return YES;
            }
            
            
            //ViewController.m
            
            - (IBAction)GoToNext:(id)sender 
            {
                ViewController2 *vc2 = [[ViewController2 alloc] init];     
                [self.navigationController pushViewController:vc2 animated:YES];
            }
            

            迅速

            //Appdelegate.swift
            
            func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
                self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            
                let navigat = UINavigationController()
                let vcw = ViewController(nibName: "ViewController", bundle: nil)
            
                // Push the vcw  to the navigat
                navigat.pushViewController(vcw, animated: false)
            
                // Set the window’s root view controller
                self.window!.rootViewController = navigat
            
                // Present the window
                self.window!.makeKeyAndVisible()
                return true
            }
            
            //ViewController.swift
            
            @IBAction func GoToNext(sender : AnyObject)
            {
                let ViewController2 = ViewController2(nibName: "ViewController2", bundle: nil)
                self.navigationController.pushViewController(ViewController2, animated: true)
            }
            

            【讨论】:

            • 谢谢,我不知道为什么我不记得以前做过这一步,但它肯定对我有用。
            • 嗯,很好,每次都不要忘记在应用程序委托中执行此操作,k
            • 我通过推送视图控制器使用 appdelegate 但在 swift 3.0 中使用后视图控制器
            【解决方案8】:

            使用此代码导航下一个视图控制器,如果您使用情节提要,请按照以下代码进行操作,

            UIStoryboard *board;
            
            if (!self.storyboard)
            {
                board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            }
            else
            {
                board = self.storyboard;
            }
            
            ViewController *View = [board instantiateViewControllerWithIdentifier:@"yourstoryboardname"];
            [self.navigationController pushViewController:View animated:YES];
            

            【讨论】:

              【解决方案9】:

              如果您使用的是 Swift:

              let controller = self.storyboard!.instantiateViewControllerWithIdentifier("controllerID")
              self.navigationController!.pushViewController(controller, animated: true)
              

              【讨论】:

                【解决方案10】:
                -  (void) loginButton:(FBSDKLoginButton *)loginButton
                didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
                                error:(NSError *)error{
                    UINavigationController *nav = [self.storyboard instantiateViewControllerWithIdentifier:@"nav"];
                    ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"LoggedInVC"];
                    [nav pushViewController:vc animated:YES];
                    [self presentViewController:nav animated:YES completion:nil];
                }
                

                "nav" 是我的导航控制器的 Storyboard ID “vc”是连接到导航控制器的第一个视图控制器的 Storyboard ID

                -希望这会有所帮助

                【讨论】:

                  【解决方案11】:

                  这是完美的工作:

                  PD:记得导入目标VC:

                  #import "DestinationVCName.h"
                  
                      - (IBAction)NameOfTheAction:(id)sender 
                  {
                         DestinationVCName *destinationvcname = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationVCName"];
                      [self presentViewController:destinationvcname animated:YES completion:nil];
                  }
                  

                  【讨论】:

                    【解决方案12】:

                    Swift 使用以下代码:

                     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
                        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
                        self.window!.backgroundColor = UIColor.whiteColor()
                    
                        // Create a nav/vc pair using the custom ViewController class
                    
                        let nav = UINavigationController()
                        let vc = NextViewController(nibName: "NextViewController", bundle: nil)
                    
                        // Push the vc onto the nav
                        nav.pushViewController(vc, animated: false)
                    
                        // Set the window’s root view controller
                        self.window!.rootViewController = nav
                    
                        // Present the window
                        self.window!.makeKeyAndVisible()
                        return true
                    
                    }
                    

                    视图控制器:

                     @IBAction func Next(sender : AnyObject)
                    {
                        let nextViewController = DurationDel(nibName: "DurationDel", bundle: nil)
                    
                        self.navigationController.pushViewController(nextViewController, animated: true)
                    }
                    

                    【讨论】:

                      【解决方案13】:

                      UINavigationController 不会自动呈现在 UIViewController 中。

                      这是您应该在 Interface Builder 中看到的内容。文件所有者有导航控制器的视图出口,导航控制器是实际视图的出口;

                      【讨论】:

                      • 也许我曾经创建过自动拥有导航控制器的项目。如果是这样,那么创建项目的方法是什么,以便我可以找到默认的导航控制器。
                      • 打开 ViewController 的 nib 并按照我的说明进行更改。
                      【解决方案14】:

                      首先创建导航控制器并将其作为窗口对象的 rootViewController 提供。

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2012-01-23
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多