Search code examples
iosswiftuisegmentedcontrol

Swift 3 - Unable to execute a function


Following is my code. I have also posted a screenshot.

Button_CustomSegmentValueChanged is a custom Segmented control which i have made. Its an individual button and is working fine but I want to run "Button_CustomSegmentValueChanged" function from inside "SwipedRight" function too, but i am unable to pass the correct switch value in order to do so. Need help with it.

Anyone?

import UIKit

class CredentialsViewController: UIViewController {

    @IBOutlet weak var loginView: UIView!
    @IBOutlet weak var signupView: UIView!

    var currentSelectedView = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        let swipeR = UISwipeGestureRecognizer(target: self, action: #selector(CredentialsViewController.SwipedRight(swipe:)))
        swipeR.direction = .right
        view.addGestureRecognizer(swipeR)

        let swipeL = UISwipeGestureRecognizer(target: self, action: #selector(CredentialsViewController.SwipedLeft(swipe:)))
        swipeL.direction = .left
        view.addGestureRecognizer(swipeL)

        //Hide view
        signupView.transform = CGAffineTransform(translationX: signupView.frame.size.width, y: 0)
    }

    @IBAction func Button_CustomSegmentValueChanged(_ sender: CustomSegmentedControl) {

        switch sender.selectedSegmentIndex {
        case 0:
            LoadLoginView()
            break

        case 1:
            LoadSignupView()
            break

        default:
            break
        }

    }

    func SwipedRight(swipe : UISwipeGestureRecognizer){
        if currentSelectedView == 1 {
            Button_CustomSegmentValueChanged(0)
            //LoadLoginView()
        }
    }

    func SwipedLeft(swipe : UISwipeGestureRecognizer){
        if currentSelectedView == 0 {
            //LoadSignupView()
        }

    }

    func LoadLoginView(){

        UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
            self.signupView.transform = CGAffineTransform(translationX : self.loginView.frame.size.width, y : 0)
            self.loginView.transform = .identity
            self.currentSelectedView = 0
        })
    }

    func LoadSignupView(){

        UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
            self.signupView.transform = .identity
            self.loginView.transform = CGAffineTransform(translationX: self.loginView.frame.size.width, y: 0)
            self.currentSelectedView = 1
        })
    }


}

Screenshot of my code and error


Solution

  • I would suggest to use the buttons array on CredentialSegmentedController to mimic the behaviour of change segment

    1. Make an outlet from your custom segment control
    2. Then modify your code using this segment control like below

      func SwipedRight(swipe : UISwipeGestureRecognizer) {
      
                  if currentSelectedView == 1 {
                          //Button_CustomSegmentValueChanged(0)
                          //LoadLoginView()
                          let btn = customSegmentOutlet.buttons[0]
                          customSegmentOutlet.buttonTapped(btn)
                  }
          }
      
          func SwipedLeft(swipe : UISwipeGestureRecognizer){
                  if currentSelectedView == 0 {
                          //LoadSignupView()
                          let btn = customSegmentOutlet.buttons[1]
                          customSegmentOutlet.buttonTapped(btn)
                  }
      
          }