How to get the selected value of a UIPickerview fr

2019-09-18 13:40发布

问题:

In my view I have 2 TextFields, 1 Label, 1 Picker and 1 button

I want to the label to have the value of the 2 textfields and the selected value of the picker when the button is clicked.

The following is my button click code

-(IBAction)changeTheTextOfTheLabel
{
    NSInteger row;
    row = [listPicker selectedRowInComponent:0];
    NSString *fullName = [[NSString alloc] initWithFormat:@"%@ %@ (%@)",firstNameTextView.text,lastNameTextView.text, [listArray objectAtIndex:row]];
    [fullNameLabel setText:fullName];
    [fullName release];
}

The above code work fine except that it always put the first item ofthe picker in the label no matter which row it was selected.

Thank you.

回答1:

It doesn't look like the instance variable listPicker is connected to the UIPickerView in IB.

row = [listPicker selectedRowInComponent:0];

Here row will always be zero if listPicker is nil and that should happen only when listPicker is not connected.