带返回值的模态窗口UIAlertView

转自:  http://www.cocoachina.com/bbs/read.php?tid-60136.html


建议去上面的网页浏览,转载时,csdn不支持图片

带返回值的模态窗口UIAlertView   

在使用C#、VC编程中总是会 使用到模态窗口,它的简单使用方法如下
某个函数内
ret = theForm.showDialog();    // 阻塞在此,直到退出窗体,尤其是那些有“取消”、“确定”等的窗体
switch (ret)
{
    // 根据返回结果继续下一步动作
}

虽然使用UIAlertView也可以解决 问题,但是其编程方法太麻烦,因此对其进行了一个封装。

定义头 文件
复制代码
  1. //  DialogUIAlertView.h
  2. #import <Foundation/Foundation.h>
  3. @interface DialogUIAlertView : NSObject {
  4.     BOOL isWaiting4Tap;
  5.     UIAlertView * alv;
  6.     int alertViewRetValue;
  7.     
  8. }
  9. - (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;
  10. - (int)showDialog;
  11. @end


定义实现文件
复制代码
  1. //  DialogUIAlertView.m
  2. #import "DialogUIAlertView.h"
  3. @implementation DialogUIAlertView
  4. - (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
  5. {
  6.     alv = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles,nil];
  7.     
  8.     isWaiting4Tap = YES;
  9.     return self;
  10. }
  11. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  12. {
  13.     alertViewRetValue = buttonIndex;
  14.     isWaiting4Tap = NO;
  15. }
  16. - (int)showDialog
  17. {
  18.     isWaiting4Tap = YES;
  19.     [alv show];
  20.     while (isWaiting4Tap) {
  21.         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  22.     }
  23.     return alertViewRetValue;
  24. }
  25. - (void)dealloc {
  26.     [super dealloc];
  27.     [alv release];
  28. }
  29. @end


测试用例
1.单击 按钮,弹出AlertView
2.等待AlertView返回
3.AlertView返回之后,打印用户选择结果

复制代码
  1. -(void) btnclick:(id)sender
  2. {
  3.     NSLog(@"DiaglogUIAlertView Started");
  4.     UIButton *b = sender;
  5.     b.backgroundColor = [UIColor blueColor];
  6.     DialogUIAlertView *malv = [[DialogUIAlertView alloc] initWithTitle:@"DialogUIAlertView" message:@"This is a Modal type UIAlertView which returns an integer indicating the button tapped" cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
  7.     int ret = [malv showDialog];
  8.     [malv release];
  9.     switch (ret) {
  10.         case 0:
  11.             NSLog(@"button0, Cancel");
  12.             break;
  13.         case 1:
  14.             NSLog(@"button1, OK");
  15.             break;
  16.         default:
  17.             break;
  18.     }
  19.     
  20.     b.backgroundColor = [UIColor blackColor];
  21.     
  22.     NSLog(@"DiaglogUIAlertView Ended");
  23. }



原理
使用 NSRunLoop,在等待用户结果返回之前,在自己的进程内像UIApplicationMain一样处理 事件,使得自己阻塞,而其他的事件处理不阻塞。

做法:在有退出条件的循环内调用 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]
可以使用类似的方法做自定义的带返回结果的模态窗体了

不足
由于不知道UIAlertView有没有实现支持 va_arg,目前的这个AlertView只能支持2个按钮


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值