Set a navigation controller without an app delegate - iphone

I would like to show a Navigation Controller after clicking a button. Every tutorial assumes the navigation controller will be the first screen so it links it to the App Delegate, but App delegate only appears at MainWindow.xib.
How do you guys add a navigation controller to a view different than the MainWindow?
Thanks!

Here is some sample code to expand on Roger's answer. The following method is linked to some user interaction on the current view controller (to compose an email for example). This will give the compose view the navigation bar across the top instead of coding buttons inside your custom view.
-(void) composeButtonPushed: (id) sender {
ComposeViewController *controller = [[ComposeViewController alloc] initWithNibName:#"ComposeView" bundle:nil];
UINavigationController *composeNavController = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentModalViewController:composeNavController animated:NO];
}

UINavigationController is to navigate a heirarchy of views with UIViewControllers. If you don't have a root UIViewController, it won't work (and doesn;t make sense). If you do have a UIViewController, you simply send a - (id)initWithRootViewController:(UIViewController *)rootViewController init message to a new navigation controller passing in your UIViewController.

Related

Loading another UIView directly from the main ViewController

I am in the process of creating an application. In the main ViewController I have created the menu. But I want to have a login screen (UIView) to appear before the menu is visible.
But because the menu loads as soon as I run the application I have decided to create another UIView controller and have that loaded on top of the main ViewController.
Therefore at the end of my main ViewController viewDidLoad I have added the following code to open on top of that view the login view
LoginPageView *loginPageView = [[LoginPageView alloc] initWithNibName:#"LoginPageView" bundle:nil];
loginPageView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:loginPageView animated:YES];
As I know the purpose of that code is to present another view, but unfortunately the login view does not appear. Only the main ViewController load.
Can anyone help me on that? Have you realised what exactly I want?
Thanks a lot
In the viewDidLoad method, the view exists, but there is no guarantee that the view is already part of the view hierarchy of your application. In fact, almost ain't.
What you can try is take that code in the viewWillAppear: or viewDidAppear:.
Make a UINavigationController. Use this as your window's root view controller. Set the UINavigationController to not show the navigation bar.
Set the navigation controller's child view controller to your login view controller.
When the user logs in successfully, create your main view controller and push it, like this:
MainViewController *mainVC = [[MainViewController alloc] initWithWhatever...];
[self.navigationController pushViewController:mainVC animated:YES];
When the user logs out, you can just do this to get back to the login VC:
[self.navigationController popViewControllerAnimated:YES];

How to use UItableView and UINavigationController in a "search" application ? Best practices

I would like to combine UITableView and UINaviationController in an app but as a newbie most apps I've seen just send you straight to the results view (UITableView). But, I guess a "normal" search application does not assume you have the results on the first screen. There should be a search form on first screen with input fields and a button that triggers the search process and show some results and navigation.
So, I'm just trying to replicate this normal behaviour in my app. I've already made the search form (no navigation shown on it, of course) and a seperated View called "ListingViewController" with its related View and containing a UITableView and where I think I should add the Navigation...The next idea will be to make a DetailViewController and possibly and ListingMapController to show the listing in a GoogleMap.
So, where I'm stuck at is how to add this Navigation Controller ?
Some suggested me to add it in the SearchViewController delegate...
But I don't want a navigation on search form of course...
Some suggested me to open the Navigation controller modally...
But, I"m also planning at adding a Tab Bar to allow user to see other informations (like About,etc...) and with a modal Nav controller I don't know if they will still see the bottom Tabbar...
Any suggestions? What do you think is of best practices especially to avoid my app of being rejected by Apple?
Thx in advance for reading and helping!
Stephane
You could init the navigationController with your View Controller as the root view Controller. Then hide the navigationBar (if you need to). You would then add the navigationController.view as the subview. This will basically look like the original view controller. Then you can pushViewController: animated: to push the results view Controller.
So, for example in your AppDelegate (or in the proper view controller):
Create a property and ivar for a UINavigationController and hook up its outlets in interface builder. Then set your search controller as the root view controller for the nav bar, and add it as a subview.
MySearchViewController* searchController = [[MySearchViewController alloc] init];
self.myNavigationController = [[UINavigationController alloc] initWithRootController:searchController];
[searchController release];
self.myNavigationController.navigationBarHidden = YES;
[self.window addSubview:self.myNavigationController.view];
[self.window makeKeyAndVisible];
Then of course in your searchController, you would simply say:
ResultsViewController* myResultsViewController = [[MyResultsViewController alloc] init];
//You may want to create another init method and pass in some arguments like an array:
// [[MyResultsViewController alloc] initWithResults:results];
then push the viewController
//This is in your search controller class
[self.navigationController pushViewController:myResultsViewController Animated:YES];
[myResultsViewController release];
from the results viewController, to get back you pop the view controller off of the navigationController view controller's stack.
//In results view controller perhaps in some IBAction for a back button:
-(IBAction)backButtonPressed:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}

programmatically creating a UINavigationController

Initially I used the IB to drag a navcontroller onto my tabbar and then setting the root controller. However, I'm trying to create a nav controller without IB.
What I've done is created a navcontroller subclass that will be used in my tabbar. The nav bar does show up so I know that is working.
Now, I need to push a viewcontroller into the hierarchy. In the nav controller's viewDidLoad:
- (void)viewDidLoad {
InfoViewController *initialController = [[InfoViewController alloc] init];
[self.navigationController pushViewController:initialController animated:YES];
[initialController release];
[super viewDidLoad];
}
I get no errors, but nothing shows up in the navigation controller. Anyone know why?
What does your InfoViewController's init method look like? If you're using a nib to build that, then you need to call initWithNib, not init.
Create the viewcontroller when/where you create the navigation controller (and add them to it) then add the list of view controllers to the tabbar.

iPhone - how do I add a navigation controller to a view?

I currently have an application that does the the following:
S: Loads a view as a login screen to start with.
a: If login is successful I add a terms and conditions screen as a subview
b: If not successful I add a sign up form as a subview
F: Then I load the main part of my app on success of either of the a or b which is the part of the app where there is a navigation controller and a tab bar controller. This is set up in MainWindow.xib
S, a and b also have Nav bars but no navigation controllers as I didn't think I would need navigation control on the login screens.
However it turns out I do, I want to be able to have back navigation from both a and b to the initial login screen.
I have tried several ways of doing this including trying the following answers:
How to add navigation controller in View Based Application in iPhone?
How do you properly set up a secondary view to support a navigation Controller on the iPhone?
how to add navigation controller programatically?
But none of them work for me, they display the new Navigation controller over the login screen and dont load the a or b screens.
I'm guessing this is because I am adding them as subviews to my loginView and this is not the correct way to do this? My code is as follows:
if(self.tcSubViewController == nil){
TCSubViewController *_tcSubViewController = [[TCSubViewController alloc] initWithNibName:#"T&CView" bundle:[NSBundle mainBundle]];
self.tcSubViewController = _tcSubViewController;
[_tcSubViewController release];
}
[self.view addSubview:[tcSubViewController view]];
I'm guessing there's a fundamental flaw in the way my Login flows? I should be able to completely remove the LoginView and then display the Terms and conditions view without having to add it as subview, shouldn't I?
You need to dismiss the navigation controller to go back to.
To dismiss modal view:
1.Easy way: In your modal view in some method that you call to dismiss just add:
[self.navigationController dismissModalViewControllerAnimated:YES];
2.More complex way: Implement and delegate protocol on your modal view and make the view controller that presents the modal view the delegate of it. And in the delegate method dismiss the modal view. I do this way when I need to send data from modal view to the controller that present it.
Reference to this post
Navigation controller philosophy is that you add only navigationController.view as UIWindow subview and it wil manage the rest by itself. You only need to push/pop viewControllers and their corresponding views will be added/removed from screen automatically.
sample code from my current application:
HomeController *homeController = [[[HomeController alloc] init] autorelease];
self.controller = [[[UINavigationController alloc] initWithRootViewController:homeController] autorelease];
self.controller.navigationBarHidden = YES;
[self.window addSubview:self.controller.view];
[self.window makeKeyAndVisible];
and then to push next view you just add next controller:
[self.navigationController pushViewController:newController animated:YES];

iPhone SDK: Can't specify view to Navigation Controller

I am working on my first iPhone app and making good progress. But there is one thing I just don't understand.
When my app starts it displays a UIView with some functionality on it. This works fine. One of the buttons on that screen is supposed to load a new view (HistoryViewController) which contains a navigation controller.
The problem is… whenever HistoryViewController is loaded the app crashes because there is no view. It's true because in the xib-File I can't connect the File's Owner's view to anything:
http://www.freeimagehosting.net/image.php?1a3caa8b8d.png
I definitely have a lack of knowledge somewhere but after hours of research I have not been able to solve this problem.
Any hints?
Thank you!
Normally you would either:
click on that bottom line (History Table View Controller, "HTVC") and in the inspector window specify a NIB Name - which means you would first have to make a new NIB.
or
doubleclick that bottom line (HTVC), so the 320x480 preview window pops up, and then drag in a UIView from the library.
Using the first method, you tell the view controller to dynamically load the NIB as the view to connect, and using the second method you do this for the view controller using IB. The view you drag in will then show up as a child of that bottom line (HTVC).
edit to actually load the nib file you created, do this to push the view controller:
UIViewController *controller = [[UIViewController alloc] autorelease];
[controller initWithNibName:#"nibfilename" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
substituting UIViewController for your own view controller class (if needed) and nibfilename with the filename of the nib (minus the extension!)
It's hard to tell what your problem is exactly, but I'll offer some advice.
When creating a navigation controller (or tab controller for that matter) in interface builder, its easy to not understand what is really happening, so my suggestion drop interface builder for a second and lets build it in code.
In general I really dislike building either UI Navigation Controller or tab view controller in interface builder, I really just rather build the views themselves and create the UINavigationController in code.
You have a view which shows the HistoryTableViewController which you want to be contained in a UINavigationController so the code to do this is:
- (void) showHistory
{
HistoryViewController *historyVC = [[HistoryViewController alloc] init];
// If you create historyviewcontroller in nib
// HistoryViewController *histroyVC [[HistoryViewController alloc] initWithNibName:#"myNib" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootController:histroyViewController];
[self presentModalViewContoller:navController animated:YES];
}
This will create a nav controller showing your history view controller as the root view controller. Can't be easier.