【问题标题】:iOS Switch statement not working?iOS Switch 语句不起作用?
【发布时间】:2014-03-08 15:29:53
【问题描述】:

我有一个带有五个按钮的视图控制器。每个按钮都应该根据按钮的标签触发一个模态segue到不同的视图控制器,由声明的常量表示:

- (IBAction)aButtonTapped:(UIButton *)sender
{
    [self buttonForSegue:sender];
}

-(void) buttonForSegue:(UIButton *)sender
{

    switch ([sender tag])
    {
        case aVsAButton_tag:
            [self performSegueWithIdentifier:@"aVsAModal" sender:self];
            break;
        case cVsCButton_tag:
            [self performSegueWithIdentifier:@"cVsCModal" sender:self];
            break;
        case actVsAllButton_tag:
            [self performSegueWithIdentifier:@"actVsAllModal" sender:self];
            break;
        case catVsAllButton_tag:
            [self performSegueWithIdentifier:@"catVsAllModal" sender:self];
            break;
        case customDatePickerButton_tag:
            [self performSegueWithIdentifier:@"customDatePickerModal" sender:self];
            break;

        default:
            break;
    }
}

无论我点击哪个按钮,应用程序都会在 sim 卡中崩溃并显示此消息(仅按钮名称更改):

2014-02-10 19:11:47.305 WMDGx[24366:a0b] -[ReportViewController aVsAllButton:]:无法识别的选择器发送到实例 0x8a911e0 2014-02-10 19:11:47.307 WMDGx[24366:a0b] * ** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[ReportViewController aVsAllButton:]:无法识别的选择器发送到实例 0x8a911e0”

我的代码在我看来是合理的,但显然不是。谁能告诉我哪里出错了?

谢谢!

【问题讨论】:

  • 这个问题与你的 switch 语句无关。崩溃告诉你具体的问题。您正在调用 ReportViewController 上的选择器,但它没有响应。您需要显示调用 aVsAllButton: 选择器的代码。
  • @Jeremy--Duncan C 在第二个答案中指出了这一点。感谢您的回复!

标签: ios objective-c switch-statement


【解决方案1】:

您报告的错误不是由您发布的代码引起的。我猜您的按钮中有一个 IBAction 链接,并且该操作方法不再存在。打开故事板文件,为视图控制器选择场景,然后使用连接检查器检查每个按钮的操作链接。您可能需要断开连接并将其重新链接到正确的 IBAction。

【讨论】:

  • 宾果游戏!我在每个按钮上都有多个连接。我在早期的迭代中忘记了它们,试图将每个按钮单独连接到一个 segue。非常感谢,赞成票和接受的答案!
【解决方案2】:

看起来这与您的 switch 语句无关,而是调用了您的一个控制器上不存在的函数。

它在哪一行代码上失败了?输入按钮ForSegue?在切换的开始?在其中一个 performSegueWithIdentifier 期间?

你可以尝试的事情:

  • 确保 sender 实际上是一个 UIButton 类
  • 确保在按钮上设置了标签
  • 检查您要实现方法aVsAllButton 的视图控制器

【讨论】:

  • 具体解决方案见下方 Duncan C 的回答。感谢您的回复!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多