ios - ipad-如何在 UITableViewCell 中打开弹出 View Controller

标签 ios objective-c ipad uitableview

这些是我正在处理的文件:

  1. ChecklistViewController.h
  2. ChecklistViewController.m
  3. ChecklistTableViewCell.h
  4. ChecklistTableViewCell.m
  5. ChecklistTableViewCell.xib
  6. DetailViewController.h
  7. DetailViewController.m
  8. 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/

相关文章:

ios - Reachability reachable和unreachableBlock被多次调用?

javascript - 使用angularjs单击输入后运行textbox.blur

iphone - 如何在内部分发具有各种内容的 iphone 应用程序?

javascript - 如何通过 HTML 5 javascript 在本地 ipad/iphone 上存储数据并在设备上线时提交它们

ios - AdMob 同意书未显示?

ios - 照片库中的视频图 block

ios - 如何为 UIButton 创建的自定义背景艺术实现 scale-9(9 切片)

objective-c - 如何从 iOS 中的 URL 获取图像大小

ios - 如何在谷歌地图 ios 中标记 map 标记

ios - 更改 UIButton 的 imageview 的 alpha