这些是我正在处理的文件:
- ChecklistViewController.h
- ChecklistViewController.m
- ChecklistTableViewCell.h
- ChecklistTableViewCell.m
- ChecklistTableViewCell.xib
- DetailViewController.h
- DetailViewController.m
- DetailViewController.xib
我有一个显示 UITableView 的 ChecklistViewController。因为我需要 UITableViewCell 的不同布局,所以我有 UITableViewCell 的子类。我创建了一个 nib 文件 ChecklistTableViewCell.xib,它有一个包含 标签、按钮 和 开关 View 的 UITableViewCell .我已将 UITableViewCell 链接到自定义类 ChecklistTableViewCell。
我已经将 nib 文件中的必要导出链接到 ChecklistTableViewCell 类,并且在 ChecklistViewController cellForRowAtIndexPath 我能够显示标签文本。
在 UITableViewCell 类文件 ChecklistTableViewCell 中,我还链接并实现了一个 IBAction 方法,该方法将在单击按钮时调用。调用此方法时,如何将 DetailViewController 作为弹出窗口打开?
这是我的 IBAction 方法的片段:
-(IBAction)showPopup:(id)sender
{
NSLog(@"popup");
DetailViewController *vp = [[DetailViewController alloc] init];
vp.modalPresentationStyle = UIModalPresentationFormSheet;
vp.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
vp.view.superview.frame = CGRectMake(0, 0, 540, 620);
}
最佳答案
rdelmar 的评论一语中的。因为 presentViewController:animated:completion
是 View Controller 中的一个方法,所以您的 ChecklistTableViewCell
需要让 View Controller 知道按钮点击。
你有两个选择:
假设您的数据源是您的 View Controller ,在您的 View Controller 中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ChecklistTableViewCell *cell = ...;
cell.button.tag = indexPath.row; // or whatever appropriate
[cell.button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)buttonTapped:(UIButton *)button
{
DetailViewController *vc = ...; // get your "popup" accordingly using button.tag
[self presentViewController:vc animated:YES completion:nil];
}
或者你可以声明一个你的 View Controller 遵守的协议(protocol):
// ChecklistTableViewCell.m
@protocol ChecklistTableViewCellDelegate
- (void)buttonTapped:(ChecklistTableViewCell *)sender;
@end
@implementation ChecklistTableViewCell : UITableViewCell
{
...
- (IBAction)showPopup:(id)sender // maybe change the name to something more appropriate
{
if ([self.delegate respondsToSelector:@selector(buttonTapped:)])
{
[self.delegate buttonTapped:self];
}
}
...
}
// ChecklistViewController.m
@implementation ChecklistViewController : ChecklistTableViewCellDelegate
{
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ChecklistTableViewCell *cell = ...;
cell.tag = indexPath.row; // or whatever appropriate
cell.delegate = self;
...
return cell;
}
- (void)buttonTapped:(ChecklistTableViewCell *)sender
{
DetailViewController *vc = ...; // get your "popup" accordingly using sender.tag
[self presentViewController:vc animated:YES completion:nil];
}
...
}
关于ios - ipad-如何在 UITableViewCell 中打开弹出 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25860990/