I'm using the code bellow to check if the user is logged in or logged out, and it's working fine. But because I'm getting the storyboard by name, I'm always sending the user to the iPhone "in" or "out" view controller. It's gonna work if I get the active storyboard. How can I fix the code?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
BOOL user = ...;
NSString *segue = user ? #"in" : #"out";
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"iPhone" bundle:nil];
UIViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:segue];
[self.window setRootViewController:viewController];
return YES;
}
If all you're looking to do is to get the viewController from a different storyboard depending on whether your app is running on an iPad vs. an iPhone, you could do this with an if statement:
NSString *storyboardName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
storyboardName = #"iPad";
} else {
storyboardName = #"iPhone";
}
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
If you really need to get the "active" storyboard no matter the situation, this prior answer may help you:
UIStoryboard: What's the Correct Way to Get the Active Storyboard?
Related
I'm want to make an app that uses a UISplitViewControler on the iPad (as far as I understand it's only available on the iPad) but I want the app to be universal.
The setup is like this:
I have a UITableView (as a master view) and when I select a row it should display a detail view of that cell. I am using a Storyboard and I can't figure out how to implement the split view only for the iPad.
What would be the easiest way to achieve that? Thanks.
You don't need two storyboards to do this.You can use them both in a single storyboard.For iphone ,we normally use a class SWRevealViewController(if you are new to iOS coding ..:)) for side menu and splitviewcontroller for ipad.We can also use SWRevealViewController for ipad as well.It depends on your requirement.
For universal apps,create viewcontrollers using size Classes(usually we use any height any width for universal apps ).
change these size classes and create different viewcontrollers for ipad and iphones as required.In most cases any height any width will do the job.
After creating the viewcontrollers,in the appdelegate ,using the instantiateViewcontrollerWithIdentifier method, load the required viewcontroller.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// The device is an iPad running ios 3.2 or later.
}
else {
// The device is an iPhone or iPod touch.
}
For ipad load the splitviewcontroller. and swrevealviewcontroller for iPhone.
This is the core basics.If you need any more information,let me know.
EDIT
Have you seen an arrowmark ath the initial VC(viewcontroller) in the storyboard?This vc is loaded first after the launch screen.In my app,I have a home screen which is common to both iphone and ipad(using size classes as mentioned above).So I can set this vc as the initial VC.In this case I don't have to do anything in the appdelegate.But if I have a different home screen for ipad,then I can make a condition check in the appdelegate didFinishLaunchingWithOptions
You can load the First screen like this.You should follow through splitVC tutorilal and swrevealcontroller tutorial to set the side menu.You should load the SWrevealVC or splitViewcontroller only if the first screen contains the side menu.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UISplitViewController *split = [storyboard instantiateViewControllerWithIdentifier:#"SplitViewController"];
[AppDelegate setRootController:split storyboard:storyboard actiontype:0];
}
else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *split = [storyboard instantiateViewControllerWithIdentifier:#"SWrevealVC"];
[AppDelegate setRootController:split storyboard:storyboard actiontype:-1];
}
return YES;
}
+(void)setRootController:(UIViewController*)controller
storyboard:(UIStoryboard*)storyboard actiontype:(int) actiontype;
{
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && actiontype == 0)
{
UISplitViewController *splitViewController = (UISplitViewController *)controller;
//splitViewController.presentsWithGesture = false;
UINavigationController *masterNavigationController = [splitViewController.viewControllers objectAtIndex:0];
SideMenuViewController *controller = (SideMenuViewController *)masterNavigationController.topViewController;
controller.splitViewController = splitViewController;
splitViewController.delegate = (id)controller;
}
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[UIView
transitionWithView:appDelegate.window
duration:0.5
options:UIViewAnimationOptionAllowAnimatedContent
animations:^(void) {
BOOL oldState = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
appDelegate.window.rootViewController = controller;
[UIView setAnimationsEnabled:oldState];
}
completion:nil];
}
The code may look lengthy,but take it simple.You can only understand the logic if u do things.
When i first come to Login Screen I store value in NSUserDefaults. When I press on signinButtonAction to move to DetailScreen it stucks and never moves forward.
I have made DetailScreen embedded in Navigationcontroller as InitialViewController even the next screen is not navigating to other screens when i re-run it again it comes to DetailScreen and then DetailScreen stucks to navigate to other screens.
How do I handle this?
My code is here:
AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"email"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
categoryVC *viewController = (categoryVC *)[storyboard instantiateViewControllerWithIdentifier:#"categoryVC"];
[self.window setRootViewController:viewController];
}
else
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
signInVC *viewController = (signInVC *)[storyboard instantiateViewControllerWithIdentifier:#"signInVC"];
[self.window setRootViewController:viewController];
}
in SigninButtonAction I am doing:
{
categoryVC * second= [self.storyboard instantiateViewControllerWithIdentifier:#"categoryVC"];
[self.navigationController pushViewController:second animated:YES];
}
I would not set RootViewController if you have such a workflow. I would instantiate main view controller as is, and if you need to show auth I would use presentViewController. Later on, you can do dismiss or something like that.
So, in you app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (nil == [[NSUserDefaults standardUserDefaults] objectForKey:#"email"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
categoryVC *viewController = (categoryVC *)[storyboard instantiateViewControllerWithIdentifier:#"signInVC"];
[self.window.rootViewController presentViewController:viewController animated:NO];
}
}
and then, when you do the auth and it passes:
[self dismissViewControllerAnimated:TRUE completion:nil];
I have my login screen how ever i can able to skip that screen and go to home page when I am in the home page I could`d able to get my navigation controller in my home page i have embedded navigation controller with my login page have connectivity via push segue to home page every thing is good but nav bar is not coming in homepage if I try to embedded nav controller in homepage then also its not coming ... my code is
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
if (![defaults boolForKey:#"registered"])
{ NSLog(#"no user register ");
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
ViewController *secondViewController = [storyBoard instantiateViewControllerWithIdentifier:#"LoginPage"];
self.window.rootViewController = secondViewController;
// then set your root view controller
}
else
{ NSLog(#"user is daam sure registered");
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] ;
;
ABDB_CAMERAHOME *mainviewcontroller = [storyBoard instantiateViewControllerWithIdentifier:#"CameraHome"];
self.window.rootViewController = mainviewcontroller;
}
return YES;
}
is their I am missing constraint about nav controller in program please suggest
User following method to set rootViewController using navigation:
[(UINavigationController*)self.window.rootViewController pushViewController:yourViewController animated:YES];
In latest Xcode 6.1 in storyboards their are settings for navigation controller bar visibility. You can tick the "Shows Navigation Bar" for display.
In your appDelegate.h add below property and synthesize in .m
#property(strong,nonatomic)UINavigationController *navigationController;
#synthesize navigationController;
now modify app delegate as below
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
if (![defaults boolForKey:#"registered"])
{ NSLog(#"no user register ");
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
ViewController *secondViewController = [storyBoard instantiateViewControllerWithIdentifier:#"LoginPage"];
self.navigationController=[[UINavigationController alloc]initWithRootViewController:secondViewController];
self.window.rootViewController = self.navigationController;
// then set your root view controller
}
else
{ NSLog(#"user is daam sure registered");
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] ;
;
ABDB_CAMERAHOME *mainviewcontroller = [storyBoard instantiateViewControllerWithIdentifier:#"CameraHome"];
self.navigationController=[[UINavigationController alloc]initWithRootViewController:mainviewcontroller];
self.window.rootViewController = self.navigationController;
}
return YES;
}
Once all is set, check your storyboard whether you have mentioned proper xib identifier names over there? Also if you have used any navigation controller there, remove that also. Now clean the project from build setting and re-run.
I am trying to:
detect device (iPhone 3.5", iPhone 4", iPad, ect)
load a different storyboard depending on what device and what size the application is running on.
I have watched some tutorials but I am still not getting it, can someone please type/show what code needs to go in the app delegate to achieve these goals.
Thanks!
Try something like this in your app delegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIStoryboard *storyboard = nil;
if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
{
if ([[UIScreen mainScreen] bounds].size.height == 568.0f)
{
storyboard = [UIStoryboard storyboardWithName:#"iPhone5s" bundle:nil];
}
else
{
storyboard = [UIStoryboard storyboardWithName:#"iPhone4" bundle:nil];
}
}
else
{
storyboard = [UIStoryboard storyboardWithName:#"iPad" bundle:nil];
}
[window setRootViewController:[storyboard instantiateInitialViewController]];
[window makeKeyAndVisible];
}
The goal:
When my app starts up - I need it to display a view before it gets to the "Home" screen. Its a tab bar application and this view is not part of the tabbar.
I am using Storyboards and Xcode 5 - iOS7 only app.
The problem:
I have code that will check if the app is first launch or not. Based on that, I then want to present a one time only view to the user.
What I have tried:
The following code is in the appDelegate of the application as this is where it all starts. I call the following bit of code in there:
-(void)showCountrySettings
{
if (self.termsHaveBeenAccepted){
BBCounterySettingsViewController *countrySettings = [[BBCounterySettingsViewController alloc]initWithNibName:#"View" bundle:nil];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER"];
[self.navigationController pushViewController:vc animated:YES];
}
I get compile errors as [self.navigationController..] doesn't exist. Nor does [self.tabbarcontroller...];
This is obvious as I don't have properties setup for these - but how do I go about resolving this and connecting the tab bar to the storyboard?
What am I missing?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if(!isAgreementAccepted)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"IDD"];
self.window.rootViewController=vc;
}
return YES;
}
If agreement is not accepted set the T&C viewController as rootViewController when user click the accept button then set the TabBarviewController as root.
u can access the widow object through application delegate any where
[[[UIApplication sharedApplication]delegate] window].rootViewController=tabViewController.
Change the rootviewcontroller of window programaticaly
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *aStoryBoard=[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
UITabBarController *aTabCtrl=[aStoryBoard instantiateViewControllerWithIdentifier:#"Tab"];
FirstVC *aFirstCtrl=[aStoryBoard instantiateViewControllerWithIdentifier:#"First"];
if(self.termsHaveBeenAccepted)
self.window.rootViewController=aFirstCtrl;
else
self.window.rootViewController=aTabCtrl;
return YES;
}
This will definitely work I have tested.
If Tabbarcontroller is your root viewcontroller, then using below code will resolve the issue in your appdelegate.
-(void)showCountrySettings
{
if (self.termsHaveBeenAccepted){
BBCounterySettingsViewController *countrySettings = [[BBCounterySettingsViewController alloc]initWithNibName:#"View" bundle:nil];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER"];
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
[tabBarController presentModalviewcontroller:vc animated:YES completionblock:nil];
}
add your viewcontroller superview of tabbarcontroller as first time else call tabbarcontroller alone