I use swrevealviewcontroller to add slide menu to my app ,
Let consider we have menu like this one in pic
I need navigate from My appDelegate to any item in menu (ex:Map View Controller )
my tries :
in my appDelegate.m
UIStoryboard *storyboard =[UIStoryboard storyboardWithName:#"Main" bundle:nil];
InforView *school_view = [storyboard instantiateViewControllerWithIdentifier:#"info_view"];
[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:school_view animated:YES completion:NULL];
When its move to InforView Controller it crash in viewdidload
- (void)viewDidLoad
{
[super viewDidLoad];
_nav_bar.target = self.revealViewController;
_nav_bar.action = #selector(revealToggle:);
// its crash here
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}
my storyboard
navigation controller --> home view --> reveal View controller
reveal View controller has two views --> slide menu
navigation controller -- > Front view
my slide menu has some items as appear in pic
I need navigate from my appDelegate to one of this items
Finally I find the answer
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
// this any item in list you want navigate to
Home_tableView *home = (Home_tableView *) [storyboard instantiateViewControllerWithIdentifier:#"home_view"];
SlideMenu *slidemenu = (SlideMenu *)[storyboard instantiateViewControllerWithIdentifier:#"Menu_view"];
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;
In the SidebarDemo project, you can use the following code to show MapViewController on initial loading.
SWRevealViewController *revealViewController = (SWRevealViewController*)self.window.rootViewController;
UIStoryboard *storyboard =[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
MapViewController *mapVC = [storyboard instantiateViewControllerWithIdentifier:#"MapID"];
[self.window makeKeyAndVisible];
UINavigationController* navController = (UINavigationController*)revealViewController.frontViewController;
[navController setViewControllers: #[mapVC] animated: NO ];
[revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
After makeKeyAndVisible, the frontViewController and realViewController of the revealViewController are loaded. And you can set rootViewController of frontViewController, which is a navigationController.
Related
I'm using a Tabbarcontroller and I want a side menu in the first tab of the tab bar. For the side menu I'm using MMDrawerController.
I'm using storyboard
How should I proceed ?
Following code is working for me, I have written this code in viewDidload of LoginScreen, In Storyboard i have created LoginScreen as rootviewController with embeded navigationController (means navigationController is storyboard entry point and LoginScreen is rootViewController).
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vcSideMenu = [storyBoard instantiateViewControllerWithIdentifier:#"SideMenuScreen"];
UITabBarController *tabBar = [storyBoard instantiateViewControllerWithIdentifier:#"tabBar"];
MMDrawerController *controller = [[MMDrawerController alloc] initWithCenterViewController:tabBar leftDrawerViewController:vcSideMenu rightDrawerViewController:nil];
CGFloat menuWidth = [UIScreen mainScreen].bounds.size.width * 0.8;
[controller setMaximumLeftDrawerWidth:menuWidth];
[controller setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
[controller setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
[self.navigationController pushViewController:controller animated:NO];
I have a program with a Navigation Controller and a default RootViewController. If I do nothing programmatically, the app launches and the RootViewController works as I expect, such as what the storyboard below denotes:
The issue that I have is by incorporating the Optional Start ViewController. What I want is this: In my AppDelegate (didFinishLaunchingWithOptions), I want to have code like this:
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"OptionalStartViewController"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
To first show the Optional Start ViewController. Then, after the user has finished with the Optional viewcontroller, they can then display the RootViewController.
So in the Optional Start ViewController, I've added code like this to show the Root View Controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"RootViewController"];
[self appDelegate].window.rootViewController = viewController;
[[self appDelegate].window makeKeyAndVisible];
This all works except the RootViewController, when shown, does not have the navigation controls as expected (i.e. the view is shown without navigation controls).
I've also tried the code below (using UINavigationController instead of ViewController) with the same results...
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UINavigationController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
[self appDelegate].window.rootViewController = viewController;
[[self appDelegate].window makeKeyAndVisible];
One other twist... there could be several optional start view controllers.
Any ideas?
Delete the UINavigationController
select the controller ("optional start view controller" in our case)
click Editor >> Embed In >> Navigation Controller
Now select the Navigation Controller and Right Side Utility area, Select the option Is Initial View Controller
If you want to change the root view controller dynamically then its better to do it programmatically, in didFinishLaunchingWithOptions, Get the window instance , initialize navigation controller with the root view controller and then set windows root view controller to navigation controller.
Here is the code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
OptionalFirstViewController *optionalFirstViewController = [[OptionalFirstViewController alloc] initWithNibName:#"OptionalFirstViewController" bundle:nil];
UINavigationController *homeNavigationController = [[UINavigationController alloc] initWithRootViewController:optionalFirstViewController];
[optionalFirstViewController release];
self.window.rootViewController = homeNavigationController;
[homeNavigationController release];
[self.window makeKeyAndVisible];
return YES;
}
and make sure that you uncheck Is Initial View Controller for all view controllers in story board
Hope this helps
Without using storyboard we can set programmatically in AppDelegate Class, just write down these lines of code in AppDelegate.
AppDelegate.h
#property (strong, nonatomic) UIStoryboard *storyboard;
#property (strong, nonatomic) UINavigationController *navigationController;
AppDelegate.m
if(self.window == nil)
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
if(self.navigationController == nil)
self.navigationController = [[UINavigationController alloc]init];
if(self.storyboard == nil)
self.storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.navigationController.navigationBarHidden = YES;
// Here we can check user is login or not also,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
[self.navigationController pushViewController:ivc animated:YES];
self.window.rootViewController = ivc;
[self.window setRootViewController:self.navigationController];
[self.window makeKeyAndVisible];
In Swift Version
var window: UIWindow?
var storyBoard :UIStoryboard?
var navigationController : UINavigationController?
var mainController :MainViewController = MainViewController()
if window == nil {
window = UIWindow(frame: UIScreen.main.bounds)
}
if navigationController == nil {
navigationController = UINavigationController()
}
if storyBoard == nil {
storyBoard = UIStoryboard(name: "Main", bundle:nil)
}
navigationController?.setNavigationBarHidden(true, animated: true)
// storyboard with identifer
mainController = storyBoard?.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
navigationController?.pushViewController(mainController , animated: true)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
You can't set a new root view controller to UINavigationController after it is initialised. You should modify viewControllers array of UINavigationController according to your needs.
If you want the optional VC to be root VC, create #[optionalVC] and set it as viewControllers array of UINavigationController.
In my AppDelegates 'didFinishLaunchingWithOptions' function, I have this code in there:
if(loggedIn != nil)
{
MainViewController *mvc = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"MainView"];
[self.window setRootViewController:mvc];
}
Second Attempt which didn't work:
if(loggedIn != nil)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MainViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"MainView"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];
}
The problem is that when the MainViewController loads, the NavigationHeader is missing. I've tried various methods online and instantiations that basically do the same thing to no avail. I have also tried created a whole new navigationController and adding my view to it, however, that fails as well.
Your setting MainViewController as your root, if this is not a navigation controller, there will be no header when it opens.
Instead create a UINavigationController, set MainViewController as its root and then set the navigation controller as the window root.
e.g.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboardName" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"home"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];
or if you have the navigation controller inside the storyboard then instantiate that. Most likely the initial view controller.
e.g.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboardName" bundle:nil];
[self.window setRootViewController::[storyboard instantiateInitialViewController]];
not sure if it will be the initial viewController or not, that requires more info of your setup to know.
I have a sidebar menu, made with SWRevealViewController.
One of menu elements in sidebar is "sign-in". It opens sign-in view (via custom SWRevealViewController segue).
I want to programmatically switch to another view after user successfully signs in. How do I do that?
UPDATE, my code:
ViewController *vc = [[ViewController alloc] init]; // this is main front ViewController
SWRevealViewController *revealController = self.revealViewController;
[revealController setFrontViewController:vc animated:YES];
After this I get just empty screen. What am I doing wrong? I feel that this is wrong:
ViewController *vc = [[ViewController alloc] init];
so what is correct way to access already loaded view controller? (it loads at app start by SWRevealViewController as front controller)
Try this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SecondViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navController setViewControllers: #[rootViewController] animated: YES];
[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
work for me :-)
Use
- (void)setFrontViewController:(UIViewController *)frontViewController animated:(BOOL)animated; method
SWRevealController has it's own property you can access from any UIViewController:
SWRevealViewController *revealController = self.revealViewController;
You can check example projects at it's GitHub repository:
https://github.com/John-Lluch/SWRevealViewController
Here is a Swift version of the setFrontViewController solution:
if let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewControllerStoryBoardID") as? SecondViewController {
let navController = UINavigationController(rootViewController: secondViewController)
navController.setViewControllers([secondViewController], animated:true)
self.revealViewController().setFrontViewController(navController, animated: true)
}
Try this:
ViewController *vc = [[ViewController alloc] init]; // this is main front ViewController
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
[navController setViewControllers: #[vc] animated: YES];
// [navController pushViewController:vc animated:YES]; // use this if you want to stack the views
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
There is a new API - (void)pushFrontViewController:(UIViewController *)frontViewController animated:(BOOL)animated;
As far as I know, this is the most elegant way that animates correctly to switch between view controllers manually.
// get the view controller you want to push to properly.
// in this case, I get it from Main.storyboard
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController * vc = [sb instantiateViewControllerWithIdentifier:#"SignInViewController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
[navController setViewControllers: #[vc] animated:NO];
[self.revealViewController pushFrontViewController:navController animated:YES];
If you are in the target vc that doesn't import SWRevealViewController and want to push back:
// get your vc properly
[self.navigationController setViewControllers:#[vc] animated:YES];
Please notice that [self.navigationController showViewController:vc sender:self]; has the same result, but don't use this since this creates a new view controller on the navigation stack, which is basically meaningless while using SWRevealViewController.
This one works for me perfectly
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
UINavigationController *navController = [storyboard instantiateViewControllerWithIdentifier:#"NavController"];
[navController setViewControllers: #[rootViewController] animated: YES];
[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
#Evana has a good answer, however for my case which passing parameter to the destViewController, I need to improve it as below. By following her example exactly, I noticed the DestViewController viewDidLoad was called twice. First with 0 on the 'selectedId', only the second call did the 'selectedId' is receiving my tag value.
Thus, in order for a cleaner navigation, the DestViewController must be obtained from the navController, so that there is no redundant call.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *navController = [storyboard instantiateViewControllerWithIdentifier:#"DestNavController"];
DestViewController *destViewController = (DestViewController*)[[navController viewControllers] objectAtIndex:0];
destViewController.selectedId = ((UIButton*)sender).tag;
[self.revealViewController setFrontViewController:navController];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self. window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UINavigationController *navVC = [UINavigationController new];
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:#"your storyboard name" bundle:nil];
ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:#"VC"];
[navVC setViewControllers:[NSArray arrayWithObject:VC1] animated:NO];
[self. window setRootViewController:navVC];
[self. window makeKeyAndVisible];
return YES;
}
Now this creates a navigationController but when i try to display the secondViewController it does display me the correct Navigation Bar for that view but i see a black background instead of the actual view. Adding secondViewController as a subview works but when i now try to display the third one it does again display me the correct navigation bar with the set title for this view but i still see the secondViewController's view. Now my question how do I add viewController to the navigationController so they are displayed right? do i even have to add them? I've read through the apple class reference but there isn't any code.
try this:
self. window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:#"your storyboard name" bundle:nil];
ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:#"VC"];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:VC1];
[self. window setRootViewController:navVC];
[self. window makeKeyAndVisible];
return YES;
now inside VC1 push the next view controller to your stack with
// EDIT:
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:#"your storyboard name" bundle:nil];
ViewController *vC2 = [mStoryboard instantiateViewControllerWithIdentifier:#"VC2"];
[self.navigationController pushViewController:vC2 animated:YES];