iOS(底部)對話框UIActionSheet和UIAlertView用法

UIActionSheet和UIAlertView在iOS中,都是彈出一個對話框,用戶必須點擊上面的按鈕後才能執行其餘操做。

下圖是UIActionSheet,對話框顯示在底部: spa


下圖是AlertView,顯示在屏幕正中:
code


當按鈕在兩個之內時,按鈕是水平顯示的,當超過兩個時,會垂直顯示。
至關代碼:

首先在viewController h頭文件添加UIActionSheetDelegate,UIAlertViewDelegate協議,以下: orm

#import <UIKit/UIKit.h>
@interface TESTViewController : UIViewController <UIActionSheetDelegate,UIAlertViewDelegate>
接收ActionSheet點擊事件,以及打開AlertView對話框、接收AlertView點擊事件:

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    //該方法由UIActionSheetDelegate協議定義,在點擊ActionSheet的按鈕後自動執行
    NSString *string=[NSString stringWithFormat:@"你點擊了 %@",[actionSheet buttonTitleAtIndex:buttonIndex]];
 
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:string delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:@"取消",nil];
    [alert show];
 
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    //該方法由UIAlertViewDelegate協議定義,在點擊AlertView按鈕時自動執行,因此若是這裏再用alertView來彈出提//示,就會死循環,不停的彈AlertView
    NSString * string=[NSString stringWithFormat:@"你點擊了 %@",[alertView buttonTitleAtIndex:buttonIndex]];
    //    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:string delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil];
    //    [alert show];
    NSLog(@"%@",string);
 
}
相關文章
相關標籤/搜索