简体   繁体   中英

How to get the row number of the selected value of UIPickerView

I have a UIPickerView in my viewController. I can get the value of the row number of the selected value in the picker from the delegate method

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

But in my case, I need to get the row number of the selected component as soon as the view appears. Is there a way to get the row number/trigger the delegate method as soon as the view appears.

You just have to use selectedRowInComponent

selectedRowInComponent:

Returns the index of the selected row in a given component. - (NSInteger)selectedRowInComponent:(NSInteger)component Parameters

component

 A zero-indexed number identifying a component of the picker view. 

Return Value

A zero-indexed number identifying the selected row, or -1 if no row is selected. Availability

 Available in iOS 2.0 and later. 

See Also

 – selectRow:inComponent:animated: 

Related Sample Code

 iPhoneCoreDataRecipes 

Declared In UIPickerView.h

you use this for select in pickerview

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
   {
      NSString *str= [yardsArray objectAtIndex:row];

   }

use this code when view will load

 -(void)ViewDidLoad
  {
   NSString *str= [yardsArray count];
  }

in viewDidLoad

travers the array of your objects and match object of which you want to find out the row and tell me if this is the solution

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