【问题标题】:Objective-C: Tap on UIDownPicker and get the value to UITextfieldObjective-C:点击 UIDownPicker 并获取 UITextfield 的值
【发布时间】:2023-03-13 04:41:01
【问题描述】:

我下面的代码将从服务器获取国家名称和国家代码并存储到2 NSMutablleMArraycountryMArraycodeMArraycode。当用户选择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


    【解决方案1】:

    我不知道您为什么不将 didSelectRow 用于 PickerView。但!我有个主意。那这个呢?您已经使用 UITapGestureRecognizer 作为您的视图..so 首先,再制作一个 UITapGestureRecognizer 并在 pickerView 出现时添加 PickerView。 其次,当用户触摸pickerView 中的单元格时触发pickerView 操作。 以及何时将其从超级视图中删除。

      UITapGestureRecognizer *tap1;
    

    初始化

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

    在显示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;  
    

    在dismissPicker中

    self.textField.text = sCode;
    [self.pickerView resignFirstResponder];
    [self.pickerView setHidden:YES];
    [self.tap1 removeFromSuperView];
    

    【讨论】:

      【解决方案2】:

      尝试在表格视图单元格中显示您的响应,单击每个单元格时,您可以在新视图控制器的文本字段中显示数据

      参考以下主题:

      -UItableview -UItableview 代表(****) -自定义 UItableviewcell -不同的视图控制器 -导航控制器

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-23
        • 1970-01-01
        • 2013-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-28
        相关资源
        最近更新 更多