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


转载于:https://my.oschina.net/dingbuoyi/blog/71571

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值