天天看點

[iPhone開發之控件的使用]UIActionSheet的各種屬性、方法及代理的使用

#import "ActionSheetTestViewController.h"  

@implementation ActionSheetTestViewController  

/* 

Tasks 

Creating Action Sheets 

    – initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles:   

    Setting Properties 

    delegate  property   

    title  property   

    visible  property   

    actionSheetStyle  property  無例 

Configuring Buttons 

    – addButtonWithTitle:   

    numberOfButtons  property   

    – buttonTitleAtIndex:   

    cancelButtonIndex  property   

    destructiveButtonIndex  property   

    firstOtherButtonIndex  property   

Displaying 

    – showFromTabBar:   

    – showFromToolbar:   

    – showInView:   

Dismissing 

    – dismissWithClickedButtonIndex:animated:   

*/  

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  

- (void)viewDidLoad {  

    UILabel *numOfBtn = [[UILabel alloc]initWithFrame:CGRectMake(10.0, 10.0, 30.0, 30.0)];  

    UILabel *titleOfBtn = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 10.0, 100.0, 30.0)];  

    UILabel *cancelBtnIndex = [[UILabel alloc]initWithFrame:CGRectMake(200.0, 10.0, 30.0, 30.0)];  

    UILabel *destructiveBtnIndex = [[UILabel alloc]initWithFrame:CGRectMake(10.0, 50.0, 30.0, 30.0)];  

    UILabel *firstOtherBtnIndex = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 50.0, 30.0, 30.0)];  

    UIActionSheet *actionSheetTest = [[UIActionSheet alloc]initWithTitle:@"ActionSheetTest"   

                                delegate:self  

                                cancelButtonTitle:@"CancelButton"   

                                destructiveButtonTitle:@"RedButton"   

                                otherButtonTitles:@"OtherButton1",@"OtherButton2",nil];  

    //看actionSheet是否可見,這是一個隻讀屬性  

    BOOL a = actionSheetTest.visible;  

    NSLog(@"%d",a);  

    //不考慮指定索引的按鈕的動作,可以設定是否有動畫  

    [actionSheetTest dismissWithClickedButtonIndex:0 animated:NO];  

    //設定标題  

    actionSheetTest.title = @"ActionSheetTitle";  

    //通過給定标題添加按鈕  

    [actionSheetTest addButtonWithTitle:@"addButtonWithTitle"];  

    //按鈕總數  

    numOfBtn.text = [NSString stringWithFormat:@"%d",actionSheetTest.numberOfButtons];  

    //擷取指定索引的标題  

    titleOfBtn.text = [actionSheetTest buttonTitleAtIndex:4];  

    //擷取取消按鈕的索引  

    cancelBtnIndex.text = [NSString stringWithFormat:@"%d",actionSheetTest.cancelButtonIndex];  

    //擷取紅色按鈕的索引  

    destructiveBtnIndex.text = [NSString stringWithFormat:@"%d",actionSheetTest.destructiveButtonIndex];  

    //擷取第一個其他按鈕的索引  

    firstOtherBtnIndex.text = [NSString stringWithFormat:@"%d",actionSheetTest.firstOtherButtonIndex];  

    //設定actionSheet出現的方式  

    [actionSheetTest showInView:self.view];//or [actionSheetTest showFromTabBar:] or [actionSheetTest showFromToolBar:]  

    [self.view addSubview:numOfBtn];  

    [self.view addSubview:titleOfBtn];  

    [self.view addSubview:cancelBtnIndex];  

    [self.view addSubview:destructiveBtnIndex];  

    [self.view addSubview:firstOtherBtnIndex];  

    [actionSheetTest release];  

    [numOfBtn release];  

    [titleOfBtn release];  

    [cancelBtnIndex release];  

    [destructiveBtnIndex release];  

    [firstOtherBtnIndex release];  

    [super viewDidLoad];  

}  

// Override to allow orientations other than the default portrait orientation. 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

    // Return YES for supported orientations 

    return (interfaceOrientation == UIInterfaceOrientationPortrait); 

- (void)didReceiveMemoryWarning {  

    // Releases the view if it doesn't have a superview.  

    [super didReceiveMemoryWarning];  

    // Release any cached data, images, etc that aren't in use.  

- (void)viewDidUnload {  

    // Release any retained subviews of the main view.  

    // e.g. self.myOutlet = nil;  

- (void)dealloc {  

    [super dealloc];  

#pragma mark -- UIActionSheetDelegate --  

//根據被點選按鈕的索引處理點選事件  

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {  

    NSLog(@"clickedButtonAtIndex:%d",buttonIndex);  

//ActionSheet已經消失時  

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {  

    NSLog(@"didDismissWithButtonIndex:%d",buttonIndex);  

//ActionSheet即将消失時  

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {  

    NSLog(@"willDismissWithButtonIndex:%d",buttonIndex);  

//  

- (void)actionSheetCancel:(UIActionSheet *)actionSheet {  

    NSLog(@"actionSheetCancel");  

//ActionSheet已經顯示時  

- (void)didPresentActionSheet:(UIActionSheet *)actionSheet {  

    NSLog(@"didPresentActionSheet%@",actionSheet);  

//ActionSheet即将顯示時  

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {  

    NSLog(@"willPresentActionSheet%@",actionSheet);  

@end  

  本文轉自新風作浪 51CTO部落格,原文連結:http://blog.51cto.com/duxinfeng/1208757,如需轉載請自行聯系原作者