I try to build an rss reader. On the \"adding feed\" page, if I tap the \"add\" button, I hope to check if the feed is successfully added. If it is added, then trigger the unwin
Like Schemetrical said, using a delegate is an easy way to access the methods in your MainViewController.
Since you tagged this as Swift, I'll also give you a small example of a delegate in Swift.
First you create a protocol:
protocol NameOfDelegate: class { // ":class" isn't mandatory, but it is when you want to set the delegate property to weak
func someFunction() -> String // this function has to be implemented in your MainViewController so it can access the properties and other methods in there
}
In your MainViewController you have to add:
class MainViewController: UIViewController, NameOfDelegate {
// your code
@IBAction func button(sender: UIButton) {
performSegueWithIdentifier("toOtherViewSegue", sender: self)
}
fun someFunction() -> String {
// access the other methods and return it
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toOtherViewSegue" {
let destination = segue.destinationViewController as! OtherViewController
destination.delegate = self
}
}
}
And the last step, you'll have to add a property of the delegate, so you can "talk" to it. Personally I imagine this property to be a gate of some sort, between the two view controllers so they can talk to each other.
class OtherViewController: UIViewController {
weak var delegate: NameOfDelegate?
@IBAction func button(sender: UIButton) {
if delegate != nil {
let someString = delegate.someFunction()
}
}
}
I assumed you used a segue to access your other ViewController since you mentioned it in your post. This way, you can just "talk" to your MainViewController.
EDIT:
As for the unwind. This also can be done through a segue.
@IBAction func unwindToConfigMenu(sender: UIStoryboardSegue) { }
to your MainViewController.OtherViewController
. Click on the round yellow with a square inside to make sure the ViewController is selected and not some elements inside.OtherViewController
It appears i can't post any code anymore? :o will add it later.