【问题标题】:UIActionSheet responder not called [duplicate]UIActionSheet 响应者未调用 [重复]
【发布时间】:2013-12-29 18:00:43
【问题描述】:

这是我在stackoverflow上的第一篇文章,如果我犯了一些错误,请原谅我。

我是一个相对较新的应用设计师,我一直在开发一个新应用,希望能尽快发布。我最近遇到了一个问题。我无法使用UIActionSheet。虽然它显示正常,但按钮只是使操作表消失。我尝试了很多东西都无济于事;我做了this thread 中建议的所有事情(由有类似但不相同问题的人提出)。我已经测试并确保没有使用NSLog 完全调用该函数。我尝试过更改showInView,尽管这似乎不会有所作为。所以无论如何,这是我当前的代码:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if ( event.subtype == UIEventSubtypeMotionShake )
    {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"You Shook?" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"End Game" otherButtonTitles:@"Settings", nil];
        [actionSheet showInView:self.view];
    }

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

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        UIAlertView *endGameAlertView = [[UIAlertView alloc] initWithTitle:@"End Game" message:@"Are you sure you want to end the current game?" delegate:nil cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
        [endGameAlertView show];

    }

    else
    {
        UIAlertView *unimplementedSettingsAlertView = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"This feature is not yet implemented" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
        [unimplementedSettingsAlertView show];
    }
}

如您所见,actionSheet 是通过摇动设备触发的(效果很好),唯一的问题是按钮除了关闭 actionSheet 之外什么都不做。我在 OSX 10.9.1 上使用 Xcode 5.0.2。如果我遗漏了任何重要信息,请告诉我。提前谢谢!

~初级

【问题讨论】:

    标签: ios objective-c cocoa-touch uiactionsheet uiactionsheetdelegate


    【解决方案1】:

    目前您正在将actionSheetdelegate 设置为nil,因此您的委托方法没有被调用。

    在你的 ViewController.h 文件中,这样写:

    @interface ViewController : UIViewController <UIActionSheetDelegate>
    

    您必须设置 actionSheet 的委托才能调用委托方法。

        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"You Shook?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"End Game" otherButtonTitles:@"Settings", nil];
    

    【讨论】:

    • 好的很好,现在错误已经消失了,谢谢!但是,当我尝试运行它时,我收到此错误:clang: error: linker command failed with exit code 1 (use -v to see invocation)
    • 你一定会得到一些错误日志。此错误日志是否提到任何重复符号或找不到文件错误??
    • 是:ld:架构 i386 的 3 个重复符号
    • 好的,知道了!我不小心将#include AppDelegate.m 添加到我的ViewController.m 中。现在一切都很好。谢谢!
    【解决方案2】:

    将委托设置为 self

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"You Shook?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"End Game" otherButtonTitles:@"Settings", nil];
    

    【讨论】:

    • 当我这样做时出现此错误:Sending 'ViewController *const __strong' to parameter of incompatible type 'id&lt;UIActionSheetDelegate&gt;'
    • 在你必须显示的标题中,这个 UIViewController 子类实现了 UIActionSheetDelegate 接口,如下所示: YourViewController: UIViewController
    猜你喜欢
    • 1970-01-01
    • 2011-06-03
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 2016-10-22
    • 2016-02-11
    相关资源
    最近更新 更多