Domanda

I've created custom tableView Controller, inside the cell i've placed a button to open the device photo library. My problem, i cant able to open imagePickerController from CustomCell.m, its shows below error. enter image description here

Please give some idea to fix my issue.

È stato utile?

Soluzione

TableViewCell is a view, you can not present on views instead UIViewController can handle it. You should transfer the control from your cell to your controller that holds tableview and creates custom cell for it.

Try like this:

Custom Cell .h Class:

@protocol changePictureProtocol <NSObject>
-(void)loadNewScreen:(UIViewController *)controller;
@end

@property (nonatomic, retain) id<changePictureProtocol> delegate;

Then Synthesize it in.m.

Add this in m file:

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

The viewcontroller that loads this cell:

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

   cell.delegate = self;
}

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

Its a psuedocode to give you an idea.

EDIT:

Swift equivalent:

CustomTableViewCell.swift code:

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 code:

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

    };
}

Altri suggerimenti

The answers proposing a delegate or instance variable are correct, but, however, in most cases it is not important to use a special view controller to present the new controller. In these cases the following solution is much simpler: Just use the applications root view controller:

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

presentViewController: message is there for Viewcontroller. Do delegate the control from Cell to viewController and use the same line would solve your problem. UITableViewCell does not responds to presentViewController: message.

You need a UIViewController to present from. There are a few things you can do here. One would be to create a custom delegate protocol with a changePicturePressed callback method. Then you can assign the containing view controller as that delegate and perform the presentation in the delegate method.

The other thing you can do is pass the UIViewController into your cell during initialization and set it as a weak property. Then you can perform the presentation directly from inside the cell using that property.

Make IBOutlet of your button

Then in cellForRowAtIndexPath give target to the button

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

One other way to do this is pass reference of the controller of the CustomCell to the CustomCell and use that to present the new controller.

Swift:

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

You need viewcontroller for presenting the view. You cannot present viewcontroller on tableviewcells.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top