Talha Ch Talha Ch - 1 month ago 9
iOS Question

How to find which textField has loaded the TableView

I've a

ViewController
which two
UITextFields
. First text field is area name and when this text field is tapped it opens up (View displayed using KGModal) a
UITableViewController
which sends call to server and fetch and display area names.
The second text field is dependent on the area text field. It's the zip code field and when user select the area and the tap on the zip code field it send a call with area name to fetch zip codes of that area.

My question is that how can I use the same table view controller class to send a different call based on the sender text field. I'm using KGModal to display the view.

UPDATE:

Here is the delegate method of UITextField where I'm sending the
id
of the sender.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

if (textField==self.areaField) {
[self.view endEditing:YES];
[self showTableView:self.areaField];
return NO;
}
else if (textField==self.zipField)
{
[self.view endEditing:YES];
[self showTableView:self.zipField];
return NO;
}
else return YES;
}


And in the method I'm using it as

-(void)showTableView:(id)sender
{
ServiceEnMTableViewController *ivc = [self.storyboard instantiateViewControllerWithIdentifier:@"CountryVC"];

//ivc.delegate = self;

ivc.view.frame = CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen] bounds]) - 40, CGRectGetHeight([[UIScreen mainScreen] bounds]) - 80);
[[KGModal sharedInstance] setCloseButtonType:KGModalCloseButtonTypeRight];
[[KGModal sharedInstance] showWithContentViewController:ivc andAnimated:YES];
[[KGModal sharedInstance] setTapOutsideToDismiss:YES];
[[KGModal sharedInstance] setModalBackgroundColor:[UIColor clearColor]];
[[KGModal sharedInstance] setBackgroundDisplayStyle:KGModalBackgroundDisplayStyleSolid];
}


So how to pass the sender here?

Answer

Define Enum like this.

typedef NS_ENUM(NSUInteger, TextFieldsIdentifier) {
        kAreaField,
        kZipField,
    };

Now your textField Delegate will be look like this

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    if (textField==self.areaField) {
        [self.view endEditing:YES];
        [self showTableView:kAreaField];
        return NO;
    }
    else if (textField==self.zipField)
    {
        [self.view endEditing:YES];
        [self showTableView:kZipField];
        return NO;
    }
    else return YES;
}

and your method showTableView: will look like this

-(void)showTableView:(TextFieldsIdentifier) textFieldIdentifier{
    ServiceEnMTableViewController *ivc = [self.storyboard      instantiateViewControllerWithIdentifier:@"CountryVC"];
    //make property like @property(nonatomic,assign) TextFieldsIdentifier textFieldIdentifier; in ServiceEnMTableViewController and assign value
    ivc.textFieldIdentifier = textFieldIdentifier;
    //ivc.delegate = self;

    ivc.view.frame = CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen]   bounds]) - 40, CGRectGetHeight([[UIScreen mainScreen] bounds]) - 80);
    [[KGModal sharedInstance] setCloseButtonType:KGModalCloseButtonTypeRight];
    [[KGModal sharedInstance] showWithContentViewController:ivc andAnimated:YES];
    [[KGModal sharedInstance] setTapOutsideToDismiss:YES];
    [[KGModal sharedInstance] setModalBackgroundColor:[UIColor clearColor]];
    [[KGModal sharedInstance]  setBackgroundDisplayStyle:KGModalBackgroundDisplayStyleSolid];
  }
Comments