How to pass data in conditional unwind segue?

前端 未结 3 408
失恋的感觉
失恋的感觉 2021-01-29 06:25

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

相关标签:
3条回答
  • 2021-01-29 07:08

    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.

    0 讨论(0)
  • 2021-01-29 07:10
    - (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];
    
    0 讨论(0)
  • 2021-01-29 07:28

    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.

    0 讨论(0)
提交回复
热议问题