我有一个使用所需值的iOS UiPicker,并且可以通过以下代码检测何时选择了新选项:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    NSString *aCompany = [arrayColour objectAtIndex:[pickerView selectedRowInComponent:0]];

    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Picked:"
                          message:aCompany
                          delegate:nil
                          cancelButtonTitle:@"Okay"
                          otherButtonTitles:nil];

    [alert show];


}


我的问题是按下这样的按钮后如何从选择器中获取最新信息:

- (IBAction)DoneButton:(id)sender {



    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Picked:"
                          message:aCompany
                          delegate:nil
                          cancelButtonTitle:@"Okay"
                          otherButtonTitles:nil];

    [alert show];

}

最佳答案

pickerView设置为IBOutlet(并使用IB链接),然后可以在didSelectRow之外访问它。

@property (nonatomic) IBOutlet UIPickerView *myPickerView;


然后,您可以在DoneButton中使用以下行:

NSString *aCompany = [arrayColour objectAtIndex:[self.myPickerView selectedRowInComponent:0]];

10-07 19:43