我创建了自定义 tableView Controller ,在单元格内我放置了一个按钮来打开设备照片库。我的问题是,我无法从 CustomCell.m 打开 imagePickerController,它显示以下错误。
请提供一些想法来解决我的问题。
最佳答案
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/