【问题标题】:Get UIActionSheet to perform action获取 UIActionSheet 以执行操作
【发布时间】:2013-02-02 02:26:55
【问题描述】:

这是一个菜鸟问题。我创建了 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];
}

【问题讨论】:

    标签: iphone ios uiactionsheet


    【解决方案1】:

    尝试调用UIActionSheet委托方法

    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
    {
    
        NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];
    
        if([title isEqualToString:@"Delete"])
        {
         //do your stuff in here
    
         }
    
    }
    

    【讨论】:

    • 谢谢!好的,如果用户决定触摸屏幕而不是取消按钮,我还希望操作表消失并且什么也不做。我该如何实现?
    • 你需要使用UITapGestureRecognizerstackoverflow.com/questions/2623417/…
    • 这是一个新问题。您应该将其中一个答案标记为正确。而轻击手势识别器也是一个不错的答案。
    • 我不知道你能做到这一点。我如何“将这些答案之一标记为正确”? @danh
    • 答案投票附近有一个小复选框。只需在对您最有帮助的答案上单击它。
    【解决方案2】:

    您的类必须符合UIActionSheetDelegate 协议。

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

    请参阅here 了解更多信息。

    【讨论】:

    • 你的意思是这样的@interface yellowView : UIView ?
    • 是的,正确的。通过这个你告诉框架,你将实现一个或多个委托方法。
    【解决方案3】:

    只是添加到其他两个解决方案,您可以像这样在- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 方法中添加委托(假设您的类实现了UIActionSheetDelegate 协议):

    shakeActionSheet.delegate = self;
    

    【讨论】:

    • init 方法中的参数“delegate:self”不处理吗?
    猜你喜欢
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2020-08-14
    相关资源
    最近更新 更多