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.
This question is related to
ios
objective-c
iphone
swift
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)
If you are using Swift:
let controller = self.storyboard!.instantiateViewControllerWithIdentifier("controllerID")
self.navigationController!.pushViewController(controller, animated: true)
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];
}
Create the navigation controller first and provide it as rootViewController of your window object.
UIViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboardId"];
[self.navigationController pushViewController:vc animated:YES];
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)
}
Swift 4 & 5
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "yourController") as! AlgoYoViewController
navigationController?.pushViewController(vc,
animated: true)
- (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
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
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)
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"storyBoardName" bundle:nil];
MemberDetailsViewController* controller = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentiferInStoryBoard"];
[self.navigationController pushViewController:viewControllerName 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)
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];
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;
Source: Stackoverflow.com