Initially I developed the app with only one ViewController(called MainView). Now I'd like to add one ViewController (called LoginView)infront of MainView. I added in the AppDelegate as
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LoginPage *RootViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:#"LoginPage"];
[UIApplication sharedApplication].delegate.window.rootViewController = RootViewController;
[[UIApplication sharedApplication].delegate.window makeKeyAndVisible];
return YES;
}
It looks working, but only black screen appears.
My LoginPage ViewController has the following structure. The ViewController has two buttons, one label and one text view. They don't appear and only black screen is appearer.
Just mark your Login view controller as initial view controller in storyboard
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];
I am writing program with MainStoryBoard like this. I have UITabBarController with 5 tabs. In viewdidload of each uiviewcontroller, I set like this. But, it will only do if I go to that page. So, I am planning to do in my AppDelegate for all tabs. How shall I do programatically? I can set text in UIStoryBoard but I need to set programmatically.
self.title = #"Some title";
For this you should get the storyboard instance first. And then get tabbar from story board. Do this in the following method of AppDelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
UITabBarController *tabBar = [storyBoard instantiateInitialViewController] ;
NSLog(#"%#",tabBar.viewControllers);
// You will get list of your viewcontrollers added to tabbar .Get the instances of those view controllers and set title to them here.
return YES;
}
I have two view controllers, a default ViewController and the one I added later for login: LoginViewController. I set LoginViewController as initial view. But when I run the app, after viewDidLoad in LoginViewController it fires -viewDidLoad in ViewController also.
I have a modal segue, after login is successful to perform segue and show me the ViewController, that works. But in execution of app ViewController is also fired. Does anyone have an idea why this happens? AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UIStoryboard *storyboard = self.window.rootViewController.storyboard;
UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"LoginView"];
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
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
I understand this question is many times on STackOverflow and also many on internet too. I have also asked question related to be same, but the context is different, so please bear with me as I am not able to find the solution for this task.
I am using Xcode 5, using Storyboard. MY project is Tab based and have added a LoginViewController in the storyboard. My win is to show LoginViewController prior to Tabs. I tried several ways from various sites, but nothing works fully. With this code, I could get the LoginViewController at first, but not able to get Tabs controller. In my AppDelegate :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
self.tabBarController = [[MC_MainTabBarController alloc] init];
LoginViewController *loginViewController = [storyboard instantiateViewControllerWithIdentifier:#"loginViewController"];
[self.window setRootViewController:loginViewController];
// This also works - it shows the login
//[[[[UIApplication sharedApplication] delegate] window] setRootViewController:loginViewController];
// With this Login doesn't come only
//[loginViewController setModalPresentationStyle:UIModalPresentationFullScreen];
//[tabCtrler presentViewController:loginViewController animated:NO completion:nil];
return YES;
}
In my LoginViewController.m - on loginBtnClicked event in end, I call :
NSLog(#"Showing TabController");
MC_AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate.window setRootViewController:appDelegate.tabBarController];
On execution of the above lines, I get black screen and following warning :
Showing TabController
Two-stage rotation animation is deprecated. This application should use the smoother single-stage animation.
Two-stage rotation animation is deprecated. This application should use the smoother single-stage animation.
Can anyone please help me out. I am stuck on this problem from last 2 days.
Any help is highly appreciated.
Are you sure MC_MainTabBarController is a subClass of UITabBarController? (I guess it is)
because this error can appear when the TabBar is not the rootViewController
Instead of [[MC_MainTabBarController alloc] init]; i would allocated the tab bar controller from storyboard [storyboard instantiateViewControllerWithIdentifier:#"myTabBarIdentifier"];
I try and it worked for me :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
_tabController = [storyboard instantiateViewControllerWithIdentifier:#"tabbcontrolllerid"];
LoginViewController *login = [[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:nil];
[_window setRootViewController:login];
return YES;
}
And like you in the button of login
- (IBAction)goToApp:(id)sender {
stackAppDelegate *app = [UIApplication sharedApplication].delegate;
[app.window setRootViewController:app.tabController];
}
For info the tab bar is a custom TabBarController which is a subclass of UITabBarController
And i have set in the storyboard the identifier of my custom tabbar.