2013-02-01 45 views
0

这是一个菜鸟问题..我创建了UIActionSheet。现在如何让它按下时执行“删除”。Get 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]; 
} 

回答

2

尝试调用UIActionSheet委托方法

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

    NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex]; 

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

    } 

} 
+0

谢谢!好的,我还希望操作表消失,如果用户决定触摸屏幕而不是取消按钮,则不执行任何操作。我如何实现这一点? – user1779598

+1

你需要使用'UITapGestureRecognizer' http://stackoverflow.com/questions/2623417/iphone-sdk-dismissing-modal-viewcontrollers-on-ipad-by-clicking-outside-of-it –

+0

这是一个新问题。你应该标记其中一个正确的答案。点击手势识别器也是一个很好的答案。 – danh

1

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

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

有关更多信息,请参见here

+0

你的意思是这样的@interface yellowView:UIView ? – user1779598

+0

是的,正确的。通过这个,你告诉框架,你会实现一个或多个委托方法。 – SAE

0

只是为了添加到其他两种解决方案,你可以在你- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event方法这样添加代理(假设你的类实现UIActionSheetDelegate协议):

shakeActionSheet.delegate = self; 
+0

在init方法句柄中没有参数“delegate:self”? – user1779598

+0

你是对的。对不起,我忽略了这一点。 – AC1