【问题标题】:UISegmentedControl selector not workingUISegmentedControl 选择器不起作用
【发布时间】:2013-03-14 04:59:40
【问题描述】:

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

【问题讨论】:

  • 你得到哪个错误?
  • 我得到的错误是选择器不可用
  • 可以发一下吗?
  • 好的,问题出在-[NotesModal done:]。你的done 方法中有参数吗?

标签: ios objective-c uisegmentedcontrol


【解决方案1】:

错误的重要部分是这样的:

[NotesModal done:]: unrecognized selector

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

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

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

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

【讨论】:

    【解决方案2】:

    这段代码

    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:)];
    

    【讨论】:

      【解决方案3】:

      请检查你的done方法是否有参数。

      它需要是这样的:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-22
        • 2015-09-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多