zoukankan      html  css  js  c++  java
  • 【转】iPhone 模态对话框 立即返回结果

    原文:http://blog.csdn.net/xianpengliu/article/details/6591624 

    iPhone中的UIAlertView用于显示一个模态对话框

    显示时设置代理delegate,当用户点击对话框中按钮时,系统将会调用delegate的函数

    从而使得程序可以根据用户的选择进行相应的处理

    这里使用了代理模式,虽然代理模式在ios的设计中有很多优雅的地方

    但是这里,用在返回模态对话框的结果,未免有点儿不合时宜

    每次用到这个,我就非常怀念MFC中的模态对话框:

    1. ReturnValue ret = dlg.doModal();  
    2. if (ret == x) {  
    3.     ...  
    4. else {  
    5.     ...  
    6. }  

    下面,封装一个类来实现这种简介的操作(代码摘自《iPhone开发秘籍》):

    1. #import <UIKit/UIKit.h>  
    2.   
    3. @interface ModalAlert : NSObject  
    4. + (BOOL) ask: (NSString *) question;  
    5. + (BOOL) confirm:(NSString *) statement;  
    6. @end  
    1. #import "ModalAlert.h"  
    2.   
    3. @interface ModalAlertDelegate : NSObject <UIAlertViewDelegate>  
    4. {  
    5.     CFRunLoopRef currentLoop;  
    6.     NSUInteger index;  
    7. }  
    8. @property (readonly) NSUInteger index;  
    9. @end  
    10.   
    11. @implementation ModalAlertDelegate  
    12. @synthesize index;  
    13.   
    14. // Initialize with the supplied run loop  
    15. -(id) initWithRunLoop: (CFRunLoopRef)runLoop   
    16. {  
    17.     if (self = [super init]) currentLoop = runLoop;  
    18.     return self;  
    19. }  
    20.   
    21. // User pressed button. Retrieve results  
    22. -(void) alertView: (UIAlertView*)aView clickedButtonAtIndex: (NSInteger)anIndex   
    23. {  
    24.     index = anIndex;  
    25.     CFRunLoopStop(currentLoop);  
    26. }  
    27. @end  
    28.   
    29. @implementation ModalAlert  
    30. +(NSUInteger) queryWith: (NSString *)question button1: (NSString *)button1 button2: (NSString *)button2  
    31. {  
    32.     CFRunLoopRef currentLoop = CFRunLoopGetCurrent();  
    33.       
    34.     // Create Alert  
    35.     ModalAlertDelegate *madelegate = [[ModalAlertDelegate alloc] initWithRunLoop:currentLoop];  
    36.     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:question message:nil delegate:madelegate cancelButtonTitle:button1 otherButtonTitles:button2, nil];  
    37.     [alertView show];  
    38.       
    39.     // Wait for response  
    40.     CFRunLoopRun();  
    41.       
    42.     // Retrieve answer  
    43.     NSUInteger answer = madelegate.index;  
    44.     [alertView release];  
    45.     [madelegate release];  
    46.     return answer;  
    47. }  
    48.   
    49. + (BOOL) ask: (NSString *) question  
    50. {  
    51.     return  [ModalAlert queryWith:question button1: @"No" button2: @"Yes"];  
    52. }  
    53.   
    54. + (BOOL) confirm: (NSString *) statement  
    55. {  
    56.     return  [ModalAlert queryWith:statement button1: @"Cancel" button2: @"OK"];  
    57. }  
    58. @end  
    这样,就可以使用一行代码显示模态对话框并获得返回值了:

    1. NSUInteger answer = [ModalAlert ask:@"Are you sure?"];  
  • 相关阅读:
    C# 文件类的操作---删除
    C#实现Zip压缩解压实例
    UVALIVE 2431 Binary Stirling Numbers
    UVA 10570 meeting with aliens
    UVA 306 Cipher
    UVA 10994 Simple Addition
    UVA 696 How Many Knights
    UVA 10205 Stack 'em Up
    UVA 11125 Arrange Some Marbles
    UVA 10912 Simple Minded Hashing
  • 原文地址:https://www.cnblogs.com/jacktu/p/2247255.html
Copyright © 2011-2022 走看看