一般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)
}
NSNotificationCenter.defaultCenter().addObserver("shouye", selector: "xiaohcu", name:"shouye", object: nil)
func xiaochu()
{
self.dismissViewControllerAnimated(true, completion: nil)
}
deinit
{
NSNotificationCenter.defaultCenter().removeObserver(self)
}
本文介绍了一种在iOS应用中实现登录和注册页面的方法。通过使用present方式展示登录注册界面,确保不会干扰到主流程。提供了两种实现方案:一种是使用单一导航控制器管理登录和注册流程;另一种则是通过页面间的通知来间接返回到主页面。
7171

被折叠的 条评论
为什么被折叠?



