2011-09-29 101 views
0

因此,我对xcode相当陌生,而且我仍在试图弄清楚所有控制器如何协同工作。我目前正在编写一个具有多个屏幕的应用程序,就像您使用tabcontroller进行组织一样。但是,我实际上并没有在屏幕上为底部的选项卡留出空间。环顾其他模板,我找到了实用程序应用程序的入门代码。我真的很喜欢它是如何将小我置于底部,然后翻转到完全不同的控制器。使用实用程序翻转作为多个控制器的菜单

是否可以使用flipController作为菜单(带有像主屏幕一样的图标),并根据按下的内容将其翻转到多个控制器中的任何一个?我知道,如果可能的话,它将与代理人的代码有关,但到目前为止,我还没有能够在互联网上找到任何东西,我没有任何运气修补它。

任何帮助将不胜感激。

回答

0

在按钮水龙头上切换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模式有一个很好的理解。

我不是专家,但(因为任何人阅读上面的代码可以告诉),所以我会说相信我的书。尤其是当涉及到这些发布呼叫时。

相关问题