选择DownPicker时如何获取Array Index Value

时间:2018-04-18 10:35:04

标签: ios objective-c nsmutablearray uipickerview

我已按照此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];

}

2 个答案:

答案 0 :(得分:1)

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;
        }

    }
}

答案 1 :(得分:0)

当用户滚动时,然后添加目标。您将按照以下方法获得所需方法的回调:

[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
}

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