) UIALertView的基本用法與UIAlertViewDelegate對對話方塊的事件處理方法,uialertviewdelegate

來源:互聯網
上載者:User

(轉) UIALertView的基本用法與UIAlertViewDelegate對對話方塊的事件處理方法,uialertviewdelegate

首先,視圖控制器必須得實現協議UIAlertViewDelegate中的方法,並指定delegate為self,才能使彈出的Alert視窗響應點擊事件。具體代碼如下:
#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UIAlertViewDelegate>@end

ViewController.m中的詳細代碼:

- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view from its nib        //初始化AlertView    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertViewTest"                                                   message:@"message"                                                  delegate:self                                         cancelButtonTitle:@"Cancel"                                         otherButtonTitles:@"OtherBtn",nil];    //設定標題與資訊,通常在使用frame初始化AlertView時使用    alert.title = @"AlertViewTitle";    alert.message = @"AlertViewMessage";        //這個屬性繼承自UIView,當一個視圖中有多個AlertView時,可以用這個屬性來區分    alert.tag = 0;    //唯讀屬性,看AlertView是否可見    NSLog(@"%d",alert.visible);    //通過給定標題添加按鈕    [alert addButtonWithTitle:@"addButton"];    //按鈕總數    NSLog(@"number Of Buttons :%d",alert.numberOfButtons);    //擷取指定索引的按鈕標題    NSLog(@"buttonTitleAtIndex1:%@",[alert buttonTitleAtIndex:1]);    NSLog(@"buttonTitleAtIndex2:%@",[alert buttonTitleAtIndex:2]);    //擷取取消按鈕的索引    NSLog(@"cancelButtonIndex:%d",alert.cancelButtonIndex);    //擷取第一個其他按鈕的索引    NSLog(@"firstOtherButtonIndex:%d",alert.firstOtherButtonIndex);    //顯示AlertView    [alert show];    [alert release];}#pragma marks -- UIAlertViewDelegate --//根據被點擊按鈕的索引處理點擊事件-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    NSLog(@"clickButtonAtIndex:%d",buttonIndex);}//AlertView已經消失時執行的事件-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{    NSLog(@"didDismissWithButtonIndex");}//ALertView即將消失時的事件-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{    NSLog(@"willDismissWithButtonIndex");}//AlertView的取消按鈕的事件-(void)alertViewCancel:(UIAlertView *)alertView{    NSLog(@"alertViewCancel");}//AlertView已經顯示時的事件-(void)didPresentAlertView:(UIAlertView *)alertView{    NSLog(@"didPresentAlertView");}//AlertView即將顯示時-(void)willPresentAlertView:(UIAlertView *)alertView{    NSLog(@"willPresentAlertView");}- (void)viewDidUnload{    [super viewDidUnload];    // Release any retained subviews of the main view.    // e.g. self.myOutlet = nil;}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.