gpt4 book ai didi

swift - 如何从登录 View 加载选项卡栏 Controller - Swift

转载 作者:行者123 更新时间:2023-11-30 12:46:09 26 4
gpt4 key购买 nike

所以我用 Parse 为登录用户创建了一个 View Controller 。当用户通过输入登录按钮登录时,选项卡栏 View Controller 加载。问题是,如果用户打开应用程序并且他已经登录,我不希望他在没有输入登录信息的情况下离开。我希望签名 View Controller 将他发送到选项卡栏 View Controller 。

最初的 View Controller 是Tab bar View Controller ,我尝试了很多方法来处理这个问题,但似乎没有什么好的。

enter image description here

等待您的所有想法。

最佳答案

可能有很多不同的方法可以实现这一点,但我过去处理这种情况的一种方法是创建一个自定义容器 ViewController。该 VC 没有自己的 UIView,而是将其他 ViewController 作为其“ View ”呈现。

这是一些 Apple documentation 的链接描述了创建这种类型的 Controller 。

此架构的好处之一是,我始终有一个范围内的 VC,它可以根据与其帐户状态相关的事件(未登录、达到离线限制、帐户暂停、首先-)采取措施重新路由我的用户。时间用户、注销等)。

编辑:示例容器 Controller

以下是如何实现自定义容器 Controller 的示例。我确信有一些更好的方法可以实现此处所示的一些功能,但希望这能给您一个良好的开端。

import UIKit

class ApplicationContainerController: UIViewController {
//MARK: - View Controller Routing Properties
private var _currentClientView:UIView? = nil
private var _currentViewController: UIViewController? = nil


//MARK: - Initialization
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

//MARK: - UIViewController Members
override func viewDidLoad() {
super.viewDidLoad()

}

override var shouldAutomaticallyForwardAppearanceMethods : Bool {
return true
}

override func viewWillAppear(_ animated: Bool) {

//Get the user and route to the appropriate VC
let yourUserObject: AnyObject? = YourDataSource.TryToGetAUser()
DispatchQueue.main.async {
self.routeUser(yourUserObject)
}

}

//MARK: - Your Custom Routing Logic
func routeUser(_ yourUserObject: AnyObject?) {

//make sure we have an existing user, or else we send them to login
guard let user = yourUserObject
else {
self.displayContentController(YourLoginViewController())
return
}

var destinationViewController:UIViewController

//please use an enum or something (instead of strings) in your code
switch user.signInStatus {
case "loginActive":
let mainMenuViewController = YourMainMenuViewController()
mainMenuViewController.user = user
destinationViewController = mainMenuViewController
case "firstLogin":
let firstLoginViewController = YourFirstLoginViewController()
firstLoginViewController.user = user
destinationViewController = firstLoginViewController
case "giveUsMoney":
let weWantMoneyViewController = YourOtherViewController()
weWantMoneyViewController.user = user
destinationViewController = weWantMoneyViewController
default:
//loginFailed or some other status we don't know how to handle
destinationViewController = YourLoginViewController()
}


if let activeViewController = self._currentViewController,
type(of: activeViewController) !== type(of: destinationViewController) {
//we have an active viewController that is not the destination, cycle
self.cycleFromCurrentViewControllerToViewController(destinationViewController)
} else {
//no active viewControllers
self.displayContentController(destinationViewController)
}


}



//MARK: - Custom Content Controller Routing Methods
private func frameForContentController() -> CGRect {
return self.view.frame
}

private func newViewStartFrame() -> CGRect {
return CGRect(x: self.view.frame.origin.x,
y: self.view.frame.origin.y + self.view.frame.size.width,
width: self.view.frame.size.width,
height: self.view.frame.size.height)
}

private func oldViewEndFrame() -> CGRect {
return CGRect(x: self.view.frame.origin.x,
y: self.view.frame.origin.y - self.view.frame.size.width,
width: self.view.frame.size.width,
height: self.view.frame.size.height)
}


/**
Transitions viewControllers, adds-to/removes-from context, and animates views on/off screen.
*/
private func cycleFromCurrentViewControllerToViewController(_ newViewController: UIViewController) {
if let currentViewController = self._currentViewController {
self.cycleFromViewController(currentViewController, toViewController: newViewController)
}
}

private func cycleFromViewController(_ oldViewController:UIViewController, toViewController newViewController:UIViewController) {

let endFrame = self.oldViewEndFrame()

oldViewController.willMove(toParentViewController: nil)
self.addChildViewController(newViewController)
newViewController.view.frame = self.newViewStartFrame()

self.transition(from: oldViewController, to: newViewController,
duration: 0.5,
options: [],
animations: { () -> Void in
newViewController.view.frame = oldViewController.view.frame
oldViewController.view.frame = endFrame
}) { (finished:Bool) -> Void in
self.hideContentController(oldViewController)
self.displayContentController(newViewController)
}
}


/**
Adds a view controller to the hierarchy and displays its view
*/
private func displayContentController(_ contentController: UIViewController) {
self.addChildViewController(contentController)
contentController.view.frame = self.frameForContentController()
self._currentClientView = contentController.view
self.view.addSubview(self._currentClientView!)
self._currentViewController = contentController
contentController.didMove(toParentViewController: self)

}

/**
Removes a previously added view controller from the hierarchy
*/
private func hideContentController(_ contentController: UIViewController) {
contentController.willMove(toParentViewController: nil)
if (self._currentViewController == contentController) {
self._currentViewController = nil
}
contentController.view.removeFromSuperview()
contentController.removeFromParentViewController()

}

}

关于swift - 如何从登录 View 加载选项卡栏 Controller - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41621676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com