Navigation Controller Push View Controller
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)
}
Navigation Controller is not loading ( push , present ) another viewController
Here is the function you can change it like this and let me know the outcome
first run this one
@IBAction func buttonClicked(_ sender: UIButton) {
let signupVC = UIStoryboard.init(name:"SignUp", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
let navVC = UINavigationController.init(rootViewController: signupVC)
// self.present(navVC, animated: true, completion: nil)
if let nav = self.navigationController {
nav.pushViewController(navVC, animated: true)
} else {
print("navigation controller not found")
}
}
And then try to present ... and let me know what happened
@IBAction func buttonClicked(_ sender: UIButton) {
let signupVC = UIStoryboard.init(name:"SignUp", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
let navVC = UINavigationController.init(rootViewController: signupVC)
self.present(navVC, animated: true, completion: nil)
}
Push new view controller when button is pressed
Got an answer!
In my SceneDelegate.swift
I set:
let menuViewController = UINavigationController(rootViewController: MenuViewController())
window?.rootViewController = menuViewController
Now it works! Thanks for the comments everyone :)
Related Topics
What Is an "Unwrapped Value" in Swift
The Model Used to Open the Store Is Incompatible With the One Used to Create the Store
Always Pass Weak Reference of Self into Block in Arc
Detecting Sheet Was Dismissed on iOS 13
Run App For More Than 10 Minutes in Background
How to Pass Data Using Notificationcenter in Swift 3.0 and Nsnotificationcenter in Swift 2.0
Ios: How to Change App Language Programmatically Without Restarting the App
Best Way to Add License Section to iOS Settings Bundle
Array from Dictionary Keys in Swift
Making Gcm Work for iOS Device in the Background
Resizing Uitableview to Fit Content
How to Simultaneously Satisfy Constraints, Will Attempt to Recover by Breaking Constraint
Event Handling For iOS - How Hittest:Withevent: and Pointinside:Withevent: Are Related
Iterate Over Snapshot Children in Firebase
How to Locate the Cgrect For a Substring of Text in a Uilabel
Subclass Uiapplication With Swift
Opening View Controller from App Delegate Using Swift
Uibutton: How to Center an Image and a Text Using Imageedgeinsets and Titleedgeinsets