问题描述
我有2个视图控制器. 1:登录页面(主视图控制器) 2:注册页面.假设我要从注册页面返回到登录页面.如何解决这个问题(使用导航控制器),我是swift和iOS的新手.这是我在AppDelegate.swift中的代码.
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
}
推荐答案
以编程方式嵌入导航控制器
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嵌入导航控制器中
To enable navigation controller you need to embed-in your loginViewController in Navigation controller
打开Storyboard->选择loginviewcontroller->编辑器(在Xcode菜单中)->嵌入->导航控制器
Open Storyboard --> select loginviewcontroller --> Editor (in Xcode menu) --> Embed in --> Navigation controller
您可以看到结果类似
然后只需更新应用程序委托方法即可更改导航栏背景颜色
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
}
在 LoginViewController 控制器中,在viewDidLoad中添加向右滑动手势
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)
}
}
在 SignupViewController 控制器中,在viewDidLoad中添加向左滑动手势
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
}
这篇关于向后滑动(回到左侧)可返回上一个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!