DownPicker选中时如何获取Array Index Value

DownPicker when selected how to get the Array Index Value

我已经按照这个 link 安装了 DownPicker。选择项目时,我需要获取 NSMutableArray 索引值。背后的原因是我有 2 个数组。一种用于国家,一种用于代码。当用户选择国家时,我需要获取索引值才能获取代码。非常感谢任何帮助。

- (void)viewDidLoad
{
    [super viewDidLoad];

    //=== Initiallize the Mutable Array
    countryMArray = [[NSMutableArray alloc] init];
    codeMArray = [[NSMutableArray alloc] init];

    //=== Initialize the responseData Mutable Data
    self.responseData = [NSMutableData data];

    //=== Pass the string to web and get the return Country response.write
    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    sURL = appDelegate.gURL;
    sURL = [sURL stringByAppendingString:@"/apps/getcountry.asp?"];

    NSURLRequest *requestCountry = [NSURLRequest requestWithURL:[NSURL URLWithString:sURL]];

    (void) [[NSURLConnection alloc] initWithRequest:requestCountry delegate:self];

    //=== Pass the string to web and get the return Code response.write
    sURL = appDelegate.gURL;
    sURL = [sURL stringByAppendingString:@"/apps/getctcode.asp?"];

    NSURLRequest *requestCode = [NSURLRequest requestWithURL:[NSURL URLWithString:sURL]];

    (void) [[NSURLConnection alloc] initWithRequest:requestCode delegate:self];

    self.pickerCountry = [[DownPicker alloc] initWithTextField:self.txtCountry withData:countryMArray];

}

当用户滚动时,然后添加目标。您将在您想要的方法中获得回调,如下所示:

[self.pickerCountry addTarget:self 
    action:@selector(dp_Selected:)
    forControlEvents:UIControlEventValueChanged];


-(void)dp_Selected:(id)dp {
    NSString* selectedValue = [self.pickerCountry text];
    // do what you want
    //search the text to get index value
}

官方link: https://github.com/Darkseal/DownPicker#status-change-event-handling

viewDidLoad中添加一个目标到pickerCountry

 [self.pickerCountry addTarget:self 
  action:@selector(pickerClicked:)
   forControlEvents:UIControlEventValueChanged];

//

-(void)pickerClicked:(id)dp {

     NSString* selectedValue = [self.pickerCountry text];

       for ( int i = 0 ; i < countryMArray.count; i++) {

         NSString*item = [countryMArray objectAtIndex:i];

         if([item isEqualToString:selectedValue])
         {
             [self.pickerCode selectRow:i inComponent:0 animated:YES];

             break;
        }

    }
}