IOS UIAlertView UIActionSheet

參考資料:
http://pro.ctlok.com/2010/08/iphone-ipad-uialertview.html
http://www.istar.name/blog/ios-use-uialertview
UIAlertView  這個元件並不經常使用,若是將  UIAlertView  用做顯示普通訊息,這不是一個好的介面設計,因為彈出來的訊息是很是引人注意的,就好像  Javascript  的  alert  一樣,彈出來後整個視窗也不能操做,必定要用戶按下 「OK」 才能繼續操做,我相信各位也不喜歡到經常彈出  alert box  的網站吧,在  iPhone  也是同樣道理。

那何時才使用  UIAlertView ? 應該是有某些訊息無論如何也要用戶去知道,不是那些無關緊要的事,有多是你的應用程式發生一些問題,令操做不能繼續的訊息。例如你的應用程式必須依賴網路來拿取資料,但用戶的裝置根本沒有連接網路,這時候你便須要使用 UIAlertView  去提示用戶去連接網路,否則應用程式不能運做。
#import <UIKit/UIKit.h>

@interface  ButtonViewController : UIViewController<UIActionSheetDelegate,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *myButton;

@end

#import "ButtonViewController.h"
#define FIRST_BUTTON 1
#define SECOND_BUTTON 2
#define FIRST_ALERT_VIEW 1

@interface  ButtonViewController ()

@end 

@implementation ButtonViewController
@synthesize myButton;

- (IBAction)alertOnclick:(id)sender {
    CGRect frame = CGRectMake(60, 200, 200, 60);
    UIButton *otherBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    otherBtn.backgroundColor = [UIColor clearColor];
    [otherBtn setTitle:@"UIAlertView例子" forState:UIControlStateNormal];
    otherBtn.frame = frame;
    [otherBtn addTarget:self action:@selector(otherBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:otherBtn];
}

- (IBAction)sheetOnclick:(id)sender {
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"您肯定?" delegate:self cancelButtonTitle:@"不肯定" destructiveButtonTitle:@"很是肯定" otherButtonTitles:nil, nil];
    [sheet showInView:self.view];
}

- (void) otherBtnClick
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您點擊了動態按鈕!" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:@"第一項",@"第二項",nil];
    //設置標題與信息,一般在使用frame初始化AlertView時使用
    alert.title = @"AlertViewTitle";
    alert.message = @"AlertViewMessage";
    
    //這個屬性繼承自UIView,當一個視圖中有多個AlertView時,能夠用這個屬性來區分
    alert.tag = FIRST_ALERT_VIEW;
    
    //只讀屬性,看AlertView是否可見
    NSLog(@"%d",alert.visible);
    
    //經過給定標題添加按鈕
    [alert addButtonWithTitle:@"addButton"];
    
    //按鈕總數
    NSLog(@"numberOfButtons:%d",alert.numberOfButtons);
    
    //獲取指定索引的按鈕的標題
    NSLog(@"buttonTitleAtIndex:%@",[alert buttonTitleAtIndex:2]);
    
    //得到取消按鈕的索引
    NSLog(@"cancelButtonIndex:%d",alert.cancelButtonIndex);
    
    //得到第一個其餘按鈕的索引
    NSLog(@"firstOtherButtonIndex:%d",alert.firstOtherButtonIndex);
    [alert show];
}

-(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 clickedButtonAtIndex:(NSInteger)buttonIndex  
{  
    NSLog(@"buttonIndex:%d", buttonIndex); 
    if (buttonIndex == FIRST_BUTTON && alertView.tag == FIRST_ALERT_VIEW) {
        UIAlertView *first = [[UIAlertView alloc] initWithTitle:nil message:@"您點擊了第一個按鈕" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];
        [first show];
    } 
    if (buttonIndex == SECOND_BUTTON && alertView.tag == FIRST_ALERT_VIEW) {
        UIAlertView *second = [[UIAlertView alloc] initWithTitle:nil message:@"您點擊了第二個按鈕" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];
        [second show];
    }
} 

//AlertView已經消失時
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    NSLog(@"didDismissWithButtonIndex");
}
//AlertView即將消失時
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
    NSLog(@"willDismissWithButtonIndex");
}

- (void)alertViewCancel:(UIAlertView *)alertView {
    NSLog(@"alertViewCancel");
}
//AlertView已經顯示時
- (void)didPresentAlertView:(UIAlertView *)alertView {
    NSLog(@"didPresentAlertView");
}
//AlertView即將顯示時
- (void)willPresentAlertView:(UIAlertView *)alertView {
    NSLog(@"willPresentAlertView");
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [self setMyButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
相關文章
相關標籤/搜索