-1

我有一个名为 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
4

3 回答 3

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:)];
于 2013-03-13T20:58:38.047 回答
1

错误的重要部分是:

[NotesModal done:]: unrecognized selector

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

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

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

注意-(void)done-(void)done:(id)sender一样

于 2013-03-13T20:59:07.130 回答
0

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

它需要是这样的:

-(void)done:(id)selector
{
    [self dismissModalViewController];
}
于 2013-03-13T20:59:40.527 回答