ios - 来自 xib 中自定义 TableCell 的 PresentViewController

标签 ios objective-c uitableview uiimagepickercontroller

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

请提供一些想法来解决我的问题。

最佳答案

TableViewCell 是一个 View ,您不能在 View 上呈现,而 UIViewController 可以处理它。您应该将控制权从您的单元格转移到包含 tableview 并为其创建自定义单元格的 Controller 。

像这样尝试:

自定义单元格.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];
}

加载此单元格的 View Controller :

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

   cell.delegate = self;
}

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

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

编辑:

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

    };
}

关于ios - 来自 xib 中自定义 TableCell 的 PresentViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22904164/

相关文章:

ios - 从 iTunes Connect 接收有关 IAP 状态更改事件的通知时遇到问题

objective-c - 在非 ARC 项目中包含 ARC header

ios - cell.contentView.viewWithTag 在静态 tableView 中不起作用

ios - UINavigationController 中的 UITableViewController 来表示未知深度的树结构

ios - 夹在 willBeginEditingRowAtIndexPath 和 didEndEditingRowAtIndexPath 之间

ios - 单元格不快速显示 subview

iphone - 在联系页面添加地址簿

objective-c - 如何从 NSManagedObjectContext 中删除新对象

ios - 将图像转换为Base64编码(状态码为500)

objective-c - 有没有办法检查进程是 64 位还是 32 位?