我有2个视图控制器。 1:登录页面(主视图控制器) 2:注册页面。假设我想从注册页面返回到< em>登录页面。如何解决这个问题(使用导航控制器),我是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
}
答案 0 :(得分:3)
以编程方式嵌入导航控制器
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
打开故事板 - &gt;选择loginviewcontroller - &gt;编辑器(在Xcode菜单中) - &gt;嵌入 - &gt;导航控制器
你可以看到结果看起来像
然后只需更新app delegate方法即可更改导航栏背景颜色
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
}
答案 1 :(得分:2)
根据您的问题,我认为您已经创建了登录页面,注册页面,并且可以导航到注册页面。由于您使用的是导航控制器,因此您必须使用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)
}