你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS-UIAlertView和UIActionSheet

IOS-UIAlertView和UIActionSheet

編輯:IOS開發綜合

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];
}

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved