2011年2月11日 星期五

[iOS Dev] 簡單使用 UIActionSheet 和 iPad上的注意事項

簡單的UIActionSheet範例:

在.h加上 UIActionSheetDelegate 的 Protocols 實作 clickedButtonAtIndex 事件

1. 在iPad上不能使用在ViewDidLoad, 會出現Exception錯誤, 所以拉一個Button用IBAction去執行
2. 在iPad上點擊其他空白地方時, 會自動關閉UIActionSheet, buttonIndex會回傳3, 但是如果將cancelButtonTitle設成nil的話, buttonIndex會回傳-1

Source Code

#import <UIKit/UIKit.h>
@interface UntitledViewController : UIViewController <UIActionSheetDelegate> {
}
- (IBAction)showActionSheet:(id)sender;
@end

- (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
Related Posts Plugin for WordPress, Blogger...