2

I have a problem which drives me nuts, how is it possible to invoke a function in a view controller from within a UITableViewCell. - I am doing that in Swift.

So I have a View Controller - called mainVC in this mainVC I have declared a tableView which holds a UITableViewCell (Custom/ Dynamic) in that tableView cell I want call a function which is declared in the mainVC. If I do like self.superview?.myFunc doesn't work.

I hope someone can help me...

1 Answer 1

4

It is not as easy as that unfortunately. What you want to look into is NSNotificationCenter or even better: - Delegates. If you want a better understanding of Delegates I'd recommend this tutorial: http://www.raywenderlich.com/75289/swift-tutorial-part-3-tuples-protocols-delegates-table-views

If you don't want to read all that, I'll try my best to explain it here.

Let's say you have two view controllers: mainVC and otherVC.

Above the class definition in otherVC, add the following code:

protocol OtherVCDelegate {
   func cellClicked()
}

Inside the class in otherVC, add the following property:

var delegate: OtherVCDelegate?

In the didSelectRow (or where you execute the code), add the following code:

self.delegate?.cellClicked()

Now, back in mainVC: Make mainVC conform to this delegate by adding it to the class like this:

class MainViewController: UIViewController, DetailDelegate {
}

In MainViewController add the delegate function and insite that one put your function you want to execute:

func cellClicked() {
    println("Cell was clicked")
    myFunc()
}

The last thing you have to do is make MainViewController the delegate of OtherViewController. This has to be done where you access the otherVC from the mainVC. Let's say it is done in prepareForSegue:

if segue.identifier == "showDetail" {
       (segue.destinationViewController as DetailViewController).delegate = self
    }

It is a little bit complicated at first, but once you get a grasp of it it's a walk in the park.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your quick comment, I accepted the answer although it is not 100% what I looked for. I know how to use protocols and delegates but as I know, I can't set the delegate in a TableViewCell either use prepareForSegue in a TableViewCell class? All my code is happening in only 1 viewController class and one UITableViewCell class, so no segue is happening...and that for me seems to be the tricky part... Any ideas?
Aa, I see. Well, I still believe that delegates are the best way to go. You can set a delegate for a UITableViewCell as well. Use the code I provided above, but put all the code from the otherVC to your UITableViewCell subclass. Then, in your UITableViewCell subclass you can add self.delegate?.cellClicked() in touchesBegan. Remember to make mainVC the delegate by passing in cell.delegate = self in cellForRow. I've tested it and it should work!
On the other hand, if all of this is happening in the same view controller this shouldn't really be necessary. You should be able to just use a regular didSelectRow and fire the function from there. It's hard to tell without seeing more of your code. Let me know if you figure it out.
Thanks again, yes I got it sorted out :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.