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

请给我点主意来解决我的问题。
发布于 2014-04-07 05:31:46
TableViewCell是一个视图,您不能present上的视图,而是UIViewController可以处理它。您应该将控件从单元格转移到包含表视图并为其创建自定义单元格的控制器。
就像这样:
自定义单元格.h类:
@protocol changePictureProtocol <NSObject>
-(void)loadNewScreen:(UIViewController *)controller;
@end
@property (nonatomic, retain) id<changePictureProtocol> delegate;然后是Synthesize it in.m。
将其添加到m文件中:
-(IBAction)changePicture:(id)sender
{
// ..... blah blah
[self.delegate loadNewScreen:picker];
}加载此单元格的视图控制器:
- (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
};
}发布于 2015-01-02 11:40:51
提出委托或实例变量的答案是正确的,但是,在大多数情况下,使用特殊视图控制器表示新控制器并不重要。在这些情况下,以下解决方案要简单得多:只需使用应用程序根视图控制器:
UIViewController* activeVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[activeVC presentViewController:'new view controller'
animated:YES
completion:NULL];发布于 2014-04-07 05:24:52
presentViewController:消息存在于视图控制器中。请将控件从单元格委托给viewController,并使用同一行将解决问题。UITableViewCell不响应presentViewController:消息。
https://stackoverflow.com/questions/22904164
复制相似问题