PresentViewController来自xib中的自定义TableCell

我已经创build了自定义tableView控制器,在单元格内放置了一个button来打开设备照片库。 我的问题,我无法打开从CustomCell.m imagePickerController,其显示下面的错误。 在这里输入图像说明

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

TableViewCell是一个视图,你不能在视图上present ,而是UIViewController可以处理它。 您应该将控件从您的单元格传输到保存tableview的控制器,并为其创build自定义单元格。

尝试像这样:

自定义单元格.h类:

 @protocol changePictureProtocol <NSObject> -(void)loadNewScreen:(UIViewController *)controller; @end @property (nonatomic, retain) id<changePictureProtocol> delegate; 

然后Synthesize it in .m中进行Synthesize it in

将其添加到m文件中:

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

加载这个单元格的viewcontroller:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // create cell here cell.delegate = self; } -(void)loadNewScreen:(UIViewController *)controller; { [self presentViewController:controller animated:YES completion:nil]; } 

它是一个伪代码给你一个想法。

编辑:

迅速等值:

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

提出委托或实例variables的答案是正确的,但是,在大多数情况下,使用特殊的视图控制器来呈现新的控制器并不重要。 在这些情况下,以下解决scheme更为简单:只需使用应用程序根视图控制器即可:

 UIViewController* activeVC = [UIApplication sharedApplication].keyWindow.rootViewController; [activeVC presentViewController:'new view controller' animated:YES completion:NULL]; 

presentViewController:消息是Viewcontroller的。 将控制从Cell委派给viewController并使用同一行可以解决您的问题。 UITableViewCell不响应presentViewController:消息。

你需要一个UIViewController来呈现。 有几件事你可以在这里做。 一个是用changePicturePressedcallback方法创build一个自定义委托协议。 然后,可以将包含的视图控制器作为该委托进行分配,并在委托方法中执行演示。

你可以做的另一件事是在初始化时将UIViewController传递给你的单元格,并将其设置为一个弱属性。 然后,您可以使用该属性直接在单元格内执行演示文稿。

让你的buttonIBOutlet

然后在cellForRowAtIndexPath给这个button的目标

 CustomCell *customCell=[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"]; [customCell.yourButton addTarget:self action:@selector(yourButtonAction:) forControlEvents:UIControlEventTouchUpInside]; 

另一种方法是将CustomCell的控制器的引用传递给CustomCell,并使用它来呈现新的控制器。

迅速:

CustomCell.swift

 class CustomTableViewCell: UITableViewCell { // Rest of the class stuff var parentViewController: UIViewController? = nil gotoNewControllerBtn.addTarget(self, action: #selector(gotoNewController), for: .touchUpInside) func gotoNewController() { let newViewController = NewViewController() parentViewController.present(newViewController, animated: true, completion: nil) } } 

ViewController.swift

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

您需要viewcontroller呈现视图。 您不能在tableviewcells上呈现viewcontroller。