Can anyone tell me how to start UINavigationContoller from ÀppDelegate?
I can start arootViewContollerbut cannot start a specificUIViewControllerlike I was trying in commented code.
The commented code starts the **ChooseTableViewController** but does not displayUINavigationBar`.
whats the better approach?
Here is my code
- (void)setRootViewController:(NSString *)storyBoardName {
//set the Root ViewController
UIStoryboard *story = [UIStoryboard storyboardWithName:storyBoardName
bundle:nil];
UINavigationController *newViewController =
[story instantiateInitialViewController];
self.window.rootViewController = newViewController;
/*
ChooseTableViewController *chooseTableViewController =
[story instantiateViewControllerWithIdentifier:#"ChooseTableViewController"];
self.window.rootViewController = chooseTableViewController;
*/
}
Appdelegate.h
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UINavigationController *navigationController;
Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.navigationController = [storyboard instantiateViewControllerWithIdentifier:#"navigation"];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"ChooseTableViewController"];
navigationController=[[UINavigationController alloc]initWithRootViewController:viewController];
self.window.rootViewController =self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
// Your main storyboard
UIStoryboard *story = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
// Your root navigation controller
UINavigationController *newViewController = [story instantiateInitialViewController];
// Your root view controller for root navigation controller
ChooseTableViewController *chooseTableViewController = [story instantiateViewControllerWithIdentifier:#"ChooseTableViewController"];
// Set your view controller as root view controller of your root navigation controller
newViewController.rootViewController = chooseTableViewController;
// set your root navigation controller
self.window.rootViewController = newViewController;
Related
AppDelegate.m
_viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:_viewController];
nav.navigationBar.barStyle = UIBarStyleBlackOpaque;
[_window addSubview:nav.view];
ViewContoller.m
UINavigationController *nav = self.navController;
[nav pushViewController:controller animated:YES];
I don't know why the UINavigationController always nil.
Please help!!
Instead of adding navigationController's view as window's subview try adding navigationController as window's rootViewController
window.rootViewController = nav;
[_window makeKeyAndVisible];
homeViewController = (mainStoryboard.instantiateViewControllerWithIdentifier("register") as? RegisterViewController)!
let navigationController :UINavigationController = UINavigationController()
navigationController.pushViewController(homeViewController, animated: true)
navigationController.navigationBarHidden = false
window?.rootViewController = nil
window?.rootViewController = navigationController
window?.makeKeyWindow()
#interface AppDelegate ()
#property (strong, nonatomic) UINavigationController *navigationController;
#end
//In Your Appdelegate didfinishlaunching method:
self.window = [[UIWindow alloc] init];
[self.window makeKeyAndVisible];
self.navigationController = [[UINavigationController alloc] initWithRootViewController: YourViewController];
self.window.rootViewController = self.navigationController;
//In Your View controller:
[self.navigationController pushViewController:controller animated:YES];
NEW SOLUTION
try this
in AppDelegate.m
#implementation AppDelegate
{}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *_navController = [[UINavigationController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = _navController;
ViewController* _viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[_navController pushViewController:_viewController animated:YES];
return YES;
}
in ViewContoller.m now this will work:
UINavigationController *nav = self.navigationController;
[nav pushViewController:controller animated:YES];
OLD SOLUTION
try this code
in AppDelegate.h be sure to have this at least
#interface AppDelegate : NSObject <UIApplicationDelegate>
{}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
#end
in AppDelegate.m at least this
#implementation AppDelegate
{}
#synthesize window=_window;
#synthesize navController=_navController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *_navController = self.window.rootViewController;
UIViewController* _viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[_navController pushViewController:_viewController animated:YES];
return YES;
}
#end
in storyboard be sure to have created a NavigationController binded to a ViewController, defined as initial view controller and binded as root view controller for the binded view controller
in the app general settings tab be sure to have set the storyboard in the deployment info section (also in this section you could set status bar style)
in ViewContoller.m now this will work:
UINavigationController *nav = self.navigationController;
[nav pushViewController:controller animated:YES];
to edit the navigationBar style you can do it this way
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
}
i am importing MFSideMenu to my dummy project. i am using the following codes
.h file
#import <UIKit/UIKit.h>
#import "MFSideMenu.h"
#import "RightSideViewController.h"
#import "MFSideMenuContainerViewController.h"
#interface xyzAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property(strong,nonatomic)UINavigationController * navigationController;
#property(strong, nonatomic) RightSideViewController * rightViewController;
#property(strong, nonatomic) MFSideMenuContainerViewController * container;
and .m file is
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window= [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UIStoryboard * mainstoryboard = [UIStoryboard storyboardWithName:#"main" bundle:nil];
xyzViewController * vc = [mainstoryboard instantiateViewControllerWithIdentifier:#"xyzViewController"];
[self.navigationController pushViewController:vc animated:YES];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:vc];
self.rightViewController = [[RightSideViewController alloc]init];
self.container = [MFSideMenuContainerViewController containerWithCenterViewController:self.navigationController leftMenuViewController:nil rightMenuViewController:_rightViewController];
self.window.rootViewController = _container;
[self.window makeKeyAndVisible];
return YES;
}
the problem arise when i run the project it shows black screen only nothing else on simulator.
please help me to solve this issue
I solve it by myself Thanks #pkc456
i added the following code to my .m file and now it works fine
self.window= [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UIStoryboard * mainstoryboard = [UIStoryboard storyboardWithName:#"main" bundle:nil];
xyzViewController * vc = [mainstoryboard instantiateViewControllerWithIdentifier:#"xyzViewController"];
[self.navigationController pushViewController:vc animated:YES];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:vc];
I am developing iOS app in which i have side menu with TabBar,
I have settled My sidemenu container viewController as IntitalViewController of my app.
Here is my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;
UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:#"navigationController"];
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"leftSideMenuViewController"];
[container setLeftMenuViewController:leftSideMenuViewController];
[container setCenterViewController:navigationController];
return YES;
}
Here is the Design of my Storybaord:
When i run my app i see a black screen with no error
Where i am making mistake, please help
Thanks in adavnnce !!
Give try to this (after seeing your code):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
self.window.rootViewController=[[MFSideMenuContainerViewController alloc]init];
// Override point for customization after application launch.
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;
UITabBarController *tabController = [storyboard instantiateViewControllerWithIdentifier:#"tabBarController"];
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"leftSideMenuViewController"];
[container setLeftMenuViewController:leftSideMenuViewController];
[container setCenterViewController:tabController];
[self.window makeKeyAndVisible];
return YES;
}
Also give the respective id's to view controllers in storyboard ( for tabBar, navigation)
and don't forget to give root view controller for navigation
controller.
I am calling SWRevealViewController in Appdelegate.m
SWRevealViewController *svc = self.revealViewController;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
FrontVC *homeVC = [storyboard instantiateViewControllerWithIdentifier:#"FrontVC"];
[svc setFrontViewController:homeVC animated:NO];
[svc revealToggleAnimated:YES];
But i am not able to load FrontVC controlller .
what is right way to call
For Swift 2.2
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let sw = storyboard.instantiateViewControllerWithIdentifier("SWRevealViewController") as! SWRevealViewController
self.window?.rootViewController = sw
let cv = storyboard.instantiateViewControllerWithIdentifier("IDOFCONTROLLER") as! NAMEOFCONTROLLER
let navigationController = UINavigationController(rootViewController: cv)
sw.pushFrontViewController(navigationController, animated: true)
I hope this one helps.
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
// this any item in list you want navigate to
yourHomeViewController *home = (yourHomeViewController *) [storyboard instantiateViewControllerWithIdentifier:#"leid"];
yourSideMenuViewController *slidemenu = (yourSideMenuViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MenuView"];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:home];
UINavigationController *smVC = [[UINavigationController alloc]initWithRootViewController:slidemenu];
// define rear and frontviewcontroller
SWRevealViewController *revealController = [[SWRevealViewController alloc]initWithRearViewController:smVC frontViewController:nav];
// make it as root
self.window.rootViewController = revealController;
We can't call RevealViewController in App Delegate.
You can directly add this in Your ViewController for Front & rear.
Have you declared and initialized revealViewController anything like this?
#interface AppDelegate ()
#property (strong, nonatomic) SWRevealViewController *revealViewController;
#end
/* ... */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.revealViewController = /* ... init ... */;
/* ... your code here ... */
}
I didn't use a storyboard but did it like this:
SWRevealViewController *revealController = [[SWRevealViewController alloc] initWithRearViewController:menuViewController frontViewController:frontNavigationController];
SWRevealViewController *viewController = revealController;
I think u can put the start indicator in the SWRevealViewController in ur storyboard, give the login VC a name
//if user is logged in - In login VC
[[NSUserDefaults standardUserDefaults] setValue:1 forKey:isLogged];
//In Appdelegate
if([[NSUserDefaults standardUserDefaults] valueForKey:isLogged] == 1)
{
NSLog(#"have data");
//Start straight into normal SWRevealVC
self.window.rootViewController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
}
else
{
//If not, make your root VC the login viewcontroller
UIViewController* rootController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"LoginViewController"];
self.window.rootViewController = rootController;
}
I have a custom URL scheme and i want to open a certain ViewController which is not the root when i go to this URL. I have been able to do that and what remains is pushing this ViewController into the navigationController from the AppDelegate where i handle my URL like this :
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
if ([[url scheme] isEqualToString:#"njoftime"]) {
NSDictionary *getListingResponse = [[NSDictionary alloc]init];
getListingResponse = [Utils getListing:[url query]];;
if ([[getListingResponse objectForKey:#"status"] isEqualToString:#"success"]) {
ListingViewController *listingView = [[ListingViewController alloc]init];
[self.window.rootViewController.navigationController pushViewController:listingView animated:YES];
return YES;
}
but it only launches my app and not the ListingViewController i want to launch.
Any idea how can i do it differently ?
Issue
To deal with pushing and popping the viewControllers from AppDelegate, you need to use [UIApplication sharedApplication] which keeps track to all of viewControllers, beginning with root one.
Solution
To PUSH ViewController from AppDelegate
ListingViewController *listingVC = [[ListingViewController alloc] init];
[(UINavigationController *)self.window.rootViewController pushViewController:listingVC animated:YES];
To POP that ViewController you just presented, you need to use this code
[(UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController popViewControllerAnimated:YES ];
If your using storyboard then you could use below lines for pushing your VC.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
YOURCLASS *obj=[storyboard instantiateViewControllerWithIdentifier:#"YOUR_CLASS_STORYBOARD_ID"];
[self.window.rootViewController.navigationController pushViewController:obj animated:YES];
Update:-
ListingViewController *listingVC = [[ListingViewController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:listingVC];
[self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
Write this code in didFinishingWithLaunchingOptions
SecViewController *Vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil]instantiateViewControllerWithIdentifier:#"second"];
[(UINavigationController *)self.window.rootViewController pushViewController:Vc animated:YES];
For Swift 3 version:
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UINavigationController = mainStoryboardIpad.instantiateViewController(withIdentifier: "initial") as! UINavigationController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
If you want to push a UIViewController, you probably are forgetting to init it with the NibName:
LoginViewController *loginViewController = [[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:nil];
Your AppDelegate.m should look like this:
#import "TestViewController.h"
#interface AppDelegate ()
#property (strong, nonatomic) TestViewController *viewController;
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[TestViewController alloc]init];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}