My code below will fetch country name and country code from server and store into 2 NSMutablleMArray
, countryMArray
and codeMArray
code. When user select the txtCountry UITextField
It will call a picker list
and when user select the country and hit Done, it will based on index and get the Country Code from codeMArray
and put in txtCode UITexField
Instead of clicking done, I want user to be able to tap on country on the picker list and the value will be transffered to UITextField
I have tried this This Thread but it is not working. I have run out of idea, please help.
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;
}
}
}