Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 选择下拉选择器时如何获取数组索引值_Ios_Objective C_Nsmutablearray_Uipickerview - Fatal编程技术网

Ios 选择下拉选择器时如何获取数组索引值

Ios 选择下拉选择器时如何获取数组索引值,ios,objective-c,nsmutablearray,uipickerview,Ios,Objective C,Nsmutablearray,Uipickerview,我已经按照这个步骤安装了下行选择器。选择项时,我需要获取NSMutableArray索引值。背后的原因是我有2个数组。一个代表国家,一个代表代码。当用户选择国家时,我需要获取索引值以获取代码。非常感谢您的帮助 - (void)viewDidLoad { [super viewDidLoad]; //=== Initiallize the Mutable Array countryMArray = [[NSMutableArray alloc] init]; co

我已经按照这个步骤安装了下行选择器。选择项时,我需要获取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
}
官方链接:

viewDidLoad
中将目标添加到
pickerCountry

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

-(无效)选择器点击:(id)dp{
NSString*selectedValue=[self.pickerCountry text];
for(int i=0;i
-(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;
        }

    }
}