views:

1069

answers:

3

Hello

Im new to iPhone development and I have really taken this to me. I love it, but there is one thing thats naggin' me. How do I keep switching view? I know how to come from first view that is given to me when I create a new project, to a view that I make, but how do I get passed this two windows? How do I get from to views that I created?

I have this app which have a main window with a NavigationController whih is feed with a UITableViewController. This is my main menu. I have a in the upper right corner, a "+"-button which gives me a new view, but how do I get a new view from here? How do I push a new view when the user pick something to add?

Hope someone understand my question. A link to some documentation would be fine. I have looked everywhere.

+1  A: 

For that particular situation, people usually use the presentModalViewController:animated: method of UIViewController. UINavigationController is a subclass of UIViewController, so your code would look something like this:

UIViewController *addingViewController = [[UIViewController alloc] initWithNibName:@"AddingView" bundle:nil];
[[self navigationController] presentModalViewController:addingViewController animated:YES];
[addingViewController release];
Sebastian Celis
Ah this is how Im doing it. I have some problems with Xcode, so I have solved it now. I can move beyond the starrt view and my own created view. I thought in some mysterious way that I had to create a xib and then pass it (and in that way I couldnt se how to create a table like i did before). But you got me on the track.
mslot
+3  A: 

You can do this many diffrent way, you can do what the Sebastian said, you can also have a common RootViewController that manages your other view controllers view. This is what I like to do, I actually define a protocol on the RootViewController something like ToggleView:UIViewController newController UIViewController:oldController. I make any UIViewController that i want to be able to switch from that view to another implement this protocol. This makes since because generally when you click on a button, you know what View you want to go to next. So when a user clicks the button, in the UIViewController that owns the button i create the new ViewController whose view i want pushed into the screen, this is nice because it also allows me to set up data in the view controller and not have to delegate it to some other object or use a singleton to get the data in the new view, then i call my toggleView methods and the root view controller does the switching. I find this works pretty well and theres berely any code involved. I dont always u se this though, if I know a new view will always come out of another particular view, (for example a home page where one views events and creation of those events), in this case I will loosly couple the view controllers by using protocols.

Daniel
Interesting point. Can you show me an example? Im not that into Obj-C, but Im starting to like it.
mslot
+1  A: 

Here is the rootviewcontrollerdelegate definition

@protocol RootViewControllerViewDelegate

-(void)toggleView:(UIViewController )newController viewController:(UIViewController)oldController;

@end

a possible implementation to toggleView

-(void)toggleView:(UIViewController *)newController viewController:(UIViewController*)oldController {   
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:([oldController.view superview] ?  UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromLeft) forView:self.view cache:YES];
[newController viewWillAppear:YES];
[oldController viewWillDisappear:YES];
[oldController.view removeFromSuperview];
[self.view addSubview:newController.view];
[oldController viewDidDisappear:YES];
[newController viewDidAppear:YES];
[UIView commitAnimations];
[oldController release];

}

This will swipe the view controllers by flipping the view

Obviously you must make a new RootViewController somewhere and start with a view there, (could be the app delegate)

Now if you want a ViewController to be able to use the RootViewController it must conform to the protocol, you declare it in that classes interface like so

@interface MyViewController : UIViewController <RootViewControllerDelegate> {
id delegate;
}@property(assign) id <RootViewControllerViewDelegate> delegate;

Now you can use the delegates method to swap a view for another given that everything has been initialized right. the code to swap two controllers view could look like this

    NewViewController *viewController=...
    //you can set up your viewControllers data here if you need to
   //Since its probable that this view has that data it can just set it instead of
   //delegating 
viewController.delegate=delegate; //setting up the RootViewController reference
[delegate toggleView:viewController viewController:self];

remember on the toggleView call back to release the old ViewController, if you dont youll get a leak since you lose all reference to that controller.

Daniel