Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Controller Push View Controller

Question

How to navigate from one view controller to another simply using a button's touch up inside event?

More Info

What I tried in a sample project, in steps, was:

  1. Create the sample single view application.

  2. Add a new file -> Objective-C Class with XIB for user interface (ViewController2).

  3. Add a button into ViewController.xib and control click the button to ViewController.h to create the touch up inside event.

  4. Go to the newly made IBAction in ViewController.m and change it to this...

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

The code runs without errors and I tested the button's functionality with NSLog. However it still doesn't navigate me to the second view controller. Any help would be appreciated.

like image 577
Ahmed Elashker Avatar asked Dec 23 '13 11:12

Ahmed Elashker


People also ask

How do I push a presented ViewController?

VC3 -> present VC2 -> VC1 VC2 needs to be in a UINavigationController then after you present it you can push VC3. The back button will work as expected on VC3, for VC2 you should call dismiss when the back button is pressed. Try implementing some of that in code and then update your question.


Video Answer


1 Answers

Swift3

 **Push** 

do like

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

or safer

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

present

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

or safer

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

swift

//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) } 
like image 196
Anbu.Karthik Avatar answered Sep 24 '22 01:09

Anbu.Karthik