【问题标题】:Close popover by clicking UIButton in popover that created from program通过单击从程序创建的弹出窗口中的 UIButton 关闭弹出窗口
【发布时间】:2014-03-25 21:22:18
【问题描述】:

我在 tableViewCell 中有一个 UIButton 可以执行

-(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];
}

弹出一个由 UITextField 和 UIButton 组成的控制器。现在我要通过单击 UIButton 从弹出控制器传回 UITextField 中的字符串?

【问题讨论】:

  • z您的要求是什么?你想传递文本还是关闭弹出框?

标签: ios storyboard popover pass-data


【解决方案1】:

如果您将 TableView 与您的自定义 TableViewCell 一起使用,您可以在 TableViewCell 类中为您按下按钮时创建一个方法:

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;
}

在您的 CustomTableView 类中:

CustomTableView.m

在 CustomTableView 的委托方法中,将 self 设置为 CustomTableViewCell 的委托

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

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

最后在按钮的 IBAction 方法中:

- (IBAction)pushButton:(id)sender{

    UIButton *btn = (UIButton*)sender;

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

    NSString *strWritten = [cellSelected getText];

    NSLog(@"Text written: %@", strWritten);
}

【讨论】:

    【解决方案2】:

    将此视图控制器保留在 .h 中

    UIViewController *VC;
    

    改变这个

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

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

    当 .h 对象从内存中销毁时,它将从内存中释放。

    当您关闭弹出框时,最后一个弹出框仍然存在,您可以像访问一样访问

    VC.yourTextField.text
    

    当您再次出现在弹出窗口时,请确保通过设置 @"" 将文本字段清空。

    【讨论】:

    • 我在 .h 中添加了保留 VC 并在 .m 中合成它。但是 VC.yourTextView 没有出现。有没有其他方法,例如向后传递委托?
    • 在 VC.h 中添加 yourTextView
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多