//
// 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,我向右滑动它带我到另一个空白视图控制器。如何告诉这些空白视图控制器切换回主视图控制器?
答案 0 :(得分:2)
该视图控制器中是否有导航栏?
If you do, then you can simply do:
self.navigationController.popViewControllerAnimated(YES)
如果你不这样做,那么你只需要
self.performSegueWithIdentifier("segueSwipeLeft", sender: nil)
(也就是说,对您来自的视图控制器执行segue back )。 Segues不一定是推动和流行的东西。
答案 1 :(得分:1)
Segues创建其视图控制器的实例。这在向前移动时是有意义的,但如果在向后移动时“执行SegueWithIdentifier”,则可能您没有返回到先前的视图控制器,而是您正在创建并呈现之前的新实例查看控制器。
例如,假设您有两个视图控制器,A和B,并且A上有一个文本字段,其中包含用户指定的值。然后你转向B.然后你使用标准的segue回到A.文本将不在A的文本字段中,因为你正在查看A的新实例,而不是该视图控制器的原始实例。 / p>
如果你想备份,有Unwind segues,它是一种特殊的segue,可以让你返回上一个实例。它们被装配到故事板编辑器中场景顶部的绿色“退出”按钮。展开segues(有时称为Exit Segues)非常有趣,因为它们不仅可以让您放松到前一个视图控制器,而且可以一直通过深层视图控制器展开,作为展开的一部分,它们可以在目标上调用不同的方法视图控制器,例如指示在源视图控制器上点击了取消或保存按钮。
以编程方式,如果您的视图控制器是以模态方式呈现的,您还可以使用dismissViewController:animated:completion:
进行备份。