方法一 将登陆注册捆绑在一个navigation下面 再将navigation弹出 退出的时候将 navigation dismiss就可以了 这样不影响主体
//简单写了个demo 这边注册页面取消是直接取消整个导航 想回到登陆页面可以 用导航popviewcontroller就可以了
//要跳转的页面
import UIKit
class ViewController: UIViewController {
overridefunc viewDidLoad() {
super.viewDidLoad()
view.backgroundColor =UIColor.purpleColor()
// Do any additional setup after loading the view, typically from a nib.
let btn =UIButton()
btn.frame =CGRectMake(100,100, 200,100)
btn.setTitle("跳转到登陆页面", forState: UIControlState.Normal)
btn.setTitleColor(UIColor.orangeColor(), forState:UIControlState.Normal)
btn.addTarget(self, action:"goto", forControlEvents:UIControlEvents.TouchUpInside)
view.addSubview(btn)
}
func goto()
{
let nav =UINavigationController.init(rootViewController:loginController())
self.presentViewController(nav, animated:true, completion: nil)
}
overridefunc didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//登陆页面
import UIKit
class loginController: UIViewController {
overridefunc viewDidLoad() {
super.viewDidLoad()
view.backgroundColor =UIColor.blueColor()
self.navigationController?.navigationBarHidden = true
// Do any additional setup after loading the view.
buildBtn(CGRectMake(100,100, 200,50), title: "注册", action:"zhuce")
buildBtn(CGRectMake(100,200, 200,50), title: "登陆", action:"denglu")
buildBtn(CGRectMake(100,300, 200,50), title: "取消", action:"quxiao")
}
func buildBtn(frame:CGRect,title:String,action:Selector )
{
let btn =UIButton.init(frame: frame)
btn.setTitleColor(UIColor.orangeColor(), forState:UIControlState.Normal)
btn.setTitle(title, forState:UIControlState.Normal)
btn.addTarget(self, action: action, forControlEvents:UIControlEvents.TouchUpInside)
view.addSubview(btn)
}
func zhuce()
{
self.navigationController?.pushViewController(RegisterController(), animated: true)
}
func denglu()
{
self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
}
func quxiao()
{
self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
}
overridefunc didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//注册页面
import UIKit
class RegisterController: UIViewController {
overridefunc viewDidLoad() {
super.viewDidLoad()
view.backgroundColor =UIColor.darkGrayColor()
self.navigationController?.navigationBarHidden = true
let btn =UIButton.init(frame:CGRectMake(100,100, 200,50))
btn.setTitle("取消", forState: UIControlState.Normal)
btn.setTitleColor(UIColor.orangeColor(), forState:UIControlState.Normal)
btn.addTarget(self, action:"quxiao", forControlEvents:UIControlEvents.TouchUpInside)
view.addSubview(btn)
}
overridefunc didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func quxiao()
{
self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
}
}
第二种方法
这个方法不是很推荐
原理是 要跳转的页面->present到login页面 然后login页面 present 跳转到 register页面
在register页面取消要回到主页面的时候
self.dismissViewControllerAnimated(true) {
NSNotificationCenter.defaultCenter().postNotificationName("shouye", object: nil)
}
发送个通知
login的
viewdidload()里面监听
NSNotificationCenter.defaultCenter().addObserver("shouye", selector: "xiaohcu", name:"shouye", object: nil)
func xiaochu()
{
self.dismissViewControllerAnimated(true, completion: nil)
}
这样把动画效果关掉也可以实现 在注册页面直接跳到主体页面 但是实际上是 两个页面的连续跳转
//移除监听
deinit
{
NSNotificationCenter.defaultCenter().removeObserver(self)
}