I'm new to iPhone development (except developing with cocos2d).
I want to create a simple application that have one window with a button. When i press the button i want some other window to be shown.
Where can i read how do such things?
Also i don't understand well what is View, ViewController, Window. I've read the your first iOS app example.
Look for tutorials on UINavigationController, like this one.
For the meaning of view and view controller you certainly want to read the apple references or in wikipedia. The topic there would be MVC Pattern.
As to your concrete problem:
There is usually only one window in iPhone apps so you certainly want to have a button on a view and if you push that button that view disappears and instead a new view is shown.
You accomplish that by removing the view with the button from its superview ( have a look at the topic tree hierarchy ) and then add the view you want to bee shown as a subview to the main window .
Bottom line is there is one main window and you put views onto it by it's addSubview method. And you remove views by calling their removeFromSuperview method
You should to read it again or google it until you'll understand it well.
view is the graphic output, while view controller is what "manage" the behavior of the view in every event.
your function to navigate -
(IBAction) ButtonClicked
{
static YourViewController *viewController=nil;
if(viewController==nil)
viewController=[[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
[self.navigationController pushViewController:viewController animated:YES];
}
Related
I started my application as a navigation based application, and did the main bulk of the work using the tables etc. Now, I wish to create a start view page, consisting of two buttons, one which links to the RootViewController and the other which links to another view.
Is this possible? If so, how would I go about it?
Thanks!
Make a new UINavigationController with your startview as the rootViewController for the window first.
Then on the click event of the first button, make a delegate call to your appDelegate class and remove the present UINavigationController and add the main UINavigationController (which links to your RootViewController) as the rootViewController for the window.
For the second buttons click event you can simply push the next view to the navigationController.
Still learning Obj-C slowly... forgive the dumb questions...
On my 1st XIB I have the App Delegate, Nav Controller and several view controllers. Along with that I have several buttons that calls a 2nd or 3rd or subsequent XIB.
The subsequent XIBS all have buttons which display views.
So on the 2nd+ XIB I have configured it in the .h as an UIViewController however I am guessing I need to make it something else like the primary .h is an AppDelgate.
So right now the XIB wants the view set, but I don't want it to go to a view, I want it to go to the view controller... I think??
Maybe I am still going about this all wrong. I need the primary menu to call the next menu (2nd XIB) which in turn calls various views. In my Java Android app I have about 70 classes, and guess and about 45 views so I am guessing again that I do in fact need the multiple XIBS.
So the question is how do I set up the additional XIBs? Are they AppDelegates or what?
Does that change the way I call the 2nd XIB?
The XIBs or the UIViews (or it's subclasses) are just the facial make up.
For the actually programming part, you deal directly among the "controllers" classes for these views.
View controllers that you make can have an XIB attached to them. But the behavior of how and when the view is shown or hidden, is all handled by the view controller itself.
So to come to the point, if you want to have a navigation bar on the top of you app (assuming that it's a simple app wanting to show many views with a navigation bar):
Create a UINavigationController instance in your applicationDidFinishLaunching: method in the app delegate:
// Assuming that mainViewController is the first controller + view for your app.
navigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
[window addSubview:navigationController.view];
This will automatically add a navigation bar to your views. You don't need to add them manually in XIB or anywhere else. Now how you draw/implement mainViewController, is up to you.
When you want to show another view from within mainViewController, you should call:
AnotherViewController *anotherViewController = [[[AnotherViewController alloc] init] autorelease];
[self.navigationController pushViewController:anotherViewController animated:YES];
This will "push" your new view (from anotherViewController instance) into your navigation structure, which will automatically add a back button on the top.
Hope this helps clear the scene of how it works, a bit.
If you have doubts, comment about it. Have a great day!
I've been reading the Head First iPhone Development book and I understand how to get to a new view from a table but how exactly would I be able to get to a new view or view controller, by just simply pressing a button? Is that even possible?
I mean there are some apps where you click a button, not a table cell and it loads a new view. How exactly is that done? If someone could help out a newbie it would be greatly appreciated!
I think what you're looking for is a modal vew controller. THis presents a modal view like you described on top of everything else. If rootViewController is the view controller that is displaying your current view, and myNewViewController the view controller you want to display modally:
[rootViewController presentModalViewController:myNewViewController animated:YES];
There's plenty of examples of this kind of thing on the net, just search for presentModalViewController
Like bpapa said in the comments, it's hard to be specific without code. However, generally what you want to do is:
Build a navigation controller that contains one original view.
Create a button in your original view using the Interface Builder.
Build a callback method (usually defined with IBAction) that is run when the button is pushed.
In that callback method, create a new view and push it onto the navigation controller the same way you would using a table view cell.
Alternately, if you only want one level of hierarchy, you could use a modal view controller; instead of pushing onto the navigation controller in the last step, just present the modal view controller.
The general answer is that you have an object that manages which view controller loads when.
The most commonly used is the UINavigationController. It is a UIViewController that instead of controlling views, controls other view controllers. It works like a simple stack. You push views you want to display onto the nav's controller stack and when you want them to disappear you pop them off.
A common (though sloppy) way of using a nav is to make it a property of your app delegate. Then anywhere in your app you can references it by:
UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];
The view controller for the first the user sees is held in the nav's topViewController property. If you want to load a view based on a user action in the topViewController.view, you would have something like this:
- (IBAction) loadNextView:(id) sender{ // Action called by a a UI event such as a button press.
UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];
UIViewController *nextViewController=...// load from nib, connect with IBOutlet, create programmatically
[nav pushViewController:nextView animated:YES];
}
The first view disappears to be replaced by the next one. To return to the first view, you have a method in the next view controller like so:
- (IBAction) unloadSelf:(id) sender{ // Action called by a a UI event such as a button press.
UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];
[nav popViewControllerAnimated:YES];
}
... and the nav returns you automatically to the previous view regardless of what that view was.
When you first start out, especially if you use Interface Builder, the structure of the app is largely hidden. Behind the scenes all view controllers and their views exist in a hierarchy of some kind that leads back up to the app delegate. You should train yourself to think in hierarchal terms even if it is not immediately obvious how that hierarchy is constructed.
I am new to iphone development.I have created a button in UIView controller.On clicking the button it navigates to a another UITableView controller .On clicking the row of the table leads to another UIView controller which has also another table in it.On clicking the row of this table leads to another UIWebView.I have created a custom back button placed on the navigation bar of the UIView controllers and UIWebview.On clicking on the back button just leads me to the main page with a button and not to its just previous UIView.
-(IBAction) goemenbutton : (id) sender
{
[self.view removeFromSuperview];
}
The above method is used for all the custom back button.Please help me out to get the correct flow for the back button.Thanks.
You should probably look at using UINavigationController instead of interchanging several views within one UIViewController. The UINavigationController takes care of a lot of things for you, including back buttons with proper titles and nice animations. It also helps you structure your code and gives you more flexibility down the road.
A UIWebview is not a UIViewController and can not be pushed onto the NavigationController. You should probably look at wrapping it into a UIViewController.
I'm developing a menu for a game. The menu contains four buttons; Campaign, Training, Highscore and Settings.
When I click one of these buttons, a new view controller should be shown. I add the new view controller using this function:
[self.view addSubview:myViewController.view];
It does show the new view controller, but I can still touch the menu buttons from the other view controller that is "behind" the new. I can't see the buttons, but when I touch in the area of a button, the button's IBAction is called.
Of course, I can disable all buttons before adding the subview, but is this the right way to fix it? Should I remove the menu from its subview? I'm using a window-based application and do not have a tab bar or a navigation bar.
Thank you very much!
You could use a modal view:
[self presentModalViewController:myViewController animated:YES];
I think that would be the right way to do this.
You need to remove the old view from the screen when you add the new one. Three ways to do this, from my most preferred to least preferred:
Use a navigation controller and just turn off the navigation bar (by setting self.navigationController.navigationBarHidden to YES).
Use presentModalViewController:animated:, as subw recommends.
Do the following instead of what you're doing currently:
[self.view.window addSubview:myViewController.view];
[self.view removeFromSuperview];
There are two main differences between the three approaches.
One is the animation involved. #1 slides the new screen in from the right. #2 slides it up from the bottom (or cross-dissolves or flips, depending on your preference). #3 does not have any animation by default.
The other is how you go back to the menu. In #1, any part of your code (more or less) can call ([self.navigationController popViewControllerAnimated:YES]. In #2, the child view controller calls [self.parentController dismissModalViewControllerAnimated:YES]. #3 has no simple way to go back; you have to do it manually, which involves keeping a reference to the menu view controller in each of the other view controllers.
The only problem with what you're doing now is that the touches are getting passed along rather than handled by your new view. I would go one level above the menu, remove the menu and add the new view. If you need the menu back, do the opposite. I tend to use presentModalViewController for short things, like preferences, but for the full game I'd unload the menu. It's your call though.