[英]Go back to previous view controller with help of swipe back (to the left)
我有2个视图控制器。 1: 登录页面(主视图控制器) 2: 注册页面 。假设我要从注册页面返回登录页面。 如何解决这个问题(使用导航控制器),我是swift和iOS的新手。 这是我在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
}
以编程方式嵌入导航控制器
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
}
使用Storyborad的嵌入式导航控制器
要启用导航控制器,您需要将您的loginViewController嵌入导航控制器中
打开情节提要->选择loginviewcontroller->编辑器(在Xcode菜单中)->嵌入->导航控制器
你可以看到结果看起来像
然后只需更新应用程序委托方法即可更改导航栏背景色
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().backgroundColor = UIColor.whiteColor()
return true
}
在LoginViewController控制器中,在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)
}
}
在SignupViewController控制器中,在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
}
根据您的问题,我认为您已经创建了登录页面,注册页面,并且能够导航到注册页面。 由于您使用的是Navigation Controller,因此必须使用Push to ViewController。 您可以在左侧添加一个条形按钮,如下所示定义一个动作
var b = UIBarButtonItem( title: "Back To Login", style: .Plain, target: self, action: "backToLogin:" )
func backToLogin(sender: UIBarButtonItem) {
self.navigationController?.popViewControllerAnimated(true)
}
对于滑动功能:
var leftGesture = UISwipeGestureRecognizer(target: self, action: Selector("swipeToLogin:"))
leftGesture.direction = .Left
self.view.addGestureRecognizer(leftGesture)
func swipeToLogin(sender:UISwipeGestureRecognizer) {
self.navigationController?.popViewControllerAnimated(true)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.