现在的位置: 首页 > 综合 > 正文

带返回值的模态窗口UIAlertView

2013年08月27日 ⁄ 综合 ⁄ 共 2481字 ⁄ 字号 评论关闭

转自:  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个按钮

抱歉!评论已关闭.