Change rootViewController from uiviewcontroller to uinavigationcontroller - ios

I am developing an app that use MFsidemenu for the swipeBar.
My app consists of a loginController which is an uiViewController and after logging it change into an uiNavigationController.
MFsidemenu need this code running on the AppDelegate:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:#"leftSideMenuViewController"];
[MFSideMenu menuWithNavigationController:navigationController
leftSideMenuController:leftSideMenuViewController
rightSideMenuController:nil];
The problem is how to change the second line of the code if the initialView is an uiViewController(Login) and after that, how to pass it to the secondViewController(MainMenu) which is an uiNavigationController?
In a shortway, I want the MFSidemenu to work only on the secondController, which is the main controller. Thanks!
Updated:
solved, by SpaceDust solution below :)
Question Update:
So the example of using MFSidemenu only limited to showing a sidemenu on the controller. The example sideMenuController exiled from any other segue on the storyboard. on the sidemenu I implemented uiTableViewController to navigate to another controller. So how to change MainMenuView with sideMenuController didSelectRowAtIndexPath? I hope my english good enough to represent my situation though. Thanks once again!

You need to present your uiViewController(Login) as a modal view controller if you dont want to side menu appear during login.
Create a login viewcontroller in storyboard, give it a storyboard id lets say LoginViewController . Dont connect that viewcontroller to anything on storyboard let it sit a corner by itself.
first create a global singleton variable where you check if user is logged in
then In your navigationcontroller's rootviewcontroller
-(void)viewWillAppear:(BOOL)animated
{
//this will present the login view if user is not logged in
if (isLoggedIn==NO) {
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle:nil];
LoginViewController *loginVC = [sb instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self presentModalViewController:loginVC animated:YES];
}
}
in your login viewcontroller when login process is complete
change global singleton bool to isLoggedIn=YES
then dismiss your login view controller.
[self dismissViewControllerAnimated:NO];

Related

IOS/Objective-C/Storyboard: Prevent ViewController From Launching Modally

I want a viewcontroller to launch using a show transition, not modally from the bottom. Normally when I use the following code that's what happens. However, in this case, it is launching as a modal controller from the bottom up. Is there a switch I don't know about or could something be set in Storyboard that is causing this VC to launch modally from the bottom instead of showing?
UIStoryboard *storyBoard = self.storyboard;
IDImportEventsOnboard *importEvents =
[storyBoard instantiateViewControllerWithIdentifier:#"importEventsOnboard"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: importEvents];
[self presentViewController:nav animated:YES completion: nil];
The VC is embedded in a navigation controller.
Should I be using showViewController directly to the targetVC without going through the Nav? Or a pushViewController What is a proper, robust way to show a VC with a show transition?
Thanks in advance for any suggestions.
In the above code you are 'presenting' a new NavigationController from a ViewController. In order to do a push/show transition, that needs to be done on an instance of a NavigationController. If your current ViewController is already in a NavigationController, you can push the new ViewController onto the current NavigationController stack. For Example:
UIStoryboard *storyBoard = self.storyboard;
IDImportEventsOnboard *importEventsVC =
[storyBoard instantiateViewControllerWithIdentifier:#"importEventsOnboard"];
[self.navigationController pushViewController:importEventsVC animated:YES];

navigating to viewcontroller inside tabbar issue - ios

I have one tabviewcontroller and associated tab bars and view controllers as shown in the image.
My problem is, when i am trying to navigate to some inner view controllers using below code it is not even working, i have tried some other code also, some codes are navigating properly but Tabbar is missing.
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabbar = (UITabBarController *)[storyBoard instantiateViewControllerWithIdentifier:#"eventList"];
self.window.rootViewController = tabbar;
[self.window.rootViewController.navigationController pushViewController:tabbar animated:YES];
i am not that much familiar with tabviewcontroller.
Change,
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabbar = (UITabBarController *)[storyBoard instantiateViewControllerWithIdentifier:#"eventList"];
self.window.rootViewController = tabbar;
[self.window.rootViewController.navigationController pushViewController:tabbar animated:YES];
With,
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NavigationControllerOfTabbar *navVC = (NavigationControllerOfTabbar *)[storyBoard instantiateViewControllerWithIdentifier:#"navVCStoryboardID"];
self.window.rootViewController = navVC;
Problem,
You need to learn know how UITabBarController works: Read this.
You need to learn how iOS app flow/stack works, specifically for UINavigationController, UITabBarController.
In your code, this line would be nil: self.window.rootViewController.navigationController
Solution,
Create new class subclassing UINavigationController, and give first view controller's name in your storyboard.
Now initialize this with given code, and it will work.
Tab bar didn't require navigation controller to operate.
My suggestion is to remove all code give as above, and make sure that your storyboard is main interface in your target. Tabs bar should work automatically.
I've noticed that you have other tab bar controllers as children of main tab bar controller. Perhaps they are rudiments of your learning process and had to be removed.
Please make sure that you are setting up proper segues
https://imgur.com/a/WvIrh

iOS: Call segue from coded UIViewController outside of Storyboard

I have a custom UIVieController class that it's not included in any StoryBoard, I need to call from this class an UIViewController that is included in a StoryBoard, I tried many ways to do this but it doesn't work, always get a NSException because when I try to instance the UIViewController of the StoryBoard and get a nil object.
I don't know why it's happening this, I really appreciate any help with this.
Thank you.
Just to clarify in this code I omitted the pushing of the view to the UINavigationController.
I tried this.
+(void)loadSocialMediaAppSegue:(AXISAppDelegate*)delegate withName:(NSString*)segueId{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
AXISWebViewViewController *axisWebView = [storyboard instantiateViewControllerWithIdentifier:segueId];
}
and this
+(void)loadSocialMediaAppSegue:(AXISAppDelegate*)delegate withName:(NSString*)segueId{
AXISWebViewViewController *axisWebView = [delegate.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:segueId];
}
Ok the first thing you need to do is to add a identifier to you view controller at Storyboard, you can do that opening the Storyboard's Identity Inspector and fill the storyboard id field.
Now you are able to call this UIViewController from any place you want like this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"the name of your storyboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"the id of your view controller"];
I hope this can help you.
Please post the code you are using to instantiate the VC from the Storyboard.
In general:
To instantiate a View Controller from StoryBoard:
MyViewController *MyVC = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"MyViewController"];
Make sure you set the Storyboard ID and Restoration ID and Class name to "MyViewController" or whatever you want to call it and use that as the parameter for instantiateViewControllerWithIdentifier:
Then if you are in a navigation controller, simply push the instantiated view controller onto the navigation stack:
[self.navigationController pushViewController:MyVC animated:YES];
or present it modally:
[self presentViewController:MyVC animated:YES completion:nil];

Why a storyboard contain a UITabBarController can't present another storyboard/

The code about present another independent storyboard as follows:
- (IBAction)action:(id)sender {
UIStoryboard *secondStoryboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
UIViewController *firstVC = [secondStoryboard instantiateViewControllerWithIdentifier:#"22"];
[self.navigationController presentViewController:firstVC animated:YES completion:nil];
}
If my storyboard like follow contain a UITabBarController the Pesentbutton with function -(IBAction)action:(id)sender; can't present another independent storyboard.
If my storyboard like follow contain a UINavigationController the Pesentbutton with function -(IBAction)action:(id)sender; presents another independent storyboard as expected.
Who can tell me the reason?More thanks!
Because in the first case self.navigationController is nil... Since no navigation controller is present. Try presenting from self or self.tabBarController

Linking a new viewcontroller to Storyboard?

There is probably a simple solution but I can't figure it out.
I am using storyboards for the interface.
I start with a tab bar controller, but before the user is allowed to use the app the user has to authenticate himself trough a loginview which is modally pushed at the start.
I want to configure the loginview at the same storyboard, but I can't seam to figure out how to link the view controller at the storyboard and my code.
What I have done:
Create a new UIViewController subclass trough file > new > new file.
Drag a new UIViewController in the story board
Set the class in the custom class tab
drags a UILabel for test purpose.
run
No label...
Pull on a new UIViewController that will act as the login view controller onto the MainStoryboard. In the attribute inspector change the identifier to LoginViewController (or something appropriate). Then add
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
}
to the First view controller and the login screen will be loaded from your storyboard and presented.
Hope this helps.
The answer by Scott Sherwood above is most correct answer I found after lot of searching. Though very slight change as per new SDK (6.1), presentModalViewController shows deprecated.
Here is very small change to above answer.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
HomeViewController * hvc = [sb instantiateViewControllerWithIdentifier:#"LoginView"];
[hvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:hvc animated:YES completion:nil];
I'm new in this field. But if the first view controller is a navigation view controller and its rootviewcontroller is a table view controller. If you want to push a view controller like the LoginViewController when you click the cell, and you also want to go back to the table view by using the navigation bar. I recommend this way:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *controller = [sb instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
In this way, you can have the navigation.
By the way, I don't know why this kind of problem you asked will appear. I guess when the loginviewcontroller is created in the code, its view is not the view in the storyboard. If someone know the cause, please tell me! thanks!

Resources