Search code examples
iosobjective-cnsmutablearrayuipickerview

DownPicker when selected how to get the Array Index Value


I have followed this link to install the DownPicker. I need to get the NSMutableArray index value when the item is selected. The reason behind is I have 2 array. One is for country and one is for code. When user pick the country, I need to get the index value to get the code. Any help is gladly appreciated.

- (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];

}


Solution

  • In viewDidLoad add a target to 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;
            }
    
        }
    }