Iphone對話方塊工具類詳解

來源:互聯網
上載者:User

在項目中我把對話方塊的彈出與點擊事件通過一個工具類給封裝起來了,重要的是要在調用這個工具類的時候實現工具類中的協議,這個協議的方法是為了告訴頁面到底點擊了哪種對話筐的哪一個button,然後頁面中可以根據傳過去的不同值做出不同的事件響應,代碼如下:
.h檔案
[cpp]
#import <Foundation/Foundation.h> 
 
@protocol myAlertviewutilDelegate <NSObject> 
 
-(void)myalertviewbuttonclik:(int)num; 
 
@end 
 
@interface AlertViewUtil : NSObject<UIAlertViewDelegate> { 
    //自訂的對話筐協議 
    id<myAlertviewutilDelegate> utildelegate; 
    UIAlertView *myonealertview;//一個按鈕的對話筐 
    UIAlertView *mytwoalertview;//兩個按鈕的對話筐 
    UIAlertView *mythreealertview;//三個按鈕的對話筐 

 
@property(nonatomic,retain) id<myAlertviewutilDelegate> utildelegate; 
 
 
-(void)alertviewutilone:(NSString *)buttontitleone addmessage:(NSString *)message; 
-(void)alertviewutiltwo:(NSString *)buttontitleone twobutton:(NSString *)buttontitletwo addmessage:(NSString *)message; 
-(void)alertviewutilthree:(NSString *)buttontitleone twobutton:(NSString *)buttontitletwo threebutton:(NSString *)buttontitlethree addmessage:(NSString *)message; 
@end 

.m檔案
[cpp]
#import "AlertViewUtil.h" 
 
 
@implementation AlertViewUtil 
@synthesize utildelegate; 
 
-(void)dealloc{ 
    [utildelegate release]; 
    [super dealloc]; 

//實現系統對話筐協議中的button點擊方法 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    //點擊的是第一個alertview 
    if (alertView == myonealertview) { 
        if (buttonIndex == 0) { 
            [self.utildelegate myalertviewbuttonclik:11]; 
        } 
            [myonealertview release]; 
    } 
    //點擊的是第二個alertview 
    else if (alertView == mytwoalertview) { 
        if (buttonIndex == 0) { 
              [self.utildelegate myalertviewbuttonclik:21]; 
        } 
        else if(buttonIndex == 1){ 
            [self.utildelegate myalertviewbuttonclik:22]; 
        } 
        [mytwoalertview release]; 
    } 
    //點擊的是第三個alertview 
   else if (alertView == mythreealertview) { 
        if (buttonIndex == 0) { 
            [self.utildelegate myalertviewbuttonclik:31]; 
        } 
        else if(buttonIndex == 1){ 
            [self.utildelegate myalertviewbuttonclik:32]; 
        } 
        else if(buttonIndex == 2){ 
            [self.utildelegate myalertviewbuttonclik:33]; 
        } 
             
            [mythreealertview release]; 
    } 
      

 
//只有一個按鈕 的alertview 
-(void)alertviewutilone:(NSString *)buttontitleone addmessage:(NSString *)message{ 
    myonealertview = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:nil otherButtonTitles:buttontitleone, nil]; 
    [myonealertview show]; 

 
//兩個按鈕 的alertview 
-(void)alertviewutiltwo:(NSString *)buttontitleone twobutton:(NSString *)buttontitletwo addmessage:(NSString *)message{ 
    mytwoalertview = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:nil otherButtonTitles:buttontitleone,buttontitletwo, nil]; 
    [mytwoalertview show]; 

 
//三個按鈕 的alertview 
-(void)alertviewutilthree:(NSString *)buttontitleone twobutton:(NSString *)buttontitletwo threebutton:(NSString *)buttontitlethree addmessage:(NSString *)message{ 
    mythreealertview = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:nil otherButtonTitles:buttontitleone,buttontitletwo,buttontitlethree, nil]; 
    [mythreealertview show]; 

 
@end 



摘自 RiverAM的專欄

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.