在.h加上 UIActionSheetDelegate 的 Protocols 實作 clickedButtonAtIndex 事件
1. 在iPad上不能使用在ViewDidLoad, 會出現Exception錯誤, 所以拉一個Button用IBAction去執行
2. 在iPad上點擊其他空白地方時, 會自動關閉UIActionSheet, buttonIndex會回傳3, 但是如果將cancelButtonTitle設成nil的話, buttonIndex會回傳-1
Source Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface UntitledViewController : UIViewController <UIActionSheetDelegate> { | |
} | |
- (IBAction)showActionSheet:(id)sender; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)showActionSheet:(id)sender { | |
UIActionSheet *action = [[UIActionSheet alloc] | |
initWithTitle:@"Hello" | |
delegate:self | |
cancelButtonTitle:nil | |
destructiveButtonTitle:@"Delete Message" | |
otherButtonTitles:@"Option 1", @"Option 2", nil]; | |
[action showInView:self.view]; | |
[action release]; | |
} | |
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { | |
NSLog(@"%d", buttonIndex); | |
} |
Reference
Beginning-iOS-4-Application-Development