当前位置:编程学习 > wap >>

iPhone开发进阶(7)--- 利用ModalViewController切换View

 

当程序中含有多个view,需要在之间切换的时候,可以使用UINavigationController,或者是ModalViewController。UINabigationController 是通过向导条来切换多个view。而如果view 的数量比较少,且显示领域为全屏的时候,用ModalViewController 就比较合适(比如需要用户输入信息的view,结束后自动回复到之前的view)。今天我们就看看ModalViewController 的创建方法。

 

ModalViewController 并不像UINavigationController 是一个专门的类,使用UIViewController 的presentModalViewController 方法指定之后就是ModalViewController 了。

 

这里使用上两回做成的CustomViewController(由UIViewController继承)来实现ModalViewController 的实例。

 

首先,准备ModalViewController 退出时的函数。调用UIViewController 的dismissModalViewController:Animated: 方法就可以了,如下所示:

// 这里按钮按下的时候退出ModalViewController

-(void)dismiss:(id)inSender {

    //  如果是被presentModalViewController 以外的实例调用,parentViewController 将是nil,下面的调用无效

    [self.parentViewController dismissModalViewControllerAnimated:YES];

}

接下来,生成另一个CustomViewController 的实例,用来表示ModalViewController,并将其对应的view 设置成红色。然后传递给presentModalViewController: Animated: 显示ModalViewController 的view。

 

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    controller = [[CustomViewController alloc] init];

    [window addSubview:controller.view];

    [window makeKeyAndVisible];

    // 生成ModalViewController

    CustomViewController* controllerB = [[CustomViewController alloc] init];

    // 设置view 的背景为红色

    controllerB.view.backgroundColor = [UIColor redColor];

    // 显示ModalViewController view

    [controller presentModalViewController:controllerB animated:YES];

    // presentModalViewController 已经被controller 管理,这里可以释放该实例了

    [controllerB release];

}

编译执行以后,首先启动的是红色背景的ModalViewController view、按下按钮后恢复到蓝色背景的通常view 上。

 

也可以在显示ModalViewController view 之前设置UIViewContrller 的modalTransitionStyle 属性,使其以动画形式显示。

 

1

controllerB.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

以上的实现只是单一地实现了ModalViewController view 的功能,除了程序开始提醒用户一些信息外什么也做不了。另外由于是放入了applicationDidFinishLaunching 中的原因,也不能反复的显示。另外,在ModalViewController view 上设置的内容也不能反映到原来的view 上。

 

 

接下来我们将实现这些功能。

 

首先,从ModalViewController view 退出的时候,需要通知原先的view。这里使用iPhone/Cocoa 应用程序中经常使用的Delegate 设计模式(也是推荐使用的)。

 

实际上,系统所提供的图像选择控制类UIImagePickerController

或者是参照地址簿时的ABPeoplePickerNavigationController 类,都用到了Delegate 模式。

基于上一讲的中的例子,这里我们追加为3个按钮,分别是绿色,灰色和取消。

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blueColor];

    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button.frame = CGRectMake(100,100,100,100);

    button.tag = 1;

    [button setTitle:@"绿色" forState:UIControlStateNormal];

    //  按钮事件对应函数

    [button addTarget:self action:@selector(dismiss:)

        forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

 

    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button.frame = CGRectMake(100,200,100,100);

    button.tag = 2;

    [button setTitle:@"灰色" forState:UIControlStateNormal];

    //  按钮事件对应函数

    [button addTarget:self action:@selector(dismiss:)

        forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

 

    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button.frame = CGRectMake(100,300,100,100);

    button.tag = 0;

    [button setTitle:@"取消" forState:UIControlStateNormal];

    //  按钮事件对应函数

    [button addTarget:self action:@selector(dismiss:)

        forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

}

程序启动的时候依然是先显示ModalViewController view,按下任何一个按钮,将关闭该view。按下“绿色”按钮,设置背景为绿色,按下“灰色”按钮时,设置背景为灰色。“取消”的时候什么也不做。

 

委托处理用下面的函数实现,当参数inColor 为nil 的时候代表取消。

 

-(void)selectColor:(UIColor*)inColor;

委托代理的实例用id 变量表示。

 

@interface CustomViewController : UIViewController {

    id  colorSelectDelegate;

}

设置该变量的函数如下。

-(void)setColorSelectDelegate:(id)inDelegate {

    colorSelectDelegate = inDelegate;

}

另外如上面viewDidLoad 所示,按钮的tag 分别为0、1、2。按钮按下时调用的函数中由不同的tag 来发送不同的UIColor实例到colorSelectDelegate 上。

 

-(void)dismiss:(id)inSender {

    UIView* view = (UIView*)inSender;

    UIColor* requestColor = nil;

    if (view.tag == 1)

        requestColor = [UIColor greenColor];

    if (view.tag == 2)

        requestColor = [UIColor grayColor];

    [colorSelectDelegate selectColor:requestColor];

}

这是不使用UIButton* 而是用UIView* ,是因为tag 属性被定义在UIView 类中,不需要必须转换为UIButton 类。

另外这样一来,该函数在UIButton 以外的情况下也能被使用。

如果想检查id 是什么类性的可以使用isKindOfClass: 方法。

接收到具体的参数inColor 更换背景色,并关闭ModalViewController view。

 

-(void)selectColor:(UIColor*)inColor {

    if (inColor != nil)

        self.view.backgroundColor = inColor;

    [self dismissModalViewControllerAnimated:YES];

}

另外,在调用presentModalViewController 之前(显示ModalViewController view 之前),需要设定委托的实例。

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    controller = [[CustomViewController alloc] init];

    [window addSubview:controller.view];

    [window makeKeyAndVisible];

    //  创建ModalViewController view 的Controller

    CustomViewController* controllerB = [[CustomViewController alloc] init];

    // 

补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,