I managed to segue to a blank view controller. How

2019-08-09 20:13发布

问题:

//
//  ViewController.swift
//  FunFacts
//
//  Created by Alex Macleod on 4/10/14.
//  Copyright (c) 2014 Alex Macleod. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var funFactLabel: UILabel!
    @IBOutlet weak var funFactButton: UIButton!

    @IBOutlet weak var swipeView: UIView!

//    let swipeRec = UISwipeGestureRecognizer()

    let factBook = FactBook()
    let colorWheel = ColorWheel()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

//        swipeRec.addTarget(self, action: "swipedView")
//        swipeView.addGestureRecognizer(swipeRec)
//        swipeView.userInteractionEnabled = true
        var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
        swipeRight.direction = UISwipeGestureRecognizerDirection.Right
        self.view.addGestureRecognizer(swipeRight)

        var swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
        swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
        self.view.addGestureRecognizer(swipeLeft)

        var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
        swipeDown.direction = UISwipeGestureRecognizerDirection.Down
        self.view.addGestureRecognizer(swipeDown)

        var swipeUp = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
        swipeUp.direction = UISwipeGestureRecognizerDirection.Up
        self.view.addGestureRecognizer(swipeUp)

        funFactLabel.text = factBook.randomFact()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    func respondToSwipeGesture(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.Right:
//                swipedAlertViewRight()
                self.performSegueWithIdentifier("segueSwipeRight", sender: nil)

            case UISwipeGestureRecognizerDirection.Left:
//                swipedAlertViewLeft()
                swipedLeft()

            case UISwipeGestureRecognizerDirection.Down:
                var randomColor = colorWheel.randomColor()
                view.backgroundColor = randomColor
                funFactButton.tintColor = randomColor

                funFactLabel.text = factBook.randomFact()

            case UISwipeGestureRecognizerDirection.Up:
                var randomColor = colorWheel.randomColor()
                view.backgroundColor = randomColor
                funFactButton.tintColor = randomColor

                funFactLabel.text = factBook.randomFact()

            default:
                break
            }
        }
    }

    func swipedLeft() {

        self.performSegueWithIdentifier("segueSwipeLeft", sender: nil)


    }

//    func swipedAlertViewRight(){
//        let tapAlert = UIAlertController(title: "Swiped", message: "You just swiped right", preferredStyle: UIAlertControllerStyle.Alert)
//        tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
//        self.presentViewController(tapAlert, animated: true, completion: nil)
//    }
//    
//    func swipedAlertViewLeft(){
//        let tapAlert = UIAlertController(title: "Swiped", message: "You just swiped left", preferredStyle: UIAlertControllerStyle.Alert)
//        tapAlert.addAction(UIAlertAction(title: "OK", style: .Destructive, handler: nil))
//        self.presentViewController(tapAlert, animated: true, completion: nil)
//    }

    @IBAction func showFunFact() {
        var randomColor = colorWheel.randomColor()
        view.backgroundColor = randomColor
        funFactButton.tintColor = randomColor

        funFactLabel.text = factBook.randomFact()
    }

}

So I swipe left and I it takes me to a new viewViewcontroller, I swipe right it takes me to another blank view controller. How do I tell these blank view controllers to segue back to the main view controller?

回答1:

Do you have a navigation bar in that view controller?

If you do, then you can simply do:

self.navigationController.popViewControllerAnimated(YES)

If you do not, then you simply need to

self.performSegueWithIdentifier("segueSwipeLeft", sender: nil)

(which is to say, perform a segue back to the view controller you came from). Segues are not necessarily a push and pop thing.



回答2:

Segues create instances of their view controllers. This makes sense when moving in a forward direction, but if you "performSegueWithIdentifier" when moving in a backward direction, it's likely you're not returning to the previous view controller, but rather you're creating and presenting a new instance of the previous view controller.

For example, let's say you two view controllers, A and B, and A has a text field on it that has a value specified by the user. Then you segue to B. Then you use a standard segue back to A. The text won't be in the text field on A because you're looking at a new instance of A, not the original instance of that view controller.

If you want to back-up, there are Unwind segues, which are a special kind of segue to return you to a previous instance. They are rigged up to the green "exit" button at the top of your scene in the storyboard editor. Unwind segues (sometimes called Exit Segues) are interesting because they let you unwind not just to the previous view controller, but all the way back through a deep stack of view controllers, and as part of the unwind they can call different methods on the destination view controller, such as indicating that a Cancel or Save button was tapped on the source view controller.

Programatically, if your view controller was presented modally, you can also use dismissViewController:animated:completion: to back up.