gpt4 book ai didi

ios - 带有文本字段的 uipickerview 在 textFieldShouldBeginEditing 之后未被调用

转载 作者:行者123 更新时间:2023-11-29 12:21:24 26 4
gpt4 key购买 nike

我正在使用 UIPickerViewDataSource, UIPickerViewDelegate我正在使用 countrypickerview 创建一个 countrytxtfield 并且我能够将一些静态数据传递给 countrypickerview 并且它工作正常。

但是当我尝试从服务响应中获取数据时,我在 connectionDidFinishLoading 中调用相同的 countrypickerview 但它没有被调用

据我所知

我正在通过 textFieldShouldBeginEditing 方法检测 countrytxtfield 的点击

我正在调用一个方法

[self addPickerViewCountry];

其中包含 countrytextfieldcountrypickerview 详细信息

如果我将此方法放在 textFieldShouldBeginEditingcountrypickerview 被调用并且我可以看到数据,如果将相同的方法放在 textFieldShouldBeginEditing 之后和connectionDidFinishLoading 我看不到 countrypickerview

我认为 textFieldShouldBeginEditing 造成了问题。

这可能是我无法理解的简单应用逻辑。下面是我创建 countrypickerview 的代码以及我调用它的时间

viewcontroller.h文件

UIPickerView *myPickerViewCountry;
NSArray *pickerArrayCountry;

@property (weak, nonatomic) IBOutlet UITextField *country;

viewcontroller.m
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{



if( textField == _country ) {
[textField resignFirstResponder];

NSString *refidUser =[[NSUserDefaults standardUserDefaults]
stringForKey:@"refidUser"];
NSLog(@"id %@",refidUser);

NSString *editProfURL = editProfileDetails;

NSURL *url = [NSURL URLWithString:editProfURL];



NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
// NSData *requestBody = [@"UserName=%@,username&Password=%@,password" dataUsingEncoding:NSUTF8StringEncoding];
NSString *requestBody =[[NSString alloc] initWithFormat:@"UserId=%@",refidUser];

NSData *requestBodyy = [requestBody dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:requestBodyy];
NSURLResponse *response = NULL;
NSError *requestError = NULL;
}
return YES;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[self addPickerViewCountry];
}

-(void)addPickerViewCountry{

pickerArrayCountry = [[NSArray alloc]initWithObjects:@"India",
@"India",@"India",@"India", nil];

// pickerArrayCountry = countryNameArr;
NSLog(@"countryarr %@",countryNameArr);
NSLog(@"pickerarrat %@",pickerArrayCountry);
myPickerViewCountry = [[UIPickerView alloc]init];
myPickerViewCountry.dataSource = self;
myPickerViewCountry.delegate = self;
myPickerViewCountry.showsSelectionIndicator = YES;

UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered target:self action:@selector(doneCountry:)];

UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
CGRectMake(0, self.view.frame.size.height-
myPickerViewCountry.frame.size.height-50, 320, 30)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
barButtonDone, nil];
[toolBar setItems:toolbarItems];
_country.inputView = myPickerViewCountry;
_country.inputAccessoryView = toolBar;
}

如果我在

中调用 [self addPickerViewCountry]方法
 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self addPickerViewCountry];
}

我能够看到 UIPickerView,如果我在 connectionDidFinishLoading 中调用它,我将看不到 UIPickerView。任何人都可以建议我问题出在哪里?

最佳答案

你在哪里添加了myPickerViewCountry,比如[self.view addSubview: myPickerViewCountry]?另一件事是你继续分配新的 UIPickerView..

我觉得这样比较合适:

if (myPickerViewCountry == nil)
{
myPickerViewCountry = [[UIPickerView alloc]init];
myPickerViewCountry.dataSource = self;
myPickerViewCountry.delegate = self;
myPickerViewCountry.showsSelectionIndicator = YES;
}

关于你的问题:你没有为你的设置框架

myPickerViewCountry = [[UIPickerView alloc]init];

尝试设置如下:

myPickerViewCountry = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];

嗯..这就是我认为是错误的..

关于ios - 带有文本字段的 uipickerview 在 textFieldShouldBeginEditing 之后未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30528069/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com