在textFieldShouldBeginEditing之后没有调用textfield的uipickerview

时间:2015-05-29 11:11:18

标签: ios objective-c uipickerview

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

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

根据我的说法

我正在检测来自countrytxtfield方法的textFieldShouldBeginEditing

我正在调用方法

[self addPickerViewCountry];

包含countrytextfieldcountrypickerview详细信息

如果我将此方法放在textFieldShouldBeginEditing countrypickerview内,我可以看到数据,如果在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。有人可以建议我问题出在哪里吗?

3 个答案:

答案 0 :(得分:1)

您在哪里添加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)];

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

答案 1 :(得分:0)

我不明白connectionDidFinishLoading方法如何调用? 因为您从不在服务器上发送请求。

您只能在URLRequest方法中创建一个textFieldShouldBeginEditing对象,而不会在服务器上发送它。那么,connectionDidFinishLoading方法如何被调用。

你能发送整个.m课吗?所以,我会帮助你。你是否在这堂课中打电话给任何其他请求。

答案 2 :(得分:0)

在这些行中,

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;   

您创建NSMutableURLRequest,但您永远不会启动请求。 我非常怀疑-connectionDidFinishLoading甚至被称为。仔细检查,断点,请填写完整的代码,以便我们有一些你做错的背景。