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
You can always use delegates.
Set up a delegate in your add feed page and get mainViewController
to conform to the delegate. Add a delegate method (- (BOOL)canGenerateFeed:(NSURL *)url
) and a delegate property (@property (weak, nonatomic) id <AddFeedControllerDelegate> delegate
).
When your add feed page calls [self.delegate canGenerateFeed:url]
and your mainViewController
conforms to the delegate, the method in mainViewController
is called (that should reply BOOL as stated in the method declaration). Then you can reply YES or NO accordingly, which will be sent back through to the add feed page.
- (UIViewController*)viewControllerForStoryboardName:(NSString*)storyboardName class:(id)class
{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
NSString* className = nil;
if ([class isKindOfClass:[NSString class]])
className = [NSString stringWithFormat:@"%@", class];
else
className = [NSString stringWithFormat:@"%s", class_getName([class class])];
UIViewController* viewController = [storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"%@", className]];
return viewController;
}
// get the view controller
ViewController* viewController = (ViewController*)[self viewControllerForStoryboardName:@"MyStoryboard" class:[OtherViewController class]];
// Pass data here
viewController.data = myData;
// or you can push it
[self.navigationController pushViewController:viewController animated:YES];
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.