iPhone,是否可以将actionSheet和clickedButtonAtIndex事件放在可重用的类中?

时间:2010-11-11 15:38:05

标签: iphone objective-c xcode

我想在几个视图的clickedButtonAtIndex中提供相同的按钮和相同的功能,我可以在可重用的类中执行此操作吗?

如果是这样的话?

1 个答案:

答案 0 :(得分:1)

一种方法是将您的动作表+委托封装到一个新类中:

@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

这可以扩展为添加您可能需要的委托方法。当动作表可见时,您需要确保对象继续存在(保留对象) - 动作表不会增加其委托的引用计数...