UISegmentedControl选择器不起作用

时间:2013-03-13 20:50:49

标签: ios objective-c uisegmentedcontrol

我有一个名为Modal的调用,我正在运行以下代码。

    - (void)createAccessoryView
{

    CGRect frame = CGRectMake(0.0, self.frame.size.height, self.frame.size.width, 44.0);
    fieldAccessoryView = [[UIToolbar alloc] initWithFrame:frame];
    fieldAccessoryView.barStyle = UIBarStyleBlackOpaque;
    fieldAccessoryView.tag = 200;

    [fieldAccessoryView setBarStyle:UIBarStyleBlack];

    UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone  target:self action:@selector(done:)];

    UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Previous", @""), NSLocalizedString(@"Next", @""), nil]];
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    [segmentedControl setMomentary:YES];
    UIBarButtonItem *segmentButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];

    [fieldAccessoryView setItems:[NSArray arrayWithObjects:segmentButton, spaceButton, doneButton, nil] animated:NO];

}

-(void)segmentAction:(id)selector
{

}

然后我创建了一个扩展Modal的类,并且有一些UITextField。单击文本字段会按预期显示键盘。键盘启动后,我会看到上一个/下一个和完成的按钮。单击完成抛出和错误,并且不应该使用segmentAction方法。不太清楚为什么。

这是我点击完成按钮后得到的堆栈跟踪

2013-03-13 15:54:33.956 myapp[74194:c07] -[NotesModal done:]: unrecognized selector sent to instance 0x80f3fb0
2013-03-13 15:54:33.961 myapp[74194:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NotesModal done:]: unrecognized selector sent to instance 0x80f3fb0'
*** First throw call stack:
(0x1b54012 0x1470e7e 0x1bdf4bd 0x1b43bbc 0x1b4394e 0x1484705 0x3b82c0 0x5f4a64 0x1484705 0x3b82c0 0x3b8258 0x479021 0x47957f 0x4786e8 0x3e7cef 0x3e7f02 0x3c5d4a 0x3b7698 0x1aafdf9 0x1ad7f3f 0x1ad796f 0x1afa734 0x1af9f44 0x1af9e1b 0x1aae7e3 0x1aae668 0x3b4ffc 0x1786d 0x24b5)
libc++abi.dylib: terminate called throwing an exception

3 个答案:

答案 0 :(得分:1)

此代码

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone  
                                                                            target:self 
                                                                            action:@selector(done:)];

需要一个方法

-(void)done:(id)selector
{
    //…
}

提供或将UIBarButtonItem更改为

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone  
                                                                            target:self 
                                                                            action:@selector(segmentAction:)];

答案 1 :(得分:1)

错误的重要部分是:

[NotesModal done:]: unrecognized selector

因此,它崩溃了,因为它无法识别方法done:

确保您确实拥有done:方法,例如:

 -(void)done:(id)sender
 {
      // whatever it does here...
 }

注意-(void)done-(void)done:(id)sender 相同。

答案 2 :(得分:0)

请检查您的done方法是否包含任何参数。

它必须是:

-(void)done:(id)selector
{
    [self dismissModalViewController];
}