SubView did not load in the first launching of my application - iphone

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

Related

Create UINavigationController in other xib?

Hello, I have this code in my AppDelegate:
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
Now I want to create a XIB file in which I want to put a UINavigationController and add a UITableView. How do I create this through code without changing the delegate class?
I've tried this but it does not work:
PlacesTableViewController *obj = [[PlacesTableViewController alloc]init];
obj.title = #"Farmacie intorno a te";
UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:obj];
[self.window addSubview:navC.view];
First, the "modern" way to set up your window is to use the window's rootViewController property:
window.rootViewController = someViewController;
The old way, where you add a view controller's view to the window as a subview, still works but the app will log a complaint about wanting the root view controller to be set up by the time the app is done launching.
Second, if you're going to replace the root view controller (in this case you're replacing your viewController with navC) using the old style, you'd want to remove the old view controller's view from the window. Your best bet is to just use the window's rootViewController property, since your code will be expected to use that going forward anyway.

how to segue a view controller on app load using tabbarcontroller

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.

XCode TabBar app - multiple views?

I have been working to create an app with tabs at the bottom, but with the ability to switch views with my own links on the nib's that the tabBar app displays.
I have been trying to find an example of this but xcode 4.2 (whether on ios5.0 or earlier) does not create a mainwindow.xib, so i have had to find a way to create this programmataiically as follows:
#import "AppDelegate.h"
#import "HomeViewController.h"
#import "ViewListViewController.h"
#import "ShareViewController.h"
#import "FriendsViewController.h"
#import "SettingsViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *homeViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
UIViewController *viewListViewController = [[ViewListViewController alloc] initWithNibName:#"ViewListViewController" bundle:nil];
UIViewController *shareViewController = [[ShareViewController alloc] initWithNibName:#"ShareViewController" bundle:nil];
UIViewController *friendsViewController = [[FriendsViewController alloc] initWithNibName:#"FriendsViewController" bundle:nil];
UIViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setDelegate:self];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:homeViewController, viewListViewController, shareViewController, friendsViewController, settingsViewController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
This code in my AppDelegate.m works fine and i can switch between the tabs perfectly. The issue I am having is that I will be adding graphics to the pages and using them as buttons to switch between views for adding/editing/deleting data from a database (separate issue I will have to learn to do next).
Most examples i have found start the app with either a tabBar, or a uiNavigation with UIViews to switch between. I have yet to find an example that will clearly show how to do what I am looking to do. Is my approach wrong to create the tabBar application as my base template for this, or can this be done? Each tab may have multiple views that perform functions, but I need to be able to create and switch between them within the tabBar framework.
thanks in advance,
Silver Tiger
-UPDATE:-
AFter reading up a bit I am now thinking that aUINavigationController is not what I am looking for. What I need is a way for a custom button that I create to allow me to switch views, as the buttons I will be using in my content will be either graphics or transparent on top of a graphic background. Is it possible to use custom buttons to push new views while still keeping the tabBar visible at all times? My structure would be as below
Home
Add
Edit
delete
ViewList - Single page to view list
Share - Single page to send emails / invites
Friends
Add
Edit
Delete
Settings - Single page for app settings/preferences
so essentially i'd only really have 2 of the tabs where i would be pushing new views but the UINavigationController doesn't seem to fit the method i am using since i am not using a single bar up top to switch views.
Both UINavigationController and UITabBarController are controllers of view controllers. In this function, they work the same way. But there are some differences. Use this as a guide:
UINavigationController: Here you usually have a navigation bar at the top. You "push" other view controllers onto a "stack" and the navigation controller takes care of all the buttons on top to go back and forth as well as the animations, etc. Often, but not always, this is implemented with table views.
UITabBarController: Here you have the tab bar at the bottom at all times, so this should only make sense if there are things the user would typically have to access regardless of where she is in the view hierarchy. Look at the Alarm Clock app for a typical example.
Combination: If you want to combine the two, you start with the tab bar controller. You can then assign separate navigation controllers to one or more tab bar items. The navigation controllers in turn manage their view controllers. Make sure your UI does not get too cluttered with navigation controls at the top and bottom of the screen. Look at the iTunes App for a typical example.
BTW, you can always make these controls disappear, if one view needs more space (e.g. in the iPod app the tab bar gets replaced by a toolbar when going to "Now Playing").

iPhone: create new "View-based Application" = no view controller?

I have created a new iPhone "View-based Application" in Xcode. I then added a new "UIViewController subclass" and checked the "with XIB for user interface. Now the issue I have is that after hooking up all the variables and message handlers, I cannot push the new controller onto the stack using the following code:
[self.navigationController pushViewController:self.cabinetController
animated:YES];
All the variables and views are hooked up correctly, so all that I can think of is that its the way I am doing it, by pushing it onto the "navigationController". Is there something I am missing here? (I am very new to iPhone and Apple programming in general, so its probably a very simple oversight).
I realise that not enough information has been supplied ... here is a link to the project. Please note that it is an educational exercise has some creatively names classes.
http://files.me.com/nippysaurus/4yqz8t
In your appDelegate create a UINavigationController instance variable and then use your existing viewController as the rootViewController of the navigation controller.
e.g. in pure code using a UITableViewController (you can use xibs as well which your template app probably does).
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Create root view and navigation controller
UITableViewController *rootViewController = [[[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
// Not necessary if you're using xibs
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Add the nav controller's root view to the window
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
You need to change your view controller to a navigation controller, with its root view controller set as the current view controller.
If you examine your self.navigationController, you will realize it is nil. Messaging nil doesn't hurt, so no error message here.
Add another layer with a UINavigationController, and add your RandomShitViewController (nice name btw.) as its root view controller.
The navigation controller handles the push / pop part, your old controller manages its view.

TableView To Navigation Controller on the View-Based Application Project?

I built an application. On the one my views I used TableView. So now I want to change this Table view to a navigation controller.
1- How can I change UITable view to Navigation Controller.؟
The easiest way to achieve this is to create a new 'Navigation-based Application'. This will set up everything you need. By default this will set you up with simple RootViewController, if you want you can change this to be your TableViewController by editing the application delegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
rootViewController.managedObjectContext = self.managedObjectContext;
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
All you need to do is copy your TableViewController into the new project and change the RootViewController to be yours
If you have started with one of the template project in XCode find the .xib file for your view and launch InterfaceBuilder by double clicking the .xib file. You can manipulate the view directly by dragging and dropping components as well as adding components from a palette.
I would walk through one of Apples tutorials and get a feel for working with InterfaceBuilder - this tutorial shows adding a view controller.
Thank you everybody . finally i figured out :D , just put this code :
UINavigationController *myNav=[[UINavigationController alloc] initWithRootViewController:[[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil]];
[self presentModalViewController:myNav animated:YES];