0

My code below will fetch country name and country code from server and store into 2 NSMutablleMArray, countryMArray and codeMArraycode. 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;
        }

    }
}
Hanz Cheah
  • 761
  • 5
  • 15
  • 44

2 Answers2

0

I don't know why you did not use didSelectRow for PickerView. But! I have an idea. What about this? you already use UITapGestureRecognizer for your view..so First, Make one more UITapGestureRecognizer and add on PickerView when pickerView appear. Second when user touch the cell in pickerView trigger pickerView action. and when then remove it from super view.

  UITapGestureRecognizer *tap1;

init

self.tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissPicker)];

In method that you show pickerView..

[self.pickerView setHidden:NO];         
[self.pickerView addGestureRecognizer:tap1];

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

sCode = [NSString stringFormatWith:@"%+%@" , [codeMArray objectAtIndex:row]];
txtCode.text= sCode;  

In dismissPicker

self.textField.text = sCode;
[self.pickerView resignFirstResponder];
[self.pickerView setHidden:YES];
[self.tap1 removeFromSuperView];
User18474728
  • 363
  • 2
  • 11
0

Try to display your response in tableview cells and on click of each cell you can display the data in textfield in new view controller

Refer the following topics:

-UItableview -UItableview delegates(****) -custom UItableviewcell -different view controllers -navigation controllers

Jerry
  • 21
  • 2