iPhone, is it possible to put an actionSheet and the clickedButtonAtIndex event in a reusable class?
I want to provide the same buttons and the sa开发者_如何转开发me functionality in the clickedButtonAtIndex
on several views, can I do this in a reusable class?
If so how ?
One way would be to encapsulate your actionsheet+delegate into a new class:
@interface MyActionSheet : NSObject <UIActionSheetDelegate>
{
UIActionSheet* _actionSheet;
}
@end
@implementation MyActionSheet
- (id) initAndShowInView: (UIView*) view
{
if ( (self = [super init] ) )
{
_actionSheet = [[UIActionSheet alloc] initWithTitle: @"Hi There" delegate:self cancelButtonTitle:@"done" destructiveButtonTitle: nil otherButtonTitles: @"button 1", @"button2", nil];
[_actionSheet showInView:view];
}
return self;
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// your logic here
}
- (void)dealloc {
[_actionSheet release];
[super dealloc];
}
@end
This could be expanded on to add the delegate methods you may need. You'll need to make sure the object continues to live (keep a retain on it) while the actionsheet is visible - the actionsheet doesn't increment the ref count on its delegate...
精彩评论