How to push Storyboard from loginviewcontroller which is an xib - objective-c

I am having Loginview controller,I have created it with Xib and i want to push mainstoryboard on clicking loginbutton.
LoginViewController *lObjloginview = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *lObjtempview = [[UINavigationController alloc] initWithRootViewController:lObjloginview];
self.window.rootViewController = lObjtempview;
[self.window makeKeyAndVisible];
This will add loginview from xib.but after pressing loginbutton I want to load storyboard.Can anyone help me in pushing mainstoryboard which has tabbar controller inside mainstoryboard to link xib.

Load the initial view controller from the storyboard and present it
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController * initialVC = [storyboard instantiateInitialViewController];
self.window.rootViewController = initialVC;
If you know a priori the class you're instantiating
UITabBarController * initialVC = (UITabBarController *)[storyboard instantiateInitialViewController];

Related

Loginviewcontroller before SWRevealviewcontroller with Storyboard objective C

I am using SWRevealViewcontroller by using this sample:
I want to add loginviewcontroller before SWRevealViewcontroller.
What I've tried:
Added loginviewcontroller in Storyboard. Set it as InitialViewcontroller.
Inside button action.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
MapViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"MapViewController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navController setViewControllers: #[rootViewController] animated: YES];
[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
But it is not working. Please help
viewController1
viewController2 *viewController2=[self.storyboard instantiateViewControllerWithIdentifier:#"viewController2Id"];
[self.navigationController pushViewController: viewController2 animated:YES];
viewController2
-(void)pushAfterLoginScreen
{
SidebarViewController *sidebarmemu =(SidebarViewController*) [self.storyboard instantiateViewControllerWithIdentifier:#"SidebarController"];
viewController3* viewController3 = (viewController2*)[self.storyboard instantiateViewControllerWithIdentifier:#"viewController2Id"];
SWRevealViewController *revealController;
UINavigationController *frontNavigationController;
UINavigationController *rearNavigationController;
frontNavigationController =[[UINavigationController alloc]initWithRootViewController: viewController2];
rearNavigationController =[[UINavigationController alloc]initWithRootViewController:sidebarmemu];
revealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
revealController.delegate = self;
self.swcontroller =revealController;
AppDelegate *app =(AppDelegate *)[[UIApplication sharedApplication]delegate];
app.window.rootViewController =self.swcontroller;
}
SWRevealViewcontroller which is already embed with the navigationController you need to pass control to SWRevealViewcontroller after successful login using presentViewController method by setting up the storyboard id to navigationController of SWRevealViewcontroller and navigate your app using below code:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"navContStoryboardID"];
[vc setModalPresentationStyle:UIModalPresentationCustom]; // optional
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; // optional
[self presentViewController:vc animated:YES completion:nil];
Hope this helps you.

Present modal UIViewController over UISplitViewController from AppDelegate

I've written the code below to present a modal UIViewController on top of a UISplitViewController from AppDelegate. However, it keeps throwing Application tried to present modally an active controller <UISplitViewController: 0x138023800>..
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
NSLog(#"%#", shortcutItem.type);
if([shortcutItem.type isEqualToString:#"com.myapp.show-me"]){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UISplitViewController *tc = [storyboard instantiateViewControllerWithIdentifier:#"SplitViewController"];
self.window.rootViewController = tc;
UINavigationController *ivc = [storyboard instantiateViewControllerWithIdentifier:#"Navigation"];
UIViewController *detailViewController = [ivc.viewControllers objectAtIndex:0];
detailViewController.restaurant = #"1";
[tc presentViewController:detailViewController animated:YES completion:nil];
} else if([shortcutItem.type isEqualToString:#"com.myapp.see-all"]){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UISplitViewController *tc = [storyboard instantiateViewControllerWithIdentifier:#"SplitViewController"];
self.window.rootViewController = tc;
}
}
What am I doing wrong with com.myapp.show-me?
As it turns out, I only needed to present the UINavigationController instead of the UIViewController.
[tc presentViewController:ivc animated:YES completion:nil];

Set Root View In App delegate with View Hierachy

I want to be able to manually set the root view controller in app delegate intact with it's view hierarchy
My View Controller Hierarchy is
TabBarController
UINavigationController
FirstViewController
DetailedViewController (I want to show this view)
Currently what I do is
DetailedViewController *vc = (DetailedViewController *)[mainStoryBoard instantiateViewControllerWithIdentifier:#"DetailedViewController"];
[self.window setRootViewController:vc];
[vc performSelector:#selector(setup:) withObject:section];
which obviously ignores the hierarchy and therefore when the app loads, I dont have the nav bar nor the tab bar.
So I tried this
UITabBarController *tabBarController = [mainStoryBoard instantiateViewControllerWithIdentifier:#"tabController"];
tabBarController.selectedIndex = 0;
DetailedViewController *vc = (DetailedViewController *)[mainStoryBoard instantiateViewControllerWithIdentifier:#"DetailedViewController"];
UINavigationController *navController = [mainStoryBoard instantiateViewControllerWithIdentifier:#"NavController"];
[navController pushViewController:vc animated:YES];
[self.window setRootViewController:tabBarController];
But using this code I can view FirstViewController and not the DetailedViewController
Your view hirarchy is such that
You have tabbar that has navcontroller with a rootview of firstviewcontroller so this code add firstview as rootView
UITabBarController *tabBarController = [mainStoryBoard instantiateViewControllerWithIdentifier:#"tabController"];
tabBarController.selectedIndex = 0;
FrstViewController *firstView = (FirstViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:#"FirstViewController"];
UINavigationController *navController = [UINavigationViewController alloc] initWithRootViewController:firstview]
self.tabbarController.viewControllers = [NSArray arrayWithObjects:navController,nil]
[self.window setRootViewController:tabBarController];
When the first we loads in viewDidload of it push detailviewcontroller
-(void) viewDidLoad
{
DetailedViewController *vc = (DetailedViewController *)[mainStoryBoard instantiateViewControllerWithIdentifier:#"DetailedViewController"];
[self.navigationController pushViewController vc];
}

Could not load NIB in bundle with name 'SecondViewController'

I'm trying to push the Navigator Controller to a different view controller but I keep getting the error saying the nib bundle name can't be loaded. Here's the code I'm using:
SecondViewController *vc1 = [[SecondViewController alloc]
initWithNibName:NSStringFromClass([SecondViewController class]) bundle:Nil];
[self.navigationController pushViewController:vc1 animated:YES];
You have to pass the name of the nib file, not the class name:
// Assuming there is a properly set up SecondViewController.xib in your project.
SecondViewController *vc1 = [[SecondViewController alloc]
initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:vc1 animated:YES];
EDIT:
If you're using Storyboards, you have to load your GUI a bit differently:
UIStoryboard *sboard = [UIStoryboard storyboardWithName:#"StoryboardFileName"
bundle:nil];
SecondViewController *vc1 = [sboard instantiateInitialViewController];

How to load a login view controller from app delegate

Here's the situation. I have an NSTimer in Appdelegate.m that performs a method that checks the users location and logs him out if he is out of a region. When that happens I want to load the login view controller. However, with the following lines of code, I am able to see the VC but if I click on a textfield to type, I get a sigbart. Any ideas how to load the initial VC from app delegate correctly?
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
LoginViewController *loginViewController = (LoginViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"Login"];
[self.window addSubview:loginViewController.view];
[self.window makeKeyAndVisible];
I've been struggling with this quite a bit. Here's what worked for me
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
LoginViewController *loginViewController = (LoginViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"Login"];
//set the root controller to it
self.window.rootViewController = loginViewController
If you're still having errors, it has to do with your LoginViewController, not the AppDelegate transition. Hope this helps.
Both audiophilic and Flaviu 's approaches are ok. But both of you are missing one extra line of code.
I found out that you have to allocate and init the UIWindow object first. I dont know why but initializing the object solved the problems.
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
audiophilic's approach:
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
ResultsInListViewController *resultsInListViewController = (ResultsInListViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"ResultsInListViewController"];
[self.window addSubview:resultsInListViewController.view];
[self.window makeKeyAndVisible];
Flaviu's approach:
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
ResultsInListViewController *resultsInListViewController = (ResultsInListViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"ResultsInListViewController"];
[self.window setRootViewController:resultsInListViewController];
[self.window makeKeyAndVisible];
This is the code I use when I want to change views:
#import "LoginViewController.h"
LoginViewController *screen = [[LoginViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
//or
[self.window addSubView:screen];
Using that code will create a brand new instance of it and show it, so it should accomplish your question. You may have to move some things around, but it should work.
Let me know if you need any more help.
Hope this helps you!