my app starts either to a login view or to the main menu, depending whether the user logged in in a previous session. That's why I define the "initial view controller" programmatically.
The login scene is the root view controller of the navigation controller and from there the user has the option to register or login.
The problem is, that if use the following code, the login view is in fact shown, but the navigation bar is missing... Any ideas what I am doing wrong? All works fine if I declare the login scene in Interface-Builder as the "initial view controller", but I do need to show the view programmatically.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"LoginScene"];
}
If the LoginScene is the root view controller of the navigation controller, then you should be setting the nav controller as the window's root if I'm understanding correctly:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"LoginNavController"];
}
Related
I have one view controller with out navigation controller. Its name is LoginViewController. In my AppDelegate, I want to keep my LoginViewController as root view controller.
How can I do this in Objective-C? How can I set my view controller as root view controller?
Note: My view controller does not have navigation view controller. It's a single view controller.
do like
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1. get the Storyboard Name
UIStoryboard* main = [UIStoryboard storyboardWithName:#"Main"
bundle:[NSBundle mainBundle]];
//2. get the ViewController using Storyboard ID
UIViewController *viewConr = [main instantiateViewControllerWithIdentifier:#"HomeViewController"];
// 3.finally assign the Root
self.window.rootViewController = viewConr;
[self.window makeKeyAndVisible];
return YES;
}
for E.g
Without storyboard:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
LoginViewcontroller *Vc = [[LoginViewcontroller alloc]init];
self.window.rootViewController = Vc;
[self.window makeKeyAndVisible];
return YES;
}
If using storyboard just make that view controller as initial view controller from storyboard.
If you want to set home page VC as root vc from login page VC
Import appdelegate in login page
#import "AppDelegate.h"//Import in Login page VC
self.delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; // In viewDidLoad
Write below code where you navigate
//Make root view controller
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
HomeViewController * hvc = [mainStoryBoard instantiateViewControllerWithIdentifier:#"Home"];//Your Home page story board ID
self.delegate.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:hvc];
[self.delegate.window makeKeyAndVisible];
So the problem is--and this is new, this used to work in iOS 7-- when I comment out "FinishedLaunching", it rotates just fine but when I override this "FinishedLaunching" function to create the "window" myself, it fails to rotate. This is really strange. Any thoughts?
The reason I want to use FinishedLunching is because sometimes I want a different viewController to be the initial view controller.
Here is the code in my "FinishedLunching"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
The problem is that you are creating a new window with
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
when the AppDelegate one that it automatically sets up for you that is perfectly fine to use, just use self.window without alloc-initing a new one
I'm following the advice of this question in order to define my initial view controller at runtime, instead of always being the same based off of the Storyboard property.
This is my didFinishLaunching method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *compactNavigationController = [mainStoryboard instantiateViewControllerWithIdentifier:#"CompactNavigationController"];
self.window.rootViewController = compactNavigationController;
[self.window makeKeyAndVisible];
CompactPostsViewController *controller = (CompactPostsViewController *)compactNavigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
return YES;
}
And I've unchecked "Is Initial View Controller" in my Storyboard, and I've removed "Main" from the default Storyboard in my app's info.
But when I launch the app I get a completely black screen. What am I doing wrong?
Your window is nil because you never created it. You need to add this line,
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
The window is created for you when you have the "Main storyboard base name" entry in your info.plist, but if you remove that, then you have to create the window yourself.
check your linkings in your storyboard(by right click on the tableview controller).This happens mostly when you not linked your tableview controller properly.
My app consists of a navigation controller and view controllers. Some parts of my UI is done on storyboard and some are initiated from code (in viewDidLoad). My goal is to load childViewController X (consists of a back button, nag bar, label and table) when the app is launched from a push notification "properly" through this method in appDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
This might be a simple question to you guys but I've asked several questions the pass few days (you can look at my questions history). After trying several methods, my app either:
crashes
loads without navigation bar
loads with navigation bar but no label (back button does not work)
loads with navigation bar but with black screen underneath
Only the tableView is dragged onto storyboard. The navigation bar is inferred but I have code that dictates its back button and title. The rest is all done through code in the .m file.
I know people have asked this question before but none of the methods worked. How can I load this childViewController properly through a push notification?
EDIT/UPDATE:
My code so far:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController* firstVC = [mainstoryboard instantiateViewControllerWithIdentifier:#"NotificationsViewController"];
[self.window.rootViewController addChildViewController:firstVC];
[(UINavigationController *)self.window.rootViewController pushViewController:firstVC animated:NO];
self.window.rootViewController.childViewControllers);
}}
Error Code:
'-[SWRevealViewController pushViewController:animated:]: unrecognized selector sent to instance 0x14575f20'
SWRevealViewController is my library for my sidebar menu view controller.
UPDATE2:
I've also tried this method:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:#"NotificationsViewController"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = initViewController;
[self.window makeKeyAndVisible];
This loads the correct viewController but without a navigation bar.
Try this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(dictionary != nil) {
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *firstVC = [mainstoryboard instantiateViewControllerWithIdentifier:#"NotificationsViewController"];
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
// Navigation already created
if(navigationController) {
[navigationController pushViewController:firstVC animated:NO];
// Navigation not created
} else {
UINavigationViewController *navigationController = [[UINavigationViewController alloc] initWithRootController:firstVC];
[self.window setRootViewController:firstVC];
[self.window makeKeyAndVisible];
}
}
return YES;
}
You had some problem in your code:
You need to have a UINavigationController in order to have a navigation bar and manage the push/pop actions, so your window root controller should be a UINavigationController.
If you need to initialize a UINavigationController the best and simple way is to initialize it with a root controller directly, so leave the push/pop for user actions: an application has always a root controller, so why don't you create it immediately?
The "addChildController" is another thing: it is used to create custom container controller, it means that you have to implement all the business logic manually. I suggest you to use it only if you are experienced enough, since it could be difficult - Cocoa has enough components to leave it for a small set of applications.
You are using a third part controller, the "SWRevealViewController". They should have an example project, you can try to "copy" it and them customize it for your own purpose.
Let me know if your code works well, otherwise post the new error and I'll try to help you
The goal:
When my app starts up - I need it to display a view before it gets to the "Home" screen. Its a tab bar application and this view is not part of the tabbar.
I am using Storyboards and Xcode 5 - iOS7 only app.
The problem:
I have code that will check if the app is first launch or not. Based on that, I then want to present a one time only view to the user.
What I have tried:
The following code is in the appDelegate of the application as this is where it all starts. I call the following bit of code in there:
-(void)showCountrySettings
{
if (self.termsHaveBeenAccepted){
BBCounterySettingsViewController *countrySettings = [[BBCounterySettingsViewController alloc]initWithNibName:#"View" bundle:nil];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER"];
[self.navigationController pushViewController:vc animated:YES];
}
I get compile errors as [self.navigationController..] doesn't exist. Nor does [self.tabbarcontroller...];
This is obvious as I don't have properties setup for these - but how do I go about resolving this and connecting the tab bar to the storyboard?
What am I missing?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if(!isAgreementAccepted)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"IDD"];
self.window.rootViewController=vc;
}
return YES;
}
If agreement is not accepted set the T&C viewController as rootViewController when user click the accept button then set the TabBarviewController as root.
u can access the widow object through application delegate any where
[[[UIApplication sharedApplication]delegate] window].rootViewController=tabViewController.
Change the rootviewcontroller of window programaticaly
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *aStoryBoard=[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
UITabBarController *aTabCtrl=[aStoryBoard instantiateViewControllerWithIdentifier:#"Tab"];
FirstVC *aFirstCtrl=[aStoryBoard instantiateViewControllerWithIdentifier:#"First"];
if(self.termsHaveBeenAccepted)
self.window.rootViewController=aFirstCtrl;
else
self.window.rootViewController=aTabCtrl;
return YES;
}
This will definitely work I have tested.
If Tabbarcontroller is your root viewcontroller, then using below code will resolve the issue in your appdelegate.
-(void)showCountrySettings
{
if (self.termsHaveBeenAccepted){
BBCounterySettingsViewController *countrySettings = [[BBCounterySettingsViewController alloc]initWithNibName:#"View" bundle:nil];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER"];
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
[tabBarController presentModalviewcontroller:vc animated:YES completionblock:nil];
}
add your viewcontroller superview of tabbarcontroller as first time else call tabbarcontroller alone