how to segue a view controller on app load using tabbarcontroller - ios5

I am using a tabbarcontroller as the root view controller. Unfortunately, using the new storyboard functionality, it is proving difficult to segue a view controller - Login Page - on the app load.
I am using the below code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
[tabBarController performSegueWithIdentifier:#"loginPage" sender:self];
The segue is set up properly. I went into one of the tabs view controllers and made an IBAction and it successfully segued.
Thanks in advance.

Ran into this same issue today. I had to call:
[self.window makeKeyAndVisible];
before
[self.window.rootViewController performSegueWithIdentifier:#"LoginView" sender:self];
So I'm assuming that when using storyboards the makeKeyAndVisible happens after didFinishLaunchinWithOptions: returns. So when were calling the segue its happening on a view thats not onscreen.

I ran recently into the same issue. However, the solution provided did not work out for me.
The reason was that I used a "push" segue to display my login view controller (which was embedded inside a navigation controller). Changing the style of the segue from "push" to "modal" did the trick for me. Apparently, it is not possible to initiate a "push" segue from within a tab bar controller but only from within a navigation controller.
Furthermore, I did not put the line
[self performSegueWithIdentifier:#"LoginSegue sender:self];
in the method didFinishLaunchingWithOptions:didFinishLaunchingWithOptions: of the app's delegate, but rather in the method viewDidAppear:. Doing so, I did not need the following line of code:
[self.window makeKeyAndVisible];
Hope this is useful to others.

Related

Trigger a seque from the AppDelegate to popup a view in the StoryBoard

I am trying to convert my App to a Storyboard, but am having some problems.
In de previous model I could have an 'actionClass' in my AppDelegate which I called when I needed to pop-up a view.
E.g.
DOArticleViewController *articleView = [[DOArticleViewController alloc] initWithArticle:article notification: notification nibName:#"DOArticleViewController" bundle:nil];
[[self navigationController] pushViewController:articleView animated:YES];
But now with the storyboard it does not work anymore.
Then I tried the following code in the AppDelegate:
id currentController = [[[[self window] rootViewController] navigationController] visibleViewController];
[currentController performSegueWithIdentifier:#"settingsSeque" sender:nil];
Don;t think this is the best anyway, as only the rootViewController has all the seques needed, and might not be the visibleViewController, but one step at a time.
With this the only thing I see happening is the message and no action:
Unbalanced calls to begin/end appearance transitions for UINavigationController: 0xb428e00.
I spend a view hours now on trying to figure out to get this to work, but am realising that it might be better to go back to the traditional independent XIB files....
I solved with the answer given in this question: ios: Accessing a navigation controller from app delegate
I tried to get the Navigation Controller, but this was nil and I didn't realise it.
My navigation controller was the rootViewController, so that is also the NavigationController.
Casting the rootViewController to NavigationController and invoking 'visibleViewController' worked fine after that!

Change XIB View at app start

What is the right way to change XIB View which loaded at app start depending on some app settings. Of course I know how to get all settings I need.
In your application's delegate, in the method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if(yourSettings)
myViewController = [[MyViewController alloc] initWithNibName:#"FirstNibName" bundle:nil];
else
myViewController = [[MyViewController alloc] initWithNibName:#"SecondNibName" bundle:nil];
self.window.rootViewController = self.myViewController;
[self.window makeKeyAndVisible];
return YES;
}
And that's it. Probably you may want to save your settings in your user defaults so you can load the view properly.
However, I would use 2 different view controllers, as you probably want them to do different things, not just modify some graphics.
EDIT-
Let me see if I understand. You will always load the first view controller and, if some conditions are met, you modally want to present the second view controller that will get dismissed at some point, returning the user to the first view controller. If this is the case, I suggest you move the code in your first view controller, in viewDidLoad or better yet in viewDidAppear, as this view controller will always get loaded. Also this way the user can see that he will eventually go to that view controller. I use something like this in applications the user needs to login to so that it will be obvious for him that he cannot continue until he does login.
I can't say that this is the right way to do it, because it's up to the programmer how he arranges his code, but it would seem to me that the place that controls what view and how it appears belongs in a view controller and not in the delegate, especially considering that your first view controller always gets loaded. It should be up to that view controller to see if it presents the second one or not.

SubView did not load in the first launching of my application

I'm new to iPhone development. I tried to build a simple application with a window and a navigation controller as a sub-view of this window. The problem is this: the sub view did not load when I launch the application. I just have a windows with black screen. To load the view controller, I have to quit the application and launch it a second time, then I have my sub view with the navigation controller. I added a button directly in the window to make sure that the black screen is not a problem, but I saw the button at startup.
This is the code I have in my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
[self.window addSubview:_navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Do you have any solution for this problem?
Thank you.
You need to make sure that you have your view hierarchy setup. The window's rootViewController will be the UINavigationController. The UINavigationController controls a hierarchy of viewControllers, so when you instantiate it, you need to assign a rootViewController. Often times this is a subclass of a UITableView.
Because you are alloc/initing the window, I'm assuming that you do not have a XIB/NIB with the UINavigationController and an associated rootViewController like a UITableViewController. Also, rather than adding the view of your navigation controller, you need to assign the rootViewController, to the window. Since iOS4 this is the preferred way of doing things. See here as well. Try this code:
YourViewController *yourViewController = /* code for alloc/initing your viewController */
_navigationController=[[UINavigationController alloc] initWithRootViewController:yourViewController ]
self.window.rootViewController=_navigationController; /* instead of using [self.window addSubview: _navigationController.view] */
[self.window makeKeyAndVisible];
If you are using a XIB/NIB, then you need to make sure the _navigationController is wired up to the XIB file and has a subclass of a viewController wired up as it's rootViewController.
Good Luck

Allowing autorotation inside a programmatically created view that is displayed modally

This has been asked a dozen times on this site, but I haven't found an answer that works for me. I have an iPad application with a UISplitViewController on the root level that is created programmatically. Inside the view that is being displayed in the right hand pane, triggered by user interaction, a UINavigationController is programmatically created and presented to the user. Here is that code:
listenerController = [[UINavigationController alloc] initWithRootViewController:listenerView];
[listenerController.navigationBar setTintColor:[UIColor colorWithRed:185.0f/255.0f green:80.0f/255.0f blue:0.0f/255.0f alpha:1.0f]];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[listenerController setModalPresentationStyle:UIModalPresentationFormSheet];
[listenerController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[listenerController setModalInPopover:YES];
}
[self presentModalViewController:listenerController animated:YES];
[listenerController release];
This does create the view controller properly, but when it is displayed the iPad is forced back into portrait view regardless of what orientation I have the iPad in. Then when I dismiss the modal window, it will rotate back.
I already have shouldAutorotateToInterfaceOrientation in the viewcontroller of the righthand pane set to YES, and I even tried adding this to the main app delegate class without any luck. It doesn't seem like I should have to subclass UINavigationController just to override the shouldAutorotateToInterfaceOrientation method.
Am I calling presentModalViewController from the wrong object? I've tried [self presentModalViewController ...] as well as [self.parentViewController presentModalViewController ...] with the same results.
I'm assuming that the self in your code example is the right-view (detail) view controller. You need to call presentModalViewController from the root UISplitViewController.

iOS Root View Controller's viewDidAppear: called while splash screen (Default.png) still on screen

In my iOS app I want to run a series of operations in my Root View Controller after it has already appeared on the screen. However, it seems that the iOS app is calling viewDidAppear while the splash screen (i.e. showing the Default.png image) is still on the screen and before the root view controller is laid out on the screen. I've tried the same code in viewDidLoad as well, and had the same problem. How can I force code to run only once the root view controller is actually on-screen?
in viewdidload use this
[self performSelector:#selector(loadData) withObject:nil afterDelay:.5];
and then use your code inside loaddata method...
I just encountered a very similar problem myself, wherein I wanted to display a modal login view after my root view controller loaded. I'd previously been using viewDidAppear, but the behavior broke when I upgraded to the iOS 4.3 SDK.
I fixed it by calling a selector on my root view controller from the app delegate's application:didFinishLaunchingWithOptions: selector. Using a delay as in the other answer is a bit of a kludge, and probably not entirely reliable.
In yourAppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the view controller's view to the window and display.
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
// Invoke operations here. For example, show login view:
[viewController showModalLoginView];
return YES;
}