Skip to main content
 首页 » 操作系统

swift之我设法转到一个空白的 View Controller 。我该如何返回

2024年01月01日37虾米哥

// 
//  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() 
    } 
 
} 

所以我向左滑动,它会带我到一个新的 viewViewcontroller,我向右滑动它会带我到另一个空白 View Controller 。我如何告诉这些空白 View Controller 返回到主视图 Controller ?

请您参考如下方法:

那个 View Controller 中有导航栏吗?

If you do, then you can simply do:

self.navigationController.popViewControllerAnimated(YES) 

如果你不这样做,那么你只需要

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

(也就是说,执行一个 segue back 到您来自的 View Controller )。 Segues 不一定是推和弹出的东西。