0

单击下面的按钮操作时,出现异常:

-[UIRoundedRectButton selectedSegmentIndex]:发送到实例 0x8178b90 的无法识别的选择器
'

(它也被初始化为 - (IBAction)genderBtn:(id)sender; 在头文件中)。

我不知道我是否应该以某种方式将其初始化为另一种方法或不进行全局初始化。任何方法的想法将不胜感激。

- (IBAction)submitButton:(id)sender {

  double BAC=0;

  //  NSString *weight=weightTextField.text;

  //Other variables etc.

UISegmentedControl *gender = (UISegmentedControl *)sender;


   UIButton *gender = (UIButton *)sender;


if (gender.selected == 0 ) {



} else if (gender.selected = 1){



}


UIAlertView *alertMessage = [[UIAlertView alloc] initWithTitle:@"Your Results:"
                                          message:[NSString stringWithFormat:@" Your Percentage is: "]
                                         delegate:self
                                cancelButtonTitle:@"OK"
                                otherButtonTitles:nil];

[alertMessage show];
}
4

1 回答 1

1

错误是说尽管您认为 sender 值是 UISegmentedControl,但事实并非如此。这是一个 UIRoundedRectButton。结果是您最终将仅 UISegmentedControl 实现的消息发送到 UIRoundedRectButton,因此它无法识别选择器。确保此操作连接到正确类型的按钮。


编辑:好的。我以前看过你的代码。我认为问题在于您使用了普通的 UIButton 而不是 UISegmentedControl,但问题似乎真的是您根本不应该使用 sender 参数。

我认为您有一个 UISegmentedControl 供用户选择某些内容,还有一个 UIButton 供他们在完成选择后点击。问题是您在询问发件人参数(即提交按钮) UISegmentedControl 的选择状态是什么。您需要将 UISegmentedControl 存储在一个属性中,并在您的提交方法中使用它来获取 selectedSegmentIndex。

- (IBAction)submitButton:(id)sender {

    double BAC=0;

    //NSString *weight=weightTextField.text;

    //Other variables etc.

    UISegmentedControl *gender = self.segmentedControl;


    if (gender.selectedSegmentIndex == 0 ) {
        //something
    } else if (gender.selectedSegmentIndex == 1){
        //something
    }

    UIAlertView *alertMessage = [[UIAlertView alloc] initWithTitle:@"Your Results:"
                                          message:[NSString stringWithFormat:@" Your Percentage is: "]
                                         delegate:self
                                cancelButtonTitle:@"OK"
                                otherButtonTitles:nil];

    [alertMessage show];
}

提交按钮在按下时调用它,并从您存储在属性中的分段控件中获取选定的索引。

@property (weak, nonatomic) IBOutlet UISegmentedControl* segmentedControl; //goes in @interface

@synthesize segmentedControl = _segmentedControl; //goes in @implementation

将此 IBOutlet 连接到您的分段控件并使用它。

于 2013-01-09T20:49:18.903 回答