在按钮水龙头上切换UIViewControllers?是的,我认为iOS可能有这样的壮举!
粗略浏览一下Xcode中实用程序应用程序的模板,显示了三个主要类,即应用程序委托,主视图控制器和flipside视图控制器。在应用程序委托中,窗口的根视图控制器被实例化并在应用程序完成启动时进行设置。事情是这样的:
MyRootViewController *myvc = [[MyRootViewController alloc] initWithNibName:@"MyRootView" bundle:nil];
self.window.rootViewController = myvc;
[myvc release];
在MyRootViewController,你会看到一个方法,可能-(IBAction)showInfo:(id)sender
,它实例和模态呈现不利的一面视图控制器。这个消息是通过点击一个连接在.xib文件中的按钮发送的。
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,它的视图以模态方式显示在屏幕上。
要切换回来,flipside控制器会发送它的委托(MyRootViewController)-(IBAction)flipsideViewControllerDidFinish:(id)sender
消息,通知它可以将视图翻转回来。在该方法中,MyFlipsideViewController被解雇。
可能有很多种方法可以实现您描述的应用程序(比我要描述的要好得多),但是如果您想模仿实用程序应用程序模板,则可以创建一个根视图控制器它充当一系列其他视图控制器的代表。它应该有一个像-showInfo:(id)sender
这样的方法,但如果它能够显示基于按钮按下的不同控制器,那将会很不错。你可以做到这一点的方法之一是通过给每个按钮一个特定的标签,然后使用switch
,像这样:
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];
可以使用MyFlipsideViewController与各种基于按钮标签上的不同意见,或者您可以实例不同的控制器并呈现它们 - 只要确保它们具有指向MyRootViewController的委托属性即可。
在视图之间移动真的是iOS编程的面包和黄油。我个人推荐iOS Programming: The Big Nerd Ranch Guide - 我认为阅读它会让你对iOS编程中的MVC模式有一个很好的理解。
我不是专家,但(因为任何人阅读上面的代码可以告诉),所以我会说相信我的书。尤其是当涉及到这些发布呼叫时。