I am designing an iPhone App wherein I would like there to be a main screen. I then want users to be able to hit an info button in the bottom right of this screen and enter an entirely different interface that will handle options and various forms of data display.
After some research on View Controllers in Apple's documentation, I have determined that I definitely want to use a modal view when the info button is pressed. My problem, though, is that the interface that fits best is a UITabBarController. I'd like to have a tab in my UITabBarController that represents the main screen and, when touched, will tell the main screen to dismiss the UITabBarController.
I have seen quite a few examples on the web where people add custom view controllers with buttons that trigger the modal view dismissal to tabs on the UITabBarController, but I would like to entirely avoid displaying an extra view. As soon as the user touches the main screen tab, I'd like the modal view to dismiss. I have already experimented with approaches that placed the main screen in the UITabBarController, and was not satisfied, so I'm hoping somebody out there is familiar with the rare modal UITabBarController design and can offer me some guidance.
Is this at all possible? If not, how can I accomplish the same interface flow in a different way?
I haven't tried this yet, but is there a way I could set the UITabBarController up with an empty UIViewController to represent the main screen and call on a UITabBarControllerDelegate to send the dismiss message to the main screen before the empty view appears?
Ok, I figured this out. I admit this is probably not the most orthodox UI design, but I think it does make intuitive sense... somebody should probably link me to the Apple UI guideline that this violates, though.
The following code will allow a user to press an info button that will animate the presentation of a modal UITabBarController. In this example, the UITabBarController has only one tab and the tab bar has not been set up with icons or titles. Once in the modal view, the user can click the first tab in the bar and the modal view will be dismissed. (This logically means that you'll want to tell the UITabBarController to start at an index other than the first.)
- (IBAction)infoButtonPressed
{
UIViewController *mainScreen = [[UIViewController alloc] init];
NSArray *viewControllers = [[NSArray alloc] initWithObjects:mainScreen, nil];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = viewControllers;
//This means that "self" needs to implement the UITabBarControllerDelegate protocol
tabBarController.delegate = self;
[self presentModalViewController:tabBarController animated:YES];
[mainScreen release];
[viewControllers release];
[tabBarController release];
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (viewController == [tabBarController.viewControllers objectAtIndex:0]) {
[self dismissModalViewControllerAnimated:YES];
}
}
I am now, however, getting a warning about two-stage rotation animation:
Using two-stage rotation animation. To use the smoother single-stage animation, this application must remove two-stage method implementations.
Using two-stage rotation animation is not supported when rotating more than one view controller or view controllers not the window delegate
Preliminary research said that it was because I was using a UIImageViewController to display a background on my mainScreen view, but I have since changed that to UIColor initWithPatternImage. Anybody know how to get rid of the warning-- or if it even matters to ignore it?
Sounds like a confusing (or at least unusual) user experience. Tabs are expected to be the apps main navigation level.
You should consider showing the sections in a segmented button or in a vertical list with back navigation (e.g. The preferences app)
Related
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];
}
This is the situation:
I have a tab bar with 2 tabs. Tab01 and Tab02.
In Tab01 I have a button which pushes repVC:
repVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:repVC animated:YES];
[(UIViewController *)[tabController.viewControllers objectAtIndex:0] setView:repVC.view];
[repVC release];
Inside repVC I have another button which pushes an MFMailComposerViewController:
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
[self presentModalViewController:mail animated:YES];
[mail release];
The problem is: when mailView is shown(in Tab01) and I click Tab02, then back to Tab01, the mailView is hidden and even if I click the email button again, the view won't be presented.
So what I have is: Tab01.view -> repVC.view -> mail.view
For repVC, I use this line when I push the view so that even if I go switch tabs, that view will still be activated:
[(UIViewController *)[tabController.viewControllers objectAtIndex:0] setView:repVC.view];
But I can't do the same for mail because tabController is declared in another class which I cannot import. So I can't access the tabController and set the view for Tab01.
Hope the edit helped the understanding.
Hmm,
I still would suggest to use a Navigationcontroller. Would make things way easier, is conform to apple guidelines and suggestions and is pretty fast implemented. (Just create a Navigationcontroller, put the View of Tab1 as main view and hand it over to the TabbarController. Then for the mailView use [self.navigationController pushViewController:mail animated:YES]; Then the navcontroller "saves" the present view for you when u switch tabs)
But if for some Reason you have to use a modalViewcontroller you could either just deactivate the tabbar while the ModalView is shown or try to implement a switch or a simple if...else case in your ViewWillAppear where u check what screen to load.
Then Clean out the Window and load the right screen.
Hope you get the idea of what I mean, sometimes my way of writing seems to confuse people. ^^
A little more information would be great.
How did u set up your TabbarController?
How do u push the new view? Within a UINavigationController? If not, then do it with a navController, he should save the actual state of view and your problem should be solved.
If u already use a navController please post your ViewDidLoad and ViewWillAppear of the Viewcontroller of Tab 1
As #Amandir points out you could probably solve your problems by using a UINavigationController. I get a feeling that you are trying to abuse the modal view controller concept a bit and that's why it doesn't work as you expect. When you use presentModalViewController:animated: the intention should be that you are displaying a view that is modal, i.e. the user must interact and dismiss the modal view before she can continue.
What the paragraph above means that when you present a modal view controller it shouldn't be possible to use the tab bar. Since you are using the word push I'm guessing that you would like change the view of Tab01 while still being able to use the functionality of the tab bar. The problem is that there isn't any built-in method of pushing view controllers besides UINavigationController. persentModalViewController:animated: should only be used in case where you want a modal view, which on the iPhone means a full screen view.
The easiest way would probably be to use an UINavigationController and hide the navigation bar. Then you would get the functionality I think you are after. The other option is to manually add and remove sub views.
[self.view addSubview:repVC.view];
and
[repVC.view removeFromSuperview];
[self.view addSubview:mail.view];
You can use block animations if you want some fancy transitions.
I've got a sample application on http://github.com/niklassaers/Test-iPhone-TabBar-App that shows my problem: I have a regular view-based application, and at some point (in this case when I click a button) I want to load a tabbar controller and display it. I believe this is what I should be doing:
MyTabBarController *tabs = [[MyTabBarController alloc] initWithNibName:#"TabBar" bundle:nil];
[self.view addSubview:tabs.view];
Unfortunately, this brings up a bit of black in the bottom of my main view and nothing more. I believed it should bring up the tabbar, the tabs, and the selected view. What is the correct way of loading a TabBarController (or making a TabBar controller if that's what I've done wrong) in a view-based application?
Cheers
Nik
You should use a UINavigationController, then just push the tabs controller onto the nav controller when you're ready to display it.
I've come across this twice now.
Sometimes using the following line of code:
[self.navigationController presentModalViewController:aViewController animated:YES];
displays the view, but the navigation bar is then hidden.
I can write:
[self.navigationController setNavigationBarHidden:NO];
to my hearts content, everywhere I can think of with no effect.
Has anyone ran into this?
Am I doing something silly?
No, I ran into this as well. The problem is that when you present a modal view controller with a UIViewController based class, it does not extend the calling navigation controller's nav bar onto the modal. The modal view covers the entire screen. What I ended up doing to solve the problem was to create a UINavigationController and push the UIViewController based class onto it, and then do presentModalViewController to the navigation controller's instance.
like:
UIViewController *vc = [[UIViewController alloc] init];
UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentModalViewController:cntrol animated:YES];
[cntrol release];
That allowed me to have a nav bar at the top.
I am not sure if that will help in your particular case, the only other thing I would suggest is to replicate the behavior of the modal with a UIAnimation that stops 44px below the top of the phone. That would keep the original navigation bar visible.
#HeatMiser shows a great way to get around the "bug" surrounding the inability to display items on the nav bar. I'm not sure, however, if this is strictly a bug in Presentation, since modal operations ought to trump the underlying view's interface theme. Having the modal operation's theme mimic the underlying UI theme is fine, but wrapping the true modal view with a navigation view feels wrong to me (extra view object just to get a little more behavior).
Instead, the following worked for me and gives the same behavior as "New Message" does in the Mail program (on the iPhone).
In IB, place a UIToolBar at the top of the modal screen (mimicking the navigation bar) with "Cancel" and "Save" UIBarButtonItem's and a Flexible Space Bar Button Item in between to get the buttons to align left and right. Then, add a UILabel centered over the UIToolBar (The Font Helvetica, Bold, Size 18 appears to match the Navigation Bar Title). Connect the buttons to IBAction's on the modal's UIViewController, and you're done.
If there is a navigation controller active, then you should just use
[self.navigationController pushViewControllerAnimated:how];
to slide another view controller in, while giving yourself and the user into a consistent user interface complete with 'automatic' back button support.
Once a navigation controller is in use, presenting a modal view controller should only be done to enlarge the usable area on the screen. And then, you should really use a fancy animation to let the user know that you are stepping away from the "task" or "steps" that the navigation controller was embodying.
Maybe this is obvious, but once you're done with the modal view and want to dismiss it, you should do something like this in your modal vc:
[parentController dismissModalViewControllerAnimated:YES];
Where parentController is a reference to the vc from where you are presenting the modal view.
The main functionality of my app is controlled by a UITabBarController. However, I need to load a View that has a UINavigationController. When I return to my UITabBarController using
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
My UITabBarController no longer responds to clicks. It seems like the View does not have focus.
However, if I use this code to switch back to the UITabBarController:
[window addSubview:tabBarController.view]
My buttons will respond. I feel like "addSubview" is less efficient because I never remove the view from the window and therefore it must be adding a second copy of the view to the stack. Am I correct? Is there a way to use the first method and make my buttons respond? Please let me know.
It sounds like maybe you're presenting the Nav Controller incorrectly. You definitely shouldn't be adding views directly to the window. You want to present it using
[myTabBarController presentModalViewController:myNavController animated:YES];
When you're done with the nav controller you dismiss it with
[myTabBarController dismissModalViewControllerAnimated:YES];
and everything should work.
BTW, this is all documented in the docs for UIViewController and the "View Controller Programming Guide for iPhone OS" document.