登陆注册页面的两种跳转方法

本文介绍了一种在iOS应用中实现登录和注册页面的方法。通过使用present方式展示登录注册界面,确保不会干扰到主流程。提供了两种实现方案:一种是使用单一导航控制器管理登录和注册流程;另一种则是通过页面间的通知来间接返回到主页面。

一般ios主流框架是以tabbar加navigation为主线的框架 但是在这些框架下得子页面经常需要用到登陆信息,所以需要进入登陆界面但是又不希望登陆界面影响流程(在未登陆的情况下才会弹出登陆 所以不适合压在导航下)所以一般使用present进行登陆注册


方法一 将登陆注册捆绑在一个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)

    }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值