Go back to previous view controller with help of swipe back (to the left)

人盡茶涼 提交于 2019-12-24 01:44:57

问题


I have 2 view controllers. 1: Login page(Main View Controller) 2:Sign Up page.Lets assume that I want to go back from Sign Up page to Login page . How to solve this problem(with Navigation Controller), I am new in swift and iOS . Here is my code in AppDelegate.swift.

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window?.backgroundColor = UIColor.whiteColor()
    self.window?.rootViewController = ViewController()
    self.window?.makeKeyAndVisible()

    return true
}

回答1:


Embed-In navigation controller programmatically

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        UINavigationBar.appearance().backgroundColor = UIColor.whiteColor()

        let loginViewController: LoginViewController = LoginViewController(nibName: "LoginViewController", bundle: nil)
        let navController: UINavigationController = UINavigationController(rootViewController: loginViewController)
        window!.makeKeyAndVisible()
        window!.addSubview(navController.view!)

        return true
    }

Embed-In navigation controller Using Storyborad

To enable navigation controller you need to embed-in your loginViewController in Navigation controller

Open Storyboard --> select loginviewcontroller --> Editor (in Xcode menu) --> Embed in --> Navigation controller

you can see result looks like

then just update app delegate method to change the navigation bar background color

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        UINavigationBar.appearance().backgroundColor = UIColor.whiteColor()

        return true
    }

In LoginViewController controller add Right swipe gesture in viewDidLoad

override func viewDidLoad() {
        super.viewDidLoad()

        let swiperight: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(LoginViewController.swiperight(_:)))
        swiperight.direction = .Right
        self.view!.addGestureRecognizer(swiperight)   
    }

    func swiperight(gestureRecognizer: UISwipeGestureRecognizer) {
            //Do what you want here
            //Load Signup view controller here

func swiperight(gestureRecognizer: UISwipeGestureRecognizer) {
        //Load Signup view controller here
        let signupHomeViewController: SignupHomeViewController = SignupHomeViewController(nibName: nil, bundle: nil)
        // and push it onto the 'navigation stack'
        self.navigationController?.pushViewController(signupHomeViewController, animated: true)

    }

    }

In SignupViewController controller add Left swipe gesture in viewDidLoad

override func viewDidLoad() {
        super.viewDidLoad()

        let swipeleft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(SignupViewController.swipeleft(_:)))
        swipeleft.direction = .Left
        self.view!.addGestureRecognizer(swipeleft) 
    }

 func swipeleft(gestureRecognizer: UISwipeGestureRecognizer) {
        //Do what you want here

        //Pop back to login view controller

    }



回答2:


As per your question i think you have already created the login page, Sign Up page and is able to navigate to Sign Up page. Since you are using Navigation Controller you must be using push to viewcontroller. You can add a barbutton on left, define a action on it as below

var b = UIBarButtonItem( title: "Back To Login", style: .Plain, target: self, action: "backToLogin:" ) 

func backToLogin(sender: UIBarButtonItem) {
    self.navigationController?.popViewControllerAnimated(true)
 }

For swipe functionalty:

var leftGesture = UISwipeGestureRecognizer(target: self, action: Selector("swipeToLogin:")) 
leftGesture.direction = .Left 
self.view.addGestureRecognizer(leftGesture)

func swipeToLogin(sender:UISwipeGestureRecognizer) {

self.navigationController?.popViewControllerAnimated(true)
}


来源:https://stackoverflow.com/questions/36217525/go-back-to-previous-view-controller-with-help-of-swipe-back-to-the-left

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!