How to pass data in conditional unwind segue?

假装没事ソ 提交于 2019-12-02 13:00:37

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.

  1. add: @IBAction func unwindToConfigMenu(sender: UIStoryboardSegue) { } to your MainViewController.
  2. In your storyboard there are 3 icons at the top of your OtherViewController. Click on the round yellow with a square inside to make sure the ViewController is selected and not some elements inside.
  3. Control drag (or right mouse drag) from the same round yellow with a square inside to the most right red square icon. Doing so pops up a menu where you can select the unwind segue.
  4. Click on the new segue you just created. Give it an identifier like "backToMain"
  5. Add something similar as the code below to OtherViewController

It appears i can't post any code anymore? :o will add it later.

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!