Obj-C UIDownpicker将在滚动期间用另一个值填充另一个文本字段

时间:2018-11-19 03:26:31

标签: objective-c nsmutablearray uipickerview

我下面的代码将从服务器获取国家名称和国家/地区代码,并将其存储到2 NSMutablleMArraycountryMArraycodeMArray代码中。当用户点击txtCountry UITextField时,它将调用down picker;当用户选择国家/地区并点击完成时,它将基于索引并获得Country Code from codeMArray并将代码放入{{1} }。

我想知道如何在滚动txtCode UITexField时立即填充txtCode UITextfield。如下图所示。而不是让用户完成任务。当用户滚动down picker时,down picker Country Name将自动填充txtCode UITexField。有没有办法做到这一点。

在*

中添加了以下代码

我添加了downpicker.m和downpicker.h以及*中的代码,但它从未触发。请帮忙,谢谢。

When scroll to Malaysia auto fill txtCode with Malaysia Country Code

When scroll to China auto fill txtCode with China Country Code

RootViewController.h

Code

RootViewController.m

#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

Downpicker.h

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

}

//**** ADDED the following but will never trigger ?
 - (void)pickerView:(UIDownPicker *)pickerCountry didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    sCode = [codeMArray objectAtIndex:row];
    txtCode.text = [@"+"stringByAppendingString:sCode];
}
//**********************
 //=== 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;
        }

    }
}

Downpicker.m

#import <UIKit/UIKit.h>

@interface DownPicker : UIControl<UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate>
{
    UIPickerView* pickerView;
    IBOutlet UITextField* textField;
    NSArray* dataArray;
    NSString* placeholder;
    NSString* placeholderWhileSelecting;
    NSString* toolbarDoneButtonText;
    NSString* toolbarCancelButtonText;
    UIBarStyle toolbarStyle;
}

@property (nonatomic) NSString* text;
@property (nonatomic) NSInteger selectedIndex;

-(id)initWithTextField:(UITextField *)tf;
-(id)initWithTextField:(UITextField *)tf withData:(NSArray*) data;

@property (nonatomic) BOOL shouldDisplayCancelButton;

/**
Sets an alternative image to be show to the right part of the textbox (assuming that showArrowImage is set to TRUE).
 @param image
 A valid UIImage
*/
-(void) setArrowImage:(UIImage*)image;

-(void) setData:(NSArray*) data;
-(void) setPlaceholder:(NSString*)str;
-(void) setPlaceholderWhileSelecting:(NSString*)str;
-(void) setAttributedPlaceholder:(NSAttributedString *)attributedString;
-(void) setToolbarDoneButtonText:(NSString*)str;
-(void) setToolbarCancelButtonText:(NSString*)str;
-(void) setToolbarStyle:(UIBarStyle)style;

/**
 TRUE to show the rightmost arrow image, FALSE to hide it.
 @param b
 TRUE to show the rightmost arrow image, FALSE to hide it.
*/
-(void) showArrowImage:(BOOL)b;

-(UIPickerView*) getPickerView;
-(UITextField*) getTextField;

/**
Retrieves the string value at the specified index.
@return
The value at the given index or NIL if nothing has been selected yet.
*/
-(NSString*) getValueAtIndex:(NSInteger)index;

/**
Sets the zero-based index of the selected item: -1 can be used to clear        selection.
@return
The value at the given index or NIL if nothing has been selected yet.
*/
-(void) setValueAtIndex:(NSInteger)index;
@end

3 个答案:

答案 0 :(得分:0)

也许您的Downpicker是UIPickerView的子类,您应该实现它的委托方法,名为:

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

可以解决您的问题。

答案 1 :(得分:0)

首先,我建议您使用NSDictionary而不是两个数组:您可以将字典的关键字设置为country short,将值设置为phone并将其用作:

NSString *phoneCode = self.codeDict[@"en"];

第二,为DownPicker创建一个类别,并覆盖[pickerView:didSelectRow:inComponent]。然后,您可以从那里发布与“完成”操作相同的通知,也可以为此创建协议。

答案 2 :(得分:0)

正如提到的其他答案一样,此处的键应位于pickerView:didSelectRow:inComponent委托方法中。

似乎您已将其添加到RootViewController中,但RootViewController不是UIPickerViewDelegate(UIPickerView的子类是)。为了解决这个问题,您需要将RootViewController调整为UIPickerViewDelegate,并将pickerView委托的实例设置为RootViewController:

  

@interface RootViewController:UIViewController

@interface RootViewController : UIViewController     <UITextFieldDelegate,UITableViewDelegate, UIPickerViewDelegate>

...

然后在rootViewController的viewDidLoad中将选择器视图的委托设置为self:

self.pickerCountry.delegate = self