来自 xib 中自定义 TableCell 的 PresentViewController [英] PresentViewController from custom TableCell in xib

查看:32
本文介绍了来自 xib 中自定义 TableCell 的 PresentViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了自定义 tableView 控制器,在单元格内我放置了一个按钮来打开设备照片库.我的问题,我无法从 CustomCell.m 打开 imagePickerController,它显示以下错误.

I've created custom tableView Controller, inside the cell i've placed a button to open the device photo library. My problem, i cant able to open imagePickerController from CustomCell.m, its shows below error.

请给出一些解决我的问题的想法.

Please give some idea to fix my issue.

推荐答案

TableViewCell 是一个视图,你不能在视图上 present 而 UIViewController 可以处理它.您应该将控制权从单元格转移到包含 tableview 的控制器并为其创建自定义单元格.

TableViewCell is a view, you can not present on views instead UIViewController can handle it. You should transfer the control from your cell to your controller that holds tableview and creates custom cell for it.

试试这个:

自定义单元格 .h 类:

@protocol changePictureProtocol <NSObject>
-(void)loadNewScreen:(UIViewController *)controller;
@end

@property (nonatomic, retain) id<changePictureProtocol> delegate;

然后.m.中合成

m 文件中添加:

-(IBAction)changePicture:(id)sender
{
    // ..... blah blah
    [self.delegate loadNewScreen:picker];
}

加载此单元格的视图控制器:

The viewcontroller that loads this cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // create cell here

   cell.delegate = self;
}

-(void)loadNewScreen:(UIViewController *)controller;
{
  [self presentViewController:controller animated:YES completion:nil];
}

这是一个给你一个想法的伪代码.

Its a psuedocode to give you an idea.

Swift 等效:

CustomTableViewCell.swift 代码:

protocol ChangePictureProtocol : NSObjectProtocol { 
    func loadNewScreen(controller: UIViewController) -> Void;  
}

class CustomTableViewCell: UITableViewCell {

    // Rest of the class stuff

    weak var delegate: ChangePictureProtocol?

    @IBAction func changePicture(sender: AnyObject)->Void
    {
        var pickerVC = UIImagePickerController();
        if((delegate?.respondsToSelector("loadNewScreen:")) != nil)
        {
           delegate?.loadNewScreen(pickerVC);
        }  
    }
}

ViewController.swift 代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as CustomTableViewCell!

    cell.delegate = self;

    return cell;
}

func loadNewScreen(controller: UIViewController) {
    self.presentViewController(controller, animated: true) { () -> Void in

    };
}

这篇关于来自 xib 中自定义 TableCell 的 PresentViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆