PresentViewController from custom TableCell in xib

2020-01-26 10:27发布

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.

7条回答
我只想做你的唯一
2楼-- · 2020-01-26 11:12

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];
查看更多
趁早两清
3楼-- · 2020-01-26 11:16

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];
查看更多
叼着烟拽天下
4楼-- · 2020-01-26 11:16

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;
}
查看更多
做自己的国王
5楼-- · 2020-01-26 11:21

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.

查看更多
SAY GOODBYE
6楼-- · 2020-01-26 11:21

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

查看更多
Anthone
7楼-- · 2020-01-26 11:22

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.

查看更多
登录 后发表回答