简体   繁体   中英

iOS UIPickerView how to get the selected row

The value of the array is this

ListArray = [[NSMutableArray alloc] init];

ListArray = [NSMutableArray arrayWithObjects:@"Google", @"Samsung", @"Twitter", @"Facebook", @"Apple", @"NiKon", nil];

And when you click on the selectButton

NSString *select = [ListArray objectAtIndex:[_picker selectedRowInComponent:0]];

NSString *title = [[NSString alloc] initWithFormat:@"Select : %@", select];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

[alert show];

Receive a message box. When you press OK, I want to go to select the value of the selected file.

But select value is 0.

I do not know how.

You can implement UIPickerViewDelegate in your class and you will get an event when the user selects a row in a component:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    // here you can remember the selected row or perform some action
}

Don't forget to set your pickerView delegate:

self.pickerView.delegate = self; // or other
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

        NSString *selectedValue=[ListArray objectAtIndex:row]];
        NsLog(@"selectedValue:%@",selectedValue);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM