0

I have a UIButton in tableViewCell that perform

-(IBAction)buttonClicked:(id)sender{

UIStoryboard *SB = [UIStoryboard storyboardWithName:@"ABC" bundle:nil];
UIViewController *VC = [SB instantiateViewControllerWithIdentifier:@"someID"];

popoverController  = [[UIPopoverController alloc] initWithContentViewController:VC];
[popoverController setDelegate:self];

..............................

[popoverController presentPopoverFromRect:rect
                                       inView:self.view
                     permittedArrowDirections:UIPopoverArrowDirectionRight
                                     animated:YES];
}

to popover a controller which consist of a UITextField and a UIButton. Now I'm going to pass back the string in UITextField from the popover controller by clicking the UIButton?

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142

2 Answers2

0

retain this view controller in in .h

UIViewController *VC;

change this

UIViewController *VC = [SB instantiateViewControllerWithIdentifier:@"someID"];

to

self.VC = [SB instantiateViewControllerWithIdentifier:@"someID"];

It will be deallocated from the memory when the .h object destroyed from memory.

When you dismiss the popover, still the last popover exists and you can access like

VC.yourTextField.text

When you present again on popover make sure to empty the text field to empty by setting @"".

0

If you are using a TableView with your custom TableViewCell you can create a method in your TableViewCell class for when you push your button:

CustomTableViewCell.h

@interface CustomTableViewCell : UITableViewCell

@property (nonatomic, strong) IBOutlet UIButton *cellButton;
@property (nonatomic, strong) IBOutlet UITextField *cellTextField;

- (NSString*)getText;

@end

CustomTableViewCell.m

- (NSString*)getText {

    return self.cellTextField.text;
}

And in your CustomTableView class:

CustomTableView.m

in delegate method of your CustomTableView set self as delegate to your CustomTableViewCell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CustomTableViewCell *cellCustom = [tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
    ...
    [cellCustom.cellButton setTag:indexPath.row];
    ...
    return cell;
}

And finally in your IBAction method of your button:

- (IBAction)pushButton:(id)sender{

    UIButton *btn = (UIButton*)sender;

    CustomTableViewCell *cellSelected = (CustomTableViewCell *)[self.tableView cellForRowAtIndexPath:btn.tag];

    NSString *strWritten = [cellSelected getText];

    NSLog(@"Text written: %@", strWritten);
}
Fran Martin
  • 2,369
  • 22
  • 19