UIAlertView就是我們常說的正告視圖
作用:提示用戶,幫助用戶選擇
在IOS中主要有2種情勢 1.是alert正告 彈出帶有震動效果 主要是給用戶1個通知
2.是ActionSheet 會在屏幕底部滑出 相當于產生1個占屏幕1/3大小的view
可以通過該窗口將信息發布到如 微薄 人人等資源上
ActionSheet和AlertView 比較相似都是給用戶1個提示信息。它是從底部彈出。它通經常使用于確認潛
在的危險或不能撤銷的操作,如刪除1個數據。
iOS程序中的Action Sheet就像Windows中的 “肯定-取消”對話框1樣,用于強迫用戶進行選擇。當用戶將要進行的操作具有1定危險時,常常使用Action Sheet對用戶進行危險提示,這樣,用戶有機會進行取消操作。
Action Sheet需要多個參數:
(1)initWithTitle:設置標題,將會顯示在Action Sheet的頂部
(2)delegate:設置Action Sheet的拜托。當Action Sheet的1個按鈕被按下后,它的delegate將會被通知,并且會履行這個delegate的actionSheet: didDismissWithButtonIndex方法將會履行。這里,我們將delegate設成self,這樣可以保證履行我們自己在ViewController.m寫的actionSheet: didDismissWithButtonIndex方法
(3)cancelButtonTitle:設置取消按鈕的標題,這個取消按鈕將會顯示在Action Sheet的最下邊
(4)destructiveButtonTitle:設置第1個肯定按鈕的標題,這個按鈕可以理解成:"好的,繼續"
(5)otherButtonTitles:可以設置任意多的肯定按鈕
Alert相當于Windows中的Messagebox,跟Action Sheet也是類似的。不同的是,Alert可以只有1個選擇項,而Action Sheet卻最少要兩個選項。
Alert也要填寫很多參數:
(1)initWithTitle:設置標題,將會顯示在Alert的頂部
(2)message:設置提示消息內容
(3)delegate:設置Alert的拜托。這里,我們設成self
(4)cancelButtonTitle:設置取消按鈕的標題
(5)otherButtonTitles:與Action Sheet類似
[alert show]這條語句用來顯示Alert。
project: UIAlertOrSheetDemo
new file ...
name:AlertView
superclass:UIAlertView
打開 AlertView.m
加入
- (void)dealloc
{
// NSLog(@"dead : %d", self.tag);
[super dealloc];
}
打開 AppDelegate.m
加入
#import "AlertView.h"
在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中的 [self.window makeKeyAndVisible]; 頂上加入
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(10, 100, 140, 40);
[button1 setTitle:@"alertView" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(showAlertView) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.frame = CGRectMake(240⑺0, 100, 140, 40);
[button2 setTitle:@"actionSheet" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(showActionView) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:button2];
在加入方法
- (void)showAlertView
{ //第1個是取消按鈕 以后是肯定按鈕
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"other1", @"other2", nil] ;
[alertView show];
}
- (void)showActionView
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"destructive" otherButtonTitles:@"other1", @"other2", @"other3", @"other3", nil] ;
[actionSheet showInView:self.window];
}