Set UITabBarController as rootViewController
I have a UITabBarController
and I want to show this screen and not the login screen if the user session is still active.
My UITabBarController
has 3 ViewControllers
and the problem is that I don't see the TabBar
links in the bottom and I'm unable to navigate.
Without the following code everything works fine. I mean after login I can navigate.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
if Auth.auth().currentUser != nil {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = HomeTabBarController()
}
return true
}
I have also tried the following code to set the rootViewController
but it's the same problem.
When I try to set one of the other view controllers as root (one of the children), the Tab Bar doesn't show at all
var rootView: MyRootViewController = MyRootViewController()
if let window = self.window{
window.rootViewController = rootView
}
What am I doing wrong here?
ios swift
add a comment |
I have a UITabBarController
and I want to show this screen and not the login screen if the user session is still active.
My UITabBarController
has 3 ViewControllers
and the problem is that I don't see the TabBar
links in the bottom and I'm unable to navigate.
Without the following code everything works fine. I mean after login I can navigate.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
if Auth.auth().currentUser != nil {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = HomeTabBarController()
}
return true
}
I have also tried the following code to set the rootViewController
but it's the same problem.
When I try to set one of the other view controllers as root (one of the children), the Tab Bar doesn't show at all
var rootView: MyRootViewController = MyRootViewController()
if let window = self.window{
window.rootViewController = rootView
}
What am I doing wrong here?
ios swift
1
"My UITabBarController has 3 ViewControllers" Really? I don't see you giving HomeTabBarController any child view controllers. Show the code where that happens. Thanks.
– matt
Jan 3 at 1:31
Hi! I have in my storyboard three relationship segues to the TabBarController. Please check my new screenshot in the updated question.
– Rexhin Hoxha
Jan 3 at 1:35
1
Okay but you are sayingwindow?.rootViewController = HomeTabBarController()
. That is not the one in the storyboard.
– matt
Jan 3 at 1:40
If I set it like thiswindow?.rootViewController = HomeTabBarController
I get this error:Cannot assign value of type 'HomeTabBarController.Type' to type 'UIViewController?'
– Rexhin Hoxha
Jan 3 at 1:44
add a comment |
I have a UITabBarController
and I want to show this screen and not the login screen if the user session is still active.
My UITabBarController
has 3 ViewControllers
and the problem is that I don't see the TabBar
links in the bottom and I'm unable to navigate.
Without the following code everything works fine. I mean after login I can navigate.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
if Auth.auth().currentUser != nil {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = HomeTabBarController()
}
return true
}
I have also tried the following code to set the rootViewController
but it's the same problem.
When I try to set one of the other view controllers as root (one of the children), the Tab Bar doesn't show at all
var rootView: MyRootViewController = MyRootViewController()
if let window = self.window{
window.rootViewController = rootView
}
What am I doing wrong here?
ios swift
I have a UITabBarController
and I want to show this screen and not the login screen if the user session is still active.
My UITabBarController
has 3 ViewControllers
and the problem is that I don't see the TabBar
links in the bottom and I'm unable to navigate.
Without the following code everything works fine. I mean after login I can navigate.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
if Auth.auth().currentUser != nil {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = HomeTabBarController()
}
return true
}
I have also tried the following code to set the rootViewController
but it's the same problem.
When I try to set one of the other view controllers as root (one of the children), the Tab Bar doesn't show at all
var rootView: MyRootViewController = MyRootViewController()
if let window = self.window{
window.rootViewController = rootView
}
What am I doing wrong here?
ios swift
ios swift
edited Jan 3 at 7:41
Kaushik Makwana
960623
960623
asked Jan 3 at 1:28


Rexhin HoxhaRexhin Hoxha
89121947
89121947
1
"My UITabBarController has 3 ViewControllers" Really? I don't see you giving HomeTabBarController any child view controllers. Show the code where that happens. Thanks.
– matt
Jan 3 at 1:31
Hi! I have in my storyboard three relationship segues to the TabBarController. Please check my new screenshot in the updated question.
– Rexhin Hoxha
Jan 3 at 1:35
1
Okay but you are sayingwindow?.rootViewController = HomeTabBarController()
. That is not the one in the storyboard.
– matt
Jan 3 at 1:40
If I set it like thiswindow?.rootViewController = HomeTabBarController
I get this error:Cannot assign value of type 'HomeTabBarController.Type' to type 'UIViewController?'
– Rexhin Hoxha
Jan 3 at 1:44
add a comment |
1
"My UITabBarController has 3 ViewControllers" Really? I don't see you giving HomeTabBarController any child view controllers. Show the code where that happens. Thanks.
– matt
Jan 3 at 1:31
Hi! I have in my storyboard three relationship segues to the TabBarController. Please check my new screenshot in the updated question.
– Rexhin Hoxha
Jan 3 at 1:35
1
Okay but you are sayingwindow?.rootViewController = HomeTabBarController()
. That is not the one in the storyboard.
– matt
Jan 3 at 1:40
If I set it like thiswindow?.rootViewController = HomeTabBarController
I get this error:Cannot assign value of type 'HomeTabBarController.Type' to type 'UIViewController?'
– Rexhin Hoxha
Jan 3 at 1:44
1
1
"My UITabBarController has 3 ViewControllers" Really? I don't see you giving HomeTabBarController any child view controllers. Show the code where that happens. Thanks.
– matt
Jan 3 at 1:31
"My UITabBarController has 3 ViewControllers" Really? I don't see you giving HomeTabBarController any child view controllers. Show the code where that happens. Thanks.
– matt
Jan 3 at 1:31
Hi! I have in my storyboard three relationship segues to the TabBarController. Please check my new screenshot in the updated question.
– Rexhin Hoxha
Jan 3 at 1:35
Hi! I have in my storyboard three relationship segues to the TabBarController. Please check my new screenshot in the updated question.
– Rexhin Hoxha
Jan 3 at 1:35
1
1
Okay but you are saying
window?.rootViewController = HomeTabBarController()
. That is not the one in the storyboard.– matt
Jan 3 at 1:40
Okay but you are saying
window?.rootViewController = HomeTabBarController()
. That is not the one in the storyboard.– matt
Jan 3 at 1:40
If I set it like this
window?.rootViewController = HomeTabBarController
I get this error: Cannot assign value of type 'HomeTabBarController.Type' to type 'UIViewController?'
– Rexhin Hoxha
Jan 3 at 1:44
If I set it like this
window?.rootViewController = HomeTabBarController
I get this error: Cannot assign value of type 'HomeTabBarController.Type' to type 'UIViewController?'
– Rexhin Hoxha
Jan 3 at 1:44
add a comment |
4 Answers
4
active
oldest
votes
I was facing same issue and I came across your post, the problem with your code is that HomeTabBarController()
is creating a whole new TabBarController so to fix it try the following approach I used.
if Auth.auth().currentUser != nil {{
print("******************************User Present******************************")
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// create view controllers from storyboard
// Interface Builder -> Identitiy Inspector -> Storyboard ID
// Set up the Tab Bar Controller to have two tabs
let tabBarController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
// Make the Tab Bar Controller the root view controller
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
}
Edit Make sure to add the Identifier to your TabBarController
// Interface Builder -> Identitiy Inspector -> Storyboard ID
add a comment |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//// this code changes the initial point of aap///////
window = UIWindow(frame: UIScreen.main.bounds)
let nav = UINavigationController()
let myview = SettingTabbar()
nav.viewControllers = [myview]
window?.rootViewController = nav
window?.makeKeyAndVisible()
return true
}
And Function SettingTabbar is:
func SettingTabbar()->(UITabBarController)
{
//Setting TabBar
let tabbar = UITabBarController()
//Designing Tabbar Item Images
let table = UITabBarItem(title: nil, image:UIImage(named: "002-list") , tag: 0)
let collection = UITabBarItem(title: nil, image: UIImage(named: "001-collect"), tag: 1)
let insert = UITabBarItem(title: nil, image: UIImage(named: "add"), tag: 2)
//Getting TabBar ViewControllers
let TableView = newViewController()
let CollectionView = PersonCollectionViewController()
let InsertRec = nextViewController()
//Setting ViewControllers on TabBar Items
TableView.tabBarItem = table
CollectionView.tabBarItem = collection
InsertRec.tabBarItem = insert
let controllers = [TableView,CollectionView,InsertRec]
tabbar.viewControllers = controllers
tabbar.viewControllers = controllers.map{UINavigationController(rootViewController: $0)}
//Setting Title
tabbar.navigationItem.title = "Person Record"
return tabbar
}
1
This method is without storyboard
– Muhammad Hashim
Jan 3 at 9:58
1
if you want that method then delete storyboard also from info.plist
– Muhammad Hashim
Jan 3 at 9:59
add a comment |
The problem is this line:
window?.rootViewController = HomeTabBarController()
That is the wrong HomeTabBarController. It is a totally new HomeTabBarController with no children. You need to fetch the HomeTabBarController that’s in the storyboard.
But how can I do that. If I set it like thiswindow?.rootViewController = HomeTabBarController
I get this error:Cannot assign value of type 'HomeTabBarController.Type' to type 'UIViewController?'
– Rexhin Hoxha
Jan 3 at 1:44
add a comment |
I finally found the solution: As @matt suggested I had to fetch the HomeTabBarController that’s in the storyboard.
window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
// controller identifier sets up in storyboard utilities
// panel (on the right), it called Storyboard ID
let viewController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
window?.makeKeyAndVisible()
window?.rootViewController = viewController
1
You could also just set the HomeTabBarController to be the initial view controller in the story board, and all the above code would be unnecessary.
– Claus Jørgensen
Jan 3 at 2:04
How can I do that? I mean I still need to show the login form if the user session has clicked log out
– Rexhin Hoxha
Jan 3 at 2:05
1
Read up about adding child view controllers (it's a thing, but commonly forgotten by many iOS devs), or just general navigation flow. It's all about how you structure your project, and making a custom window and setting the root VC manually is just going to cause you headaches later on.
– Claus Jørgensen
Jan 3 at 2:07
Do you meanperformSegue()
by 'adding child view controllers'. Because right now what I have found is one usingrootViewController
and the other isperformSegue()
. Could you give me some more information please on which is the best way to show or to hide the login screen depending if the session is active or not.
– Rexhin Hoxha
Jan 3 at 2:15
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015256%2fset-uitabbarcontroller-as-rootviewcontroller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
I was facing same issue and I came across your post, the problem with your code is that HomeTabBarController()
is creating a whole new TabBarController so to fix it try the following approach I used.
if Auth.auth().currentUser != nil {{
print("******************************User Present******************************")
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// create view controllers from storyboard
// Interface Builder -> Identitiy Inspector -> Storyboard ID
// Set up the Tab Bar Controller to have two tabs
let tabBarController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
// Make the Tab Bar Controller the root view controller
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
}
Edit Make sure to add the Identifier to your TabBarController
// Interface Builder -> Identitiy Inspector -> Storyboard ID
add a comment |
I was facing same issue and I came across your post, the problem with your code is that HomeTabBarController()
is creating a whole new TabBarController so to fix it try the following approach I used.
if Auth.auth().currentUser != nil {{
print("******************************User Present******************************")
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// create view controllers from storyboard
// Interface Builder -> Identitiy Inspector -> Storyboard ID
// Set up the Tab Bar Controller to have two tabs
let tabBarController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
// Make the Tab Bar Controller the root view controller
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
}
Edit Make sure to add the Identifier to your TabBarController
// Interface Builder -> Identitiy Inspector -> Storyboard ID
add a comment |
I was facing same issue and I came across your post, the problem with your code is that HomeTabBarController()
is creating a whole new TabBarController so to fix it try the following approach I used.
if Auth.auth().currentUser != nil {{
print("******************************User Present******************************")
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// create view controllers from storyboard
// Interface Builder -> Identitiy Inspector -> Storyboard ID
// Set up the Tab Bar Controller to have two tabs
let tabBarController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
// Make the Tab Bar Controller the root view controller
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
}
Edit Make sure to add the Identifier to your TabBarController
// Interface Builder -> Identitiy Inspector -> Storyboard ID
I was facing same issue and I came across your post, the problem with your code is that HomeTabBarController()
is creating a whole new TabBarController so to fix it try the following approach I used.
if Auth.auth().currentUser != nil {{
print("******************************User Present******************************")
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// create view controllers from storyboard
// Interface Builder -> Identitiy Inspector -> Storyboard ID
// Set up the Tab Bar Controller to have two tabs
let tabBarController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
// Make the Tab Bar Controller the root view controller
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
}
Edit Make sure to add the Identifier to your TabBarController
// Interface Builder -> Identitiy Inspector -> Storyboard ID
answered Jan 13 at 7:55


Ussaid IqbalUssaid Iqbal
5841515
5841515
add a comment |
add a comment |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//// this code changes the initial point of aap///////
window = UIWindow(frame: UIScreen.main.bounds)
let nav = UINavigationController()
let myview = SettingTabbar()
nav.viewControllers = [myview]
window?.rootViewController = nav
window?.makeKeyAndVisible()
return true
}
And Function SettingTabbar is:
func SettingTabbar()->(UITabBarController)
{
//Setting TabBar
let tabbar = UITabBarController()
//Designing Tabbar Item Images
let table = UITabBarItem(title: nil, image:UIImage(named: "002-list") , tag: 0)
let collection = UITabBarItem(title: nil, image: UIImage(named: "001-collect"), tag: 1)
let insert = UITabBarItem(title: nil, image: UIImage(named: "add"), tag: 2)
//Getting TabBar ViewControllers
let TableView = newViewController()
let CollectionView = PersonCollectionViewController()
let InsertRec = nextViewController()
//Setting ViewControllers on TabBar Items
TableView.tabBarItem = table
CollectionView.tabBarItem = collection
InsertRec.tabBarItem = insert
let controllers = [TableView,CollectionView,InsertRec]
tabbar.viewControllers = controllers
tabbar.viewControllers = controllers.map{UINavigationController(rootViewController: $0)}
//Setting Title
tabbar.navigationItem.title = "Person Record"
return tabbar
}
1
This method is without storyboard
– Muhammad Hashim
Jan 3 at 9:58
1
if you want that method then delete storyboard also from info.plist
– Muhammad Hashim
Jan 3 at 9:59
add a comment |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//// this code changes the initial point of aap///////
window = UIWindow(frame: UIScreen.main.bounds)
let nav = UINavigationController()
let myview = SettingTabbar()
nav.viewControllers = [myview]
window?.rootViewController = nav
window?.makeKeyAndVisible()
return true
}
And Function SettingTabbar is:
func SettingTabbar()->(UITabBarController)
{
//Setting TabBar
let tabbar = UITabBarController()
//Designing Tabbar Item Images
let table = UITabBarItem(title: nil, image:UIImage(named: "002-list") , tag: 0)
let collection = UITabBarItem(title: nil, image: UIImage(named: "001-collect"), tag: 1)
let insert = UITabBarItem(title: nil, image: UIImage(named: "add"), tag: 2)
//Getting TabBar ViewControllers
let TableView = newViewController()
let CollectionView = PersonCollectionViewController()
let InsertRec = nextViewController()
//Setting ViewControllers on TabBar Items
TableView.tabBarItem = table
CollectionView.tabBarItem = collection
InsertRec.tabBarItem = insert
let controllers = [TableView,CollectionView,InsertRec]
tabbar.viewControllers = controllers
tabbar.viewControllers = controllers.map{UINavigationController(rootViewController: $0)}
//Setting Title
tabbar.navigationItem.title = "Person Record"
return tabbar
}
1
This method is without storyboard
– Muhammad Hashim
Jan 3 at 9:58
1
if you want that method then delete storyboard also from info.plist
– Muhammad Hashim
Jan 3 at 9:59
add a comment |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//// this code changes the initial point of aap///////
window = UIWindow(frame: UIScreen.main.bounds)
let nav = UINavigationController()
let myview = SettingTabbar()
nav.viewControllers = [myview]
window?.rootViewController = nav
window?.makeKeyAndVisible()
return true
}
And Function SettingTabbar is:
func SettingTabbar()->(UITabBarController)
{
//Setting TabBar
let tabbar = UITabBarController()
//Designing Tabbar Item Images
let table = UITabBarItem(title: nil, image:UIImage(named: "002-list") , tag: 0)
let collection = UITabBarItem(title: nil, image: UIImage(named: "001-collect"), tag: 1)
let insert = UITabBarItem(title: nil, image: UIImage(named: "add"), tag: 2)
//Getting TabBar ViewControllers
let TableView = newViewController()
let CollectionView = PersonCollectionViewController()
let InsertRec = nextViewController()
//Setting ViewControllers on TabBar Items
TableView.tabBarItem = table
CollectionView.tabBarItem = collection
InsertRec.tabBarItem = insert
let controllers = [TableView,CollectionView,InsertRec]
tabbar.viewControllers = controllers
tabbar.viewControllers = controllers.map{UINavigationController(rootViewController: $0)}
//Setting Title
tabbar.navigationItem.title = "Person Record"
return tabbar
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//// this code changes the initial point of aap///////
window = UIWindow(frame: UIScreen.main.bounds)
let nav = UINavigationController()
let myview = SettingTabbar()
nav.viewControllers = [myview]
window?.rootViewController = nav
window?.makeKeyAndVisible()
return true
}
And Function SettingTabbar is:
func SettingTabbar()->(UITabBarController)
{
//Setting TabBar
let tabbar = UITabBarController()
//Designing Tabbar Item Images
let table = UITabBarItem(title: nil, image:UIImage(named: "002-list") , tag: 0)
let collection = UITabBarItem(title: nil, image: UIImage(named: "001-collect"), tag: 1)
let insert = UITabBarItem(title: nil, image: UIImage(named: "add"), tag: 2)
//Getting TabBar ViewControllers
let TableView = newViewController()
let CollectionView = PersonCollectionViewController()
let InsertRec = nextViewController()
//Setting ViewControllers on TabBar Items
TableView.tabBarItem = table
CollectionView.tabBarItem = collection
InsertRec.tabBarItem = insert
let controllers = [TableView,CollectionView,InsertRec]
tabbar.viewControllers = controllers
tabbar.viewControllers = controllers.map{UINavigationController(rootViewController: $0)}
//Setting Title
tabbar.navigationItem.title = "Person Record"
return tabbar
}
answered Jan 3 at 9:56


Muhammad HashimMuhammad Hashim
34
34
1
This method is without storyboard
– Muhammad Hashim
Jan 3 at 9:58
1
if you want that method then delete storyboard also from info.plist
– Muhammad Hashim
Jan 3 at 9:59
add a comment |
1
This method is without storyboard
– Muhammad Hashim
Jan 3 at 9:58
1
if you want that method then delete storyboard also from info.plist
– Muhammad Hashim
Jan 3 at 9:59
1
1
This method is without storyboard
– Muhammad Hashim
Jan 3 at 9:58
This method is without storyboard
– Muhammad Hashim
Jan 3 at 9:58
1
1
if you want that method then delete storyboard also from info.plist
– Muhammad Hashim
Jan 3 at 9:59
if you want that method then delete storyboard also from info.plist
– Muhammad Hashim
Jan 3 at 9:59
add a comment |
The problem is this line:
window?.rootViewController = HomeTabBarController()
That is the wrong HomeTabBarController. It is a totally new HomeTabBarController with no children. You need to fetch the HomeTabBarController that’s in the storyboard.
But how can I do that. If I set it like thiswindow?.rootViewController = HomeTabBarController
I get this error:Cannot assign value of type 'HomeTabBarController.Type' to type 'UIViewController?'
– Rexhin Hoxha
Jan 3 at 1:44
add a comment |
The problem is this line:
window?.rootViewController = HomeTabBarController()
That is the wrong HomeTabBarController. It is a totally new HomeTabBarController with no children. You need to fetch the HomeTabBarController that’s in the storyboard.
But how can I do that. If I set it like thiswindow?.rootViewController = HomeTabBarController
I get this error:Cannot assign value of type 'HomeTabBarController.Type' to type 'UIViewController?'
– Rexhin Hoxha
Jan 3 at 1:44
add a comment |
The problem is this line:
window?.rootViewController = HomeTabBarController()
That is the wrong HomeTabBarController. It is a totally new HomeTabBarController with no children. You need to fetch the HomeTabBarController that’s in the storyboard.
The problem is this line:
window?.rootViewController = HomeTabBarController()
That is the wrong HomeTabBarController. It is a totally new HomeTabBarController with no children. You need to fetch the HomeTabBarController that’s in the storyboard.
answered Jan 3 at 1:41
mattmatt
334k46545743
334k46545743
But how can I do that. If I set it like thiswindow?.rootViewController = HomeTabBarController
I get this error:Cannot assign value of type 'HomeTabBarController.Type' to type 'UIViewController?'
– Rexhin Hoxha
Jan 3 at 1:44
add a comment |
But how can I do that. If I set it like thiswindow?.rootViewController = HomeTabBarController
I get this error:Cannot assign value of type 'HomeTabBarController.Type' to type 'UIViewController?'
– Rexhin Hoxha
Jan 3 at 1:44
But how can I do that. If I set it like this
window?.rootViewController = HomeTabBarController
I get this error: Cannot assign value of type 'HomeTabBarController.Type' to type 'UIViewController?'
– Rexhin Hoxha
Jan 3 at 1:44
But how can I do that. If I set it like this
window?.rootViewController = HomeTabBarController
I get this error: Cannot assign value of type 'HomeTabBarController.Type' to type 'UIViewController?'
– Rexhin Hoxha
Jan 3 at 1:44
add a comment |
I finally found the solution: As @matt suggested I had to fetch the HomeTabBarController that’s in the storyboard.
window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
// controller identifier sets up in storyboard utilities
// panel (on the right), it called Storyboard ID
let viewController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
window?.makeKeyAndVisible()
window?.rootViewController = viewController
1
You could also just set the HomeTabBarController to be the initial view controller in the story board, and all the above code would be unnecessary.
– Claus Jørgensen
Jan 3 at 2:04
How can I do that? I mean I still need to show the login form if the user session has clicked log out
– Rexhin Hoxha
Jan 3 at 2:05
1
Read up about adding child view controllers (it's a thing, but commonly forgotten by many iOS devs), or just general navigation flow. It's all about how you structure your project, and making a custom window and setting the root VC manually is just going to cause you headaches later on.
– Claus Jørgensen
Jan 3 at 2:07
Do you meanperformSegue()
by 'adding child view controllers'. Because right now what I have found is one usingrootViewController
and the other isperformSegue()
. Could you give me some more information please on which is the best way to show or to hide the login screen depending if the session is active or not.
– Rexhin Hoxha
Jan 3 at 2:15
add a comment |
I finally found the solution: As @matt suggested I had to fetch the HomeTabBarController that’s in the storyboard.
window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
// controller identifier sets up in storyboard utilities
// panel (on the right), it called Storyboard ID
let viewController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
window?.makeKeyAndVisible()
window?.rootViewController = viewController
1
You could also just set the HomeTabBarController to be the initial view controller in the story board, and all the above code would be unnecessary.
– Claus Jørgensen
Jan 3 at 2:04
How can I do that? I mean I still need to show the login form if the user session has clicked log out
– Rexhin Hoxha
Jan 3 at 2:05
1
Read up about adding child view controllers (it's a thing, but commonly forgotten by many iOS devs), or just general navigation flow. It's all about how you structure your project, and making a custom window and setting the root VC manually is just going to cause you headaches later on.
– Claus Jørgensen
Jan 3 at 2:07
Do you meanperformSegue()
by 'adding child view controllers'. Because right now what I have found is one usingrootViewController
and the other isperformSegue()
. Could you give me some more information please on which is the best way to show or to hide the login screen depending if the session is active or not.
– Rexhin Hoxha
Jan 3 at 2:15
add a comment |
I finally found the solution: As @matt suggested I had to fetch the HomeTabBarController that’s in the storyboard.
window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
// controller identifier sets up in storyboard utilities
// panel (on the right), it called Storyboard ID
let viewController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
window?.makeKeyAndVisible()
window?.rootViewController = viewController
I finally found the solution: As @matt suggested I had to fetch the HomeTabBarController that’s in the storyboard.
window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
// controller identifier sets up in storyboard utilities
// panel (on the right), it called Storyboard ID
let viewController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
window?.makeKeyAndVisible()
window?.rootViewController = viewController
answered Jan 3 at 1:56


Rexhin HoxhaRexhin Hoxha
89121947
89121947
1
You could also just set the HomeTabBarController to be the initial view controller in the story board, and all the above code would be unnecessary.
– Claus Jørgensen
Jan 3 at 2:04
How can I do that? I mean I still need to show the login form if the user session has clicked log out
– Rexhin Hoxha
Jan 3 at 2:05
1
Read up about adding child view controllers (it's a thing, but commonly forgotten by many iOS devs), or just general navigation flow. It's all about how you structure your project, and making a custom window and setting the root VC manually is just going to cause you headaches later on.
– Claus Jørgensen
Jan 3 at 2:07
Do you meanperformSegue()
by 'adding child view controllers'. Because right now what I have found is one usingrootViewController
and the other isperformSegue()
. Could you give me some more information please on which is the best way to show or to hide the login screen depending if the session is active or not.
– Rexhin Hoxha
Jan 3 at 2:15
add a comment |
1
You could also just set the HomeTabBarController to be the initial view controller in the story board, and all the above code would be unnecessary.
– Claus Jørgensen
Jan 3 at 2:04
How can I do that? I mean I still need to show the login form if the user session has clicked log out
– Rexhin Hoxha
Jan 3 at 2:05
1
Read up about adding child view controllers (it's a thing, but commonly forgotten by many iOS devs), or just general navigation flow. It's all about how you structure your project, and making a custom window and setting the root VC manually is just going to cause you headaches later on.
– Claus Jørgensen
Jan 3 at 2:07
Do you meanperformSegue()
by 'adding child view controllers'. Because right now what I have found is one usingrootViewController
and the other isperformSegue()
. Could you give me some more information please on which is the best way to show or to hide the login screen depending if the session is active or not.
– Rexhin Hoxha
Jan 3 at 2:15
1
1
You could also just set the HomeTabBarController to be the initial view controller in the story board, and all the above code would be unnecessary.
– Claus Jørgensen
Jan 3 at 2:04
You could also just set the HomeTabBarController to be the initial view controller in the story board, and all the above code would be unnecessary.
– Claus Jørgensen
Jan 3 at 2:04
How can I do that? I mean I still need to show the login form if the user session has clicked log out
– Rexhin Hoxha
Jan 3 at 2:05
How can I do that? I mean I still need to show the login form if the user session has clicked log out
– Rexhin Hoxha
Jan 3 at 2:05
1
1
Read up about adding child view controllers (it's a thing, but commonly forgotten by many iOS devs), or just general navigation flow. It's all about how you structure your project, and making a custom window and setting the root VC manually is just going to cause you headaches later on.
– Claus Jørgensen
Jan 3 at 2:07
Read up about adding child view controllers (it's a thing, but commonly forgotten by many iOS devs), or just general navigation flow. It's all about how you structure your project, and making a custom window and setting the root VC manually is just going to cause you headaches later on.
– Claus Jørgensen
Jan 3 at 2:07
Do you mean
performSegue()
by 'adding child view controllers'. Because right now what I have found is one using rootViewController
and the other is performSegue()
. Could you give me some more information please on which is the best way to show or to hide the login screen depending if the session is active or not.– Rexhin Hoxha
Jan 3 at 2:15
Do you mean
performSegue()
by 'adding child view controllers'. Because right now what I have found is one using rootViewController
and the other is performSegue()
. Could you give me some more information please on which is the best way to show or to hide the login screen depending if the session is active or not.– Rexhin Hoxha
Jan 3 at 2:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015256%2fset-uitabbarcontroller-as-rootviewcontroller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
"My UITabBarController has 3 ViewControllers" Really? I don't see you giving HomeTabBarController any child view controllers. Show the code where that happens. Thanks.
– matt
Jan 3 at 1:31
Hi! I have in my storyboard three relationship segues to the TabBarController. Please check my new screenshot in the updated question.
– Rexhin Hoxha
Jan 3 at 1:35
1
Okay but you are saying
window?.rootViewController = HomeTabBarController()
. That is not the one in the storyboard.– matt
Jan 3 at 1:40
If I set it like this
window?.rootViewController = HomeTabBarController
I get this error:Cannot assign value of type 'HomeTabBarController.Type' to type 'UIViewController?'
– Rexhin Hoxha
Jan 3 at 1:44