iOS Switch语句不起作用?

时间:2014-02-11 02:19:54

标签: ios objective-c switch-statement

我有一个带五个按钮的View Controller。每个按钮应根据按钮的标记触发到不同视图控制器的模态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;
    }
}

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

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'

我的代码看起来很合理,但显然不是。有人可以告诉我哪里出错了吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

它失败了哪一行代码?进入buttonForSegue?在开关的开始?在其中一个performSegueWithIdentifier?

你可以尝试的事情:

  • 确保发件人实际上是UIButton类
  • 确保标记设置在按钮上
  • 检查您正在查看的视图控制器以实现方法aVsAllButton