#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