获取UIActionSheet以执行操作

时间:2013-02-01 17:50:53

标签: iphone ios uiactionsheet

这是一个新手问题..我创建了UIActionSheet。现在我如何让它在按下时执行“删除”。

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.subtype == UIEventSubtypeMotionShake )
    {
        // Handle shake notification
    UIActionSheet *shakeActionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                                  delegate:self
                                                         cancelButtonTitle:@"Cancel"
                                                    destructiveButtonTitle:@"Delete"
                                                         otherButtonTitles:nil];

    [shakeActionSheet showInView:self];
    }

    if ([super respondsToSelector:@selector(motionEnded:withEvent:)])
        [super motionEnded:motion withEvent:event];
}

3 个答案:

答案 0 :(得分:2)

尝试调用UIActionSheet委托方法

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

    NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"Delete"])
    {
     //do your stuff in here

     }

}

答案 1 :(得分:1)

您的课程必须符合UIActionSheetDelegate协议。

然后覆盖actionSheet:clickedButtonAtIndex:方法,评估点击的按钮并采取相应的行动。

有关详细信息,请参阅here

答案 2 :(得分:0)

只需添加其他两个解决方案,您就可以在- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event方法中添加代理(假设您的类实现了UIActionSheetDelegate协议):

shakeActionSheet.delegate = self;