导航控制器推视图控制器

如何从一个视图控制器导航到另一个简单地使用button触摸内部事件?

更多信息

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

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

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

  3. 将一个button添加到ViewController.xib中,并控制单击button到ViewController.h以创build触摸内部事件。

  4. 转到在ViewController.m中新创build的IBAction并将其更改为…

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

代码运行没有错误,我用NSLogtesting了button的function。 但它仍然不会导航到第二个视图控制器。 任何帮助,将不胜感激。

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) } 
 UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"storyBoardName" bundle:nil]; MemberDetailsViewController* controller = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentiferInStoryBoard"]; [self.navigationController pushViewController:viewControllerName animated:YES]; 

对于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) } 

在你的button操作中使用这个代码(Swift 3.0.1):

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

使用这个代码导航下一个viewcontroller,如果你正在使用storyboard意味着按照下面的代码,

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

UINavigationController不会自动出现在UIViewController中。

这是你应该在Interface Builder中看到的。 文件所有者具有查看导航控制器的出口,并且从导航控制器出口到实际视图;

界面生成器

这是完美的:

PD:记得导入目标VC:

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

“导航”是我的导航控制器的故事板ID“vc”是我的第一个视图控制器的故事板ID连接到我的导航控制器

-希望这可以帮助

 AppDelegate to 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) Between ViewControllers: let loginPageView = self.storyboard?.instantiateViewControllerWithIdentifier("scoutPageID") as! ScoutPage self.navigationController?.pushViewController(loginPageView, animated: true) 

如果你正在使用Swift:

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

UIViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@“storyboardId”]; [self.navigationController pushViewController:vc animated:YES];

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