开发者

Using Utility flip as a menu for mulitple controllers

So I'm fairly new to xcode, and I'm still trying to figure out how all the controllers work together. I'm currently writing an app that has multiple screens, like you would organize with a tabcontroller. However, i don't actually have room on my screen for the tab at the bottom. Looking around at the other templates, I found the utility application starter code. I really like how it just has the little i at the bottom and then flips around to an entirely different controller.

Is it possible to use开发者_如何转开发 the flipController as a menu(with icons like the home screen), and flip back to any one of a number of controllers based on what was pressed? I know that if it's possible, It'd have to do with code in the delegator, but so far I haven't been able to find anything on the internet, and I haven't had any luck tinkering with it.

Any help would be greatly appreciated.


Switching UIViewControllers on a button tap? Yeah, I think iOS might be capable of such a feat!

A cursory glance at the template for a Utility Application in Xcode shows three main classes, the app delegate, the main view controller, and a flipside view controller. In the app delegate, the root view controller for the window is instantiated and set when the application finishes launching. Something like this:

MyRootViewController *myvc = [[MyRootViewController alloc] initWithNibName:@"MyRootView" bundle:nil];
self.window.rootViewController = myvc;
[myvc release];

In MyRootViewController, you'll see a method, probably -(IBAction)showInfo:(id)sender, which instantiates and modally presents the flipside view controller. This message is sent by tapping a button, connected in the .xib file.

MyFlipsideViewController *mfvc = [[MyFlipsideViewController alloc] initWithNibName:@"MyFlipsideView" bundle:nil];
mfvc.delegate = self; // by setting the root view controller as the delegate, 
                      // your flipside controller can send messages to MyRootViewController
mfvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; // set how the controller's view should be displayed
[self presentModalViewController:mfvc animated:YES];
[mfvc release];

MyFlipSideViewController is presented, and its view is displayed modally on screen.

To switch back, the flipside controller sends its delegate (MyRootViewController) the -(IBAction)flipsideViewControllerDidFinish:(id)sender message, informing it that it can flip the view back. In that method, MyFlipsideViewController is dismissed.

There are probably tons of ways to implement the kind of application you describe (many way better than what I'm about to describe), but if you want to mimic the utility application template, you can make a root view controller which acts as a delegate to a series of other view controllers. It should have a method like -showInfo:(id)sender, although it would be nice if it could show different controllers based on the button pressed. One way you can do this is by giving each button a specific tag, then using a switch, like so:

MyFlipsideViewController *controller = nil;
switch ([sender tag]) {
    case 1:
        controller = [[MyFlipsideViewController alloc] initWithNibName:@"OneFlipsideView" bundle:nil];
        break;
    case 2:
        controller = [[MyFlipsideViewController alloc] initWithNibName:@"AnotherFlipsideView" bundle:nil];
        break;
    default:
        @throw [NSException exceptionWithName:@"Unrecognized object" 
                                       reason:@"I don't know how to handle a button with that tag." 
                                     userInfo:nil];
        break;
}
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];

You can use MyFlipsideViewController with a variety of different views based on the button tag, or you can instantiate different controllers and present them--just make sure they have a delegate property which points at MyRootViewController.

Moving between views is really the bread and butter of iOS programming. I personally recommend iOS Programming: The Big Nerd Ranch Guide--I think reading it will give you a very solid understanding of the MVC pattern in iOS programming.

I'm no expert, though (as anyone reading the code above can tell), so I'd say trust the book over me. Especially when it comes to those release calls.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜