【发布时间】:2023-03-13 04:41:01
【问题描述】:
我下面的代码将从服务器获取国家名称和国家代码并存储到2 NSMutablleMArray、countryMArray 和codeMArraycode。当用户选择txtCountry UITextField时,它会调用picker list,当用户选择国家并点击完成时,它将根据索引获取Country Code from codeMArray并输入txtCode UITexField
我希望用户能够在选择器列表中点击国家,而不是点击完成,该值将被转移到UITextField
我已经尝试过这个This Thread,但它不起作用。我的想法已经用完了,请帮忙。
RootViewController.h
#import <UIKit/UIKit.h>
#import "LoginViewController.h"
#import <DownPicker/UIDownPicker.h>
@protocol LoginViewProtocol <NSObject>
- (void)dismissAndLoginView;
@end
@interface RootViewController : UIViewController <UITextFieldDelegate,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITextField *txtCountry;
@property (weak, nonatomic) IBOutlet UIDownPicker *downCountry;
@property (strong, nonatomic) IBOutlet UITextField *txtCode;
@property (strong, nonatomic) IBOutlet UITextField *txtPhone;
@property (strong, nonatomic) NSMutableArray *countryMArray;
@property (strong, nonatomic) NSMutableArray *codeMArray;
@property (nonatomic) DownPicker *pickerCountry;
@end
RootViewController.m
#import "RootViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface RootViewController (){
NSString *sURL,*sResponseData, *sRemaining;
NSString *sCode;
}
@end
@implementation RootViewController
@synthesize loginView;
@synthesize txtCountry,txtCode,txtPhone;
@synthesize countryMArray;
@synthesize codeMArray;
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
//=== 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 server to get the return Country response.write
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sURL = @"https://www.share-fitness.com";
sURL = [sURL stringByAppendingString:@"/apps/getcountry.asp?"];
NSURLRequest *requestCountry = [NSURLRequest requestWithURL:[NSURL URLWithString:sURL]];
(void) [[NSURLConnection alloc] initWithRequest:requestCountry delegate:self];
//=== Pass the string to server to get the return CountryCode
sURL = @"https://www.share-fitness.com";
sURL = [sURL stringByAppendingString:@"/apps/getctcode.asp?"];
NSURLRequest *requestCode = [NSURLRequest requestWithURL:[NSURL URLWithString:sURL]];
(void) [[NSURLConnection alloc] initWithRequest:requestCode delegate:self];
//=== Initialie the picker ====
self.pickerCountry = [[DownPicker alloc] initWithTextField:self.txtCountry withData:countryMArray];
[self.pickerCountry addTarget:self
action:@selector(pickerClicked:)
forControlEvents:UIControlEventValueChanged];
}
//=== Pick the countryCode, when Country is selected based on Array Index
-(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])
{
sCode = [codeMArray objectAtIndex:i];
txtCode.text = [@"+"stringByAppendingString:sCode];
break;
}
}
}
【问题讨论】:
标签: objective-c uigesturerecognizer uipickerview