Reputation: 1000
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:
Create the sample single view application.
Add a new file -> Objective-C Class with XIB for user interface (ViewController2).
Add a button into ViewController.xib and control click the button to ViewController.h to create the touch up inside event.
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.
Upvotes: 48
Views: 184941
Reputation: 695
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"storyBoardName" bundle:nil];
MemberDetailsViewController* controller = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentiferInStoryBoard"];
[self.navigationController pushViewController:controller animated:YES];
Swift 4:
let storyBoard = UIStoryboard(name: "storyBoardName", bundle:nil)
let memberDetailsViewController = storyBoard.instantiateViewController(withIdentifier: "viewControllerIdentiferInStoryBoard") as! MemberDetailsViewController
self.navigationController?.pushViewController(memberDetailsViewController, animated:true)
Upvotes: 19
Reputation: 35783
Let's Say If you want to go from ViewController A --> B then
Make sure your ViewControllerA is embedded in Navigation Controller
In ViewControllerA's Button click you should have code like this.
@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)
}
}
}
Look at StoryboardID = ViewControllerB
Upvotes: 1
Reputation: 1754
Swift 4 & 5
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "yourController") as! AlgoYoViewController
navigationController?.pushViewController(vc,
animated: true)
Upvotes: 0
Reputation: 14296
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)
Upvotes: 0
Reputation: 279
UIViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboardId"];
[self.navigationController pushViewController:vc animated:YES];
Upvotes: 1
Reputation: 679
Use this code in your button action (Swift 3.0.1):
let vc = self.storyboard?.instantiateViewController(
withIdentifier: "YourSecondVCIdentifier") as! SecondVC
navigationController?.pushViewController(vc, animated: true)
Upvotes: 3
Reputation: 82759
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)
}
Upvotes: 66
Reputation: 749
Using this code for Navigating next viewcontroller,if you are using storyboard means follow this below code,
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];
Upvotes: 2
Reputation: 5116
If you are using Swift:
let controller = self.storyboard!.instantiateViewControllerWithIdentifier("controllerID")
self.navigationController!.pushViewController(controller, animated: true)
Upvotes: 1
Reputation: 301
- (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" is the Storyboard ID for my navigation controller "vc" is the Storyboard ID for my first view controller connected to my navigation controller
-hope this helps
Upvotes: 0
Reputation: 4734
This is working perfect:
PD: Remember to import the destination VC:
#import "DestinationVCName.h"
- (IBAction)NameOfTheAction:(id)sender
{
DestinationVCName *destinationvcname = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationVCName"];
[self presentViewController:destinationvcname animated:YES completion:nil];
}
Upvotes: 1
Reputation: 8349
For Swift use the below code:
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
}
ViewController:
@IBAction func Next(sender : AnyObject)
{
let nextViewController = DurationDel(nibName: "DurationDel", bundle: nil)
self.navigationController.pushViewController(nextViewController, animated: true)
}
Upvotes: 2
Reputation: 5368
UINavigationController is not automatically presented in UIViewController.
This is what you should see in Interface Builder. Files owner has view outlet to Navigation controller and from navigation controller is outlet to actual view;
Upvotes: 0
Reputation: 17186
Create the navigation controller first and provide it as rootViewController of your window object.
Upvotes: -1