IOS-UIAlertView和UIActionSheet

來源:互聯網
上載者:User

IOS-UIAlertView和UIActionSheet
IOS中兩大控制項:UIAlertView和UIActionSheet

UIAlertView是在螢幕中央彈出一個訊息框,該訊息框可以用來做訊息提示,也可以讓使用者選擇不同選項。

UIActionSheet是在螢幕底端彈出一個訊息框,功能類似UIAlertView,不過兩者除了位置不一樣外,其外觀也有出入。為了能夠響應UIAlertView和UIActionSheet,需要設定其代理,而對應的代理需要實現對應的協議(UIAlertViewDelegate,UIActionSheetDelegate)。實現以下兩個函數

// Called when a button is clicked. The view will be automatically dismissed after this call returns- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;// Called when a button is clicked. The view will be automatically dismissed after this call returns- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;


範例程式碼

代理實現協議
@interface ViewController : UIViewController 

主要功能代碼
-(IBAction)onTextFieldEnd:(id)sender{    [_textTime resignFirstResponder];    // 顯示對話方塊#if 0    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"TextContend" message:_textTime.text delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Good", @"Thanks", nil];        [alert show];    [alert release];#else    // 底部彈出對話方塊    UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:@"ActionSheet" delegate:self cancelButtonTitle:@"cancelButtonTitle" destructiveButtonTitle:@"destructiveButtonTitle" otherButtonTitles:@"OtherButton1", @"OtherButton2", nil];        [sheet showInView:self.view];    [sheet release];#endif}// 按鈕按下時響應函數- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    NSString* strInfo = nil;        strInfo = [[NSString alloc] initWithFormat:@"你按下了【%@】按鈕,第%d個按鈕", [alertView buttonTitleAtIndex:buttonIndex], buttonIndex];    UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"按鈕響應代理函數" message:strInfo delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];        [alert show];    [alert release];    [strInfo release];}// 按鈕按下時響應函數- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{    NSString* strInfo = nil;        strInfo = [[NSString alloc] initWithFormat:@"你按下了【%@】按鈕,第%d個按鈕", [actionSheet buttonTitleAtIndex:buttonIndex], buttonIndex];    UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"按鈕響應代理函數" message:strInfo delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];        [alert show];    [alert release];    [strInfo release];}

相關文章

聯繫我們

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