I am building an iOS app in swift and upon launching the app I would like to first show viewController2, which is presented on top of viewController1, which is embedded in a UINavigationController. The key part being that ViewController2 is not part of the navigation stack and is presented instead of being pushed.
This is my current attempt which doesn't work and only shows ViewController1 upon launch.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController1 = mainStoryboard.instantiateViewController(withIdentifier: "VC1") as! ViewController1
let viewController2 = mainStoryboard.instantiateViewController(withIdentifier: "VC2") as! ViewController2
let navController = UINavigationController(rootViewController: viewController1)
viewController1.present(viewController2, animated: false)
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
return true
}
I have this structure in place because I enable a user to be able to swipe from left to right and right to left to get to viewController2 and viewController3 in a similar fashion to Snapchat. Presenting these controllers seemed the best idea as they don't have navigation bars and once finished you would want to return to viewControlller1 - perhaps I need to change the structure of my app but would ideally not like to as I aim to submit this mvp within the next week.
Help much appreciated from you iOS and swift wizards.
// Think I need to change the structure of the app to include viewController2 on the navigation stack :( as using accepted answer causes viewController1 to be seen briefly before viewController2 is presented which is logical - in the long run this will be better despite the short term pain
rootViewController should be set before everything. Do it in this order and it should work fine.
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
navController.present(viewController2, animated: false)
Related
When developing an app, if I only want to test one screen, say, the third tab of a tab view, how would I make the app navigate there on start up?
I think a good solution is use a UI test to automatically navigate to the correct place in the app. Pausing on a breakpoint at the end of the test leaves the app to be played with manually.
rootViewController of window property in AppDelegate will be the first view controller shown on screen. you can make it by programming or using storyboard
by programming:
if NavigationController is the rootViewController of your window, put your own viewController in the first place (index at 0) of NavigationController's viewControllers which is an array. it will be on screen by default.
customNavigationController.viewControllers = [yourViewController]
or simply set your viewController to the rootViewController to the window property in appDelegate
AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
// window?.rootViewController = CustomTabBarViewController()
// customViewController will show on screen by default.
window?.rootViewController = CustomViewController()
window?.makeKeyAndVisible()
return true
}
by interface builder:
check your viewControlelr's attributes inspector panel, and check "is Initial View Controller" option, then you can see a simple arrow attached to this view controller.
or:
add identifier to your viewController, and fetch it from storyBoard, then set it to the rootViewController of the window object in appDelegate
let testController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "testController") as! TestController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = testController
I'm using the Material Framework by CosmicMind. Currently I'm trying to replace the ViewController once a row is selected in the SideNavigationController. Unfortunately I can't figure out how to do this. There're similar question here on StackOverflow (#1) unfortunately the solutions don't work for me.
Following my code in the AppDelegate.swift class:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myViewController = storyboard.instantiateViewControllerWithIdentifier("MyViewController")
let navigationController: NavigationController = AppNavigationController(rootViewController: myViewController)
let sideNavigationController: SideNavigationController = SideNavigationController(rootViewController: navigationController, leftViewController: AppLeftViewController())
// further code
return true
}
With this code everything works fine. The ViewController (MyViewController) is shown.
Now I'm trying to replace the MyViewController with MySecondViewController however this doesn't work. Following the code I've been using:
sideNavigationController?.transitionFromRootViewController(MySecondViewController())
The result is that the Toolbar disappears and I can't close the SideNavigationController anymore. So I've tried the following:
let navigationController: NavigationController = AppNavigationController(rootViewController: MySecondViewController())
sideNavigationController?.transitionFromRootViewController(navigationController)
With this code the Toolbar is visible once again but the problem with the SideNavigationController remains -> means: It can't be closed.
tl;dr
How do I replace the rootViewController of the NavigationController properly?
The NavigationController is a subclass of UINavigationController. So changing the rootViewController is as iOS made it. For example:
navigationController?.pushViewController(MySecondViewController(), animated: true)
The SideNavigationController offers the ability to transition its rootViewController. This is different, as the SideNavigationController uses child UIViewControllers and swaps them when asked. For example:
sideNavigationController?.transitionFromRootViewController(MySecondViewController())
Sometimes, people use the sideNavigationController to early. For example, you instantiate a new UIViewController, and place the sideNavigationController code in the videDidLoad method. This won't work since the new UIViewController was not yet added to the sideNavigationController view hierarchy. To solve this, use the viewWillAppear method.
The last situation to consider is when you want to load a new UIViewController in the rootViewController of the NavigationController from the sideNavigationController. In order to do this, and let's consider that the sideNavigationController's rootViewController is the NavigationController, you would need to do this:
(sideNavigationController?.rootViewController as? NavigationController)?.pushViewController(MySecondViewController(), animated: true)
This should help you out :)
My application has a ViewController i.e. the first screen that opens when app is started. I added a UITabBarController to the application through mainstoryboard. I changed the title of UITabBarController to "myTabBarController". Now how do I access this TabBarController from the viewDidLoad function of already existing ViewController?
amazed! Why do you need view controller if you want to load UITabBarController from viewDidLoad function of view controller???
However solution can be like this:
You can make segue: right click in view controller then drag to tabBarController and select segue "show".
But, better would be if you embed UITabBarController to view controller which loads your TabBarController at the beginning of your app.
1: Select View Controller
2: Go to [Editor] in menu bar of xcode
3: Select Embed In
4: Select Tab Bar Controller
and you are done :)
other solution would be:
delete the ViewController from storyboard
select tabBarController that you added.
go to "attribute inspector" in left pane and select "Is initial View Controller"
ADDED
1. Give identifier to TabBarController like given by 張家齊
2. Add button in ViewController and make Action of it in ViewController's class (to make action. right click the added button and drag to ViewController class and select Action instead of Outlet.)
3. Now add following codes in that action:
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewControllerWithIdentifier("myTabBarController") //vc is instance of TabBarController.
self.presentViewController(vc, animated: true, completion: nil)
DONE :)
In AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let main = storyboard.instantiateViewControllerWithIdentifier("myTabBarController") as! myTabBarController
self.window!.rootViewController = main
self.window!.makeKeyAndVisible()
return true
}
And you should set Identifier of myTabBarController in your Main.storyboard.
I hope this is a simple question. I have not found anything in SO that seems to fit what I'm trying to do. I am working on my first ever project so much of this is still quite new to me. The code I have pasted is from a test project I'm using before I add code to my real one.
My initial VC is the EULA. When the user accepts, I want to change the initial VC to the log in view. In this test project, I have two views with an IBAction for a Bar Button Item that will segue the project from VC to VC2. I am trying to set up code to change the initial VC in the AppDelegate with
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let storyboard = UIStoryboard(name: "View Controller", bundle: NSBundle.mainBundle())
let licenseAccepted = ?
var vc: UIViewController?
if !licenseAccepted {
vc = storyboard.instantiateViewControllerWithIdentifier("View Controller2")
} else {
vc = storyboard.instantiateInitialViewController()
}
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = vc
window?.makeKeyAndVisible()
return true
}
but this is not working. First, let licenseAccepted = ? is not correct. I found that in another set of code and I thought it might work, but no. Also, how do I set a var for if the user accepts the license in the AppDelegate? Should that be done in the initial VC, the second VC that will become the initial VC or somewhere else?
I am using Xcode 7.1 and Swift2 if that helps.
I may be way off base or I may be a keystroke or two away. I am just not getting my head around this. Your assistance will be appreciated.
I have a tabbar app with an initial login screen. The tabbarController is set as the initial view in Storyboard with 1 VC that has a navigationController also embed.
I have a loginVC instantiated and set as rootViewController in my AppDelegate. After the user has successfully sign in from the loginVC, I need to switch over to the tabbarController. Would I try to get a reference to the tabbarcontroller and set it as the new rootviewcontroller? If so, I'm having a hard time figuring out how to do this:/
AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if NSUserDefaults.standardUserDefaults().objectForKey("OAuth") == nil {
self.window = UIWindow(frame:UIScreen.mainScreen().bounds)
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var loginVC = storyboard.instantiateViewControllerWithIdentifier("LOGIN_VC") as LoginVC
self.window?.rootViewController = loginVC
self.window?.makeKeyAndVisible()
}
return true
}
This method gets called after user has successfully signed in
func dismissLoginVC() {
var tabbarController = self.storyboard?.instantiateViewControllerWithIdentifier("TABBAR") as UITabBarController
self.presentViewController(tabbarController, animated: true, completion: nil)
let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
appDelegate.window?.rootViewController = tabbarController
}
I know the problem with this is it just created a new tabbarcontroller, rather than referencing the existing tabbarController that was set as the initialView in storyboard. This seems to work but it is missing other items from the navigation bar.
Thanks for pointing me in the right direction!
I think you should change your app structure. Keep the tab bar controller as the initial view controller in the storyboard, and present modally (with no animation) the login controller from the viewDidAppear method of the controller in the first tab -- it will be the first thing the user sees. When you dismiss it, you will be back to that controller in the first tab. With this approach, you don't need any code in the app delegate.