Swipe Back

Hey I wanted to ask you how I can implement the Swipe back Gesture in Swift. So I want that the user start at the Home View Controller and then he can tap on the "happy number Rechner"-Button and then the calculator View Controller appears. Like the same way with the transitions in the Settings App of iOS. And if you swipe left the Home View should appear again. Thank you!


import UIKit






let alert = UIAlertController(title: "Warning",
               message: "Your number is too big.",
               preferredStyle: .alert)


   
   
  class ViewController: UIViewController {
     
    
     
     
    
     
    // Verbindungen zu Storyboard
     
     
    @IBAction func rechnerEntry(_ sender: Any) {
    }
     
    @IBOutlet weak var resultLabel: UILabel!
     
    @IBOutlet weak var buttons: UIButton!
     
    @IBAction func clear(_ sender: Any) {
       
      calculatorLabel.text = ""
      number = 0
    }
     
    override func viewDidLoad() {
      super.viewDidLoad()
       
     
       
       
      buttons.backgroundColor = UIColor.gray
    }
     
    // Aufführung happyFuncs
     
    func happyFunc(number: Int) -> Int {
       
      var myNum = number
      var sum = 0
      while myNum > 0 {
        let x = myNum % 10
        sum += (x * x)
        myNum /= 10
      }
      return sum
    }
     
    // happyCheckerFunc
     
    func happyChecker(_ x: Int) -> Bool {
      var alreadychecked: Set<Int> = Set()
      var result = happyFunc(number: x)
       
       
      while !alreadychecked.contains(result) {
         
        if result == 1 {
          resultLabel.text = "your number is a happy number"
          return true
        }
         
        alreadychecked.insert(result)
        result = happyFunc(number: result)
      }
      resultLabel.text = "your number is a unhappy number"
      return false
    }
     
    // Eingabe Werte
     
    var number = 0
    var x = 0
     
    @IBOutlet weak var calculatorLabel: UILabel!
     
    var isTypingNumber = false
     
    // Nummer getippt
     
    @IBAction func numberTapped(_ sender: UIButton) {
       
      let number = sender.currentTitle ?? (sender.titleLabel?.text ?? "0")
       
      if isTypingNumber {
        calculatorLabel.text = calculatorLabel.text! + number
      } else {
        calculatorLabel.text = number
        isTypingNumber = true
      }
       
    }
     
     
    // Überprüfung happy?
     
    @IBAction func happycheck(_ sender: UIButton) {
       
      number = Int(calculatorLabel.text!)!
       
      x = number
       
      if x > 9999999999999999 {
        alert.addAction(UIAlertAction(title: "OK",
                       style: .default,
                       handler: { _ in
           print("OK tap")
        }))
        present(alert, animated: true, completion: nil)
         
      }
      func happyFunc(number: Int) -> Int {
         
        var myNum = number
        var sum = 0
        while myNum > 0 {
          let x = myNum % 10
          sum += (x * x)
          myNum /= 10
        }
        return sum
      }
       
       
      func happyChecker(_ x: Int) -> Bool {
        var alreadychecked: Set<Int> = Set()
        var result = happyFunc(number: x)
         
         
        while !alreadychecked.contains(result) {
           
          if result == 1 {
            resultLabel.text = "your number is a happy number"
            return true
          }
           
          alreadychecked.insert(result)
          result = happyFunc(number: result)
        }
        resultLabel.text = "your number is a unhappy number"
        return false
      }
       
      happyChecker(x)
    }
    
       
     
     
  }
   
   
   
   



The setup is not totally clear.

There is a back button at the top of the CalculatorView.

  • Does it mean you have embedded in a NavigationView ?
  • If so, what is the segue from Calculator to Home ?
  • Where does it originate from ?

I would recommend to:

  • embed the Home View in a navigation view
  • connect to Calculator through a segue (seems this is what you did)
  • remove the existing segue from Calculator to Home
  • add a Swipe gesture in Storyboard on the whole Calculator View
  • Define the action as:
        _ = navigationController?.popViewController(animated: true)

Swipe Back
 
 
Q