Ram*_*has 11 objective-c uitableview uiimagepickercontroller ios
我已经创建了自定义tableView控制器,在单元格中我放置了一个按钮来打开设备照片库.我的问题,我无法从CustomCell.m打开imagePickerController,它显示错误. 
请给出一些想法来解决我的问题.
Nev*_*ess 30
TableViewCell是一个视图,你不能present在视图上代替UIViewController可以处理它.您应该将控件从单元格传输到包含tableview的控制器,并为其创建自定义单元格.
试试这样:
自定义单元.h类:
@protocol changePictureProtocol <NSObject>
-(void)loadNewScreen:(UIViewController *)controller;
@end
@property (nonatomic, retain) id<changePictureProtocol> delegate;
Run Code Online (Sandbox Code Playgroud)
那么Synthesize it in.m.
在m文件中添加:
-(IBAction)changePicture:(id)sender
{
// ..... blah blah
[self.delegate loadNewScreen:picker];
}
Run Code Online (Sandbox Code Playgroud)
加载此单元格的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];
}
Run Code Online (Sandbox Code Playgroud)
它是一个给你一个想法的伪代码.
编辑:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
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
};
}
Run Code Online (Sandbox Code Playgroud)
Lab*_*Ars 11
提出委托或实例变量的答案是正确的,但是,在大多数情况下,使用特殊视图控制器来呈现新控制器并不重要.在这些情况下,以下解决方案要简单得多:只需使用应用程序根视图控制器:
UIViewController* activeVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[activeVC presentViewController:'new view controller'
animated:YES
completion:NULL];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15678 次 |
| 最近记录: |