gpt4 book ai didi

ios - Swift:如何在登录 View 后显示标签栏 Controller

转载 作者:IT王子 更新时间:2023-10-29 05:11:55 26 4
gpt4 key购买 nike

我在这里看到过很多类似的帖子,但它们都是关于 Objective-C 的,而我正在用 Swift 开发我的应用程序。从图中可以看出,我有一个登录屏幕 View ,并且我正确地实现了登录机制。

enter image description here

现在我希望在登录成功后显示标签栏 Controller 。在我的登录 View Controller 中,我有这个登录功能:

var finalURL:NSString = "\(Settings.webServerLoginURL)?username=\(username)&password=\(password)"

LoginService.requestLoginWithURL(NSURL(string: finalURL as String)!, completionHandler: { (success) -> Void in
if (success) {
NSLog("Login OK")

/* Scarica dal database i tasks di LoggedUser.id */
/* Redirect al tab HOME dell'applicazione dove si mostrano il numero di task
di quell'utente ed in cima "BENVENUTO: name surname" */


}

else {
self.alertView.title = "Autenticazione fallita!"
self.alertView.message = "Username o passowrd."
self.alertView.delegate = self
self.alertView.addButtonWithTitle("OK")
self.alertView.show()
}

所以我认为我应该在

之后显示标签栏 Controller
NSLog("Login OK")

但是我不知道怎么办。我是 Swift/XCode 的初学者……如果你能解释一下。感谢所有阅读过的人。

最佳答案

要从登录页面显示标签栏 Controller ,请将登录页面和 TabbarController 连接到 Show segue 并在属性检查器中为其指定标识符(例如“mySegueIdentifier”)。

要添加 segue,只需右键单击并从 Login View Controller 拖动到 TabbarController。

在成功登录后,您可以简单地调用“performSegueWithIdentifier”方法,如下所示

self.performSegue(withIdentifier: "mySegueIdentifier", sender: nil)

在您的情况下,您可以在此行之后调用它。

NSLog("Login OK")

如果不想从登录页面导航到TabbarController,也可以在登录成功后设置为rootViewController。为此,请将标识符设置为 TabbarController(说“myTabbarController”)

let appDelegate = UIApplication.shared.delegate! as! AppDelegate
let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "myTabbarControllerID")
appDelegate.window?.rootViewController = initialViewController
appDelegate.window?.makeKeyAndVisible()

编辑:

swift 3

 let appDelegate = UIApplication.shared.delegate! as! AppDelegate

let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "myTabbarControllerID")
appDelegate.window?.rootViewController = initialViewController
appDelegate.window?.makeKeyAndVisible()

快乐的编码.. :)

关于ios - Swift:如何在登录 View 后显示标签栏 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32224416/

26 4 0