【问题标题】:Swift: Change ViewController after Authentication from RESTful APISwift:从 RESTful API 进行身份验证后更改 ViewController
【发布时间】:2016-07-09 01:22:52
【问题描述】:

我设置了一个 rails api 和一个测试用户。我可以登录/注销查看api。

我设置了一个标签应用程序,并设置了一个 LoginViewController 来对其进行身份验证。我的视图控制器(基本上)看起来像下面的代码。这不起作用。但是,第二个代码块确实有效

我猜我说错了什么。第一个块执行,然后在 -[UIKeyboardTaskQueuewaitUntilAllTask​​sAreFinished] 中出现 *** Assertion failure 崩溃。 . 等等。任何事情都会有帮助!!!

//DOES NOT WORK //

class LoginViewController: UIViewController {

@IBOutlet weak var email: UITextField!

@IBOutlet weak var password: UITextField!

@IBAction func Login(sender: AnyObject) {
    func switchToTabs() {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let tabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.window?.rootViewController = tabBarController

    }

    let authCall = PostData()
    var authToken = ""
    authCall.email = email.text!
    authCall.password = password.text!
    authCall.forData { jsonString in
        authToken = jsonString
        if(authToken != "") {
            switchToTabs()
        }
    }
}



// SAME CLASS THAT DOES WORK//

class LoginViewController: UIViewController {

@IBOutlet weak var email: UITextField!

@IBOutlet weak var password: UITextField!

@IBAction func Login(sender: AnyObject) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let tabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.window?.rootViewController = tabBarController


}

我的问题是,为什么当我不使用我的 api 时,我似乎可以导航到一个新视图,但是当我收到我的身份验证令牌时......我无法登录。

请原谅丑陋的代码..这是我使用 swift 的第一天

【问题讨论】:

    标签: ios swift rest authentication swift2


    【解决方案1】:

    每次你在闭包内做任何改变 UI 的事情或任何可能脱离主队列的事情时,将其包含在此语句中。

    dispatch_async(dispatch_get_main_queue()) {
        //the code that handles UI
    }
    

    这包括所有在闭包中的代码,就像你拥有的那样

    authCall.forData { jsonString in
        authToken = jsonString
        if(authToken != "") {
            switchToTabs()
        }
    }
    

    如果你这样写,它会起作用

     authCall.forData { 
        jsonString in
        authToken = jsonString
        if(authToken != "") {
            dispatch_async(dispatch_get_main_queue()) {
                switchToTabs()
            }
        }
    }
    

    然后在switchToTabs()打电话给你的segue

    如果您不确定自己在哪个队列中,这样做不会有什么坏处。随着您越来越熟悉使您脱离主队列的场景,您将意识到何时使用它。每当您处于关闭状态并处理 UI 时,您都可以放心离开。显然任何异步都会导致它。只需将您的 performSegueWithIdentifier 放入其中即可。

    【讨论】:

      【解决方案2】:

      如果您在情节提要上从附加到 LoginViewController 的视图控制器中创建一个转场,那么您单击转场可以给它一个标识符。然后你只需调用 performSegueWithIdentifier("identifierName", sender: nil) 并且 switchToTabs() 中的其他代码都不是必需的。

      如果您想继续沿召唤视图控制器的路线前进,那么我认为您缺少的是这些行。

      在你的班级下面使用它。

      private extension UIStoryboard {
          class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) }
      
          class func tabBarController() -> UITabBarController? {
              return mainStoryboard().instantiateViewControllerWithIdentifier("TabBarController") as? UITabBarController
          }
      }
      

      然后为此更改您的 switchToTabs() 函数

      func switchToTabs() {
          let tabBarController = UIStoryboard.tabBarController()
          view.addSubview(tabBarController!.view)
      
          //line below can be altered for different frames
          //tabBarController!.frame = self.frame
      
          addChildViewController(tabBarController!)
          tabBarController!.didMoveToParentViewController(self)
      
          //line below is another thing worth looking into but doesn't really correlate with this
          //self.navigationController!.pushViewController(tabBarController!, animated: true)
      }
      

      如果您愿意,我添加了几个 cmets 供您查看。我相信这会完成这项工作,但请记住,您前进的方向不会从内存中删除前一个屏幕。从技术上讲,它仍然位于您的新屏幕后面。对于您正在尝试做的事情,我永远不会推荐它,但如果您正在构建简单的东西,它可能不会受到伤害。

      【讨论】:

      • 所以我从 AppDelegate 调用登录控制器的方式大致相同.. 有没有办法一开始就进入 loginVC?或者我应该继续调用 LoginVC 并改变这个 segue。非常感谢你的帮助顺便说一句..
      • 似乎 preformSegueWithIdentifier 仍然以同样的方式失败...... >.
      • self.navigationController!.pushViewController(tabBarController!, animated: true) 行是我相信没有 segues 的编程方式,但我从未使用过这种方法。确保您的完成闭包实际上正在被调用。 apple segue documentation segue 必须来自与调用它的类关联的 ViewController,并且标识符 String 必须相同。一个大写字母不同,它将不起作用。
      • 我在使用 preformSegueWithIdentifier 时仍然遇到同样的错误。它在 -[UIKeyboardTaskQueue waitUntilAllTask​​sAreFinished] 中因同样的 *** 断言失败而崩溃
      猜你喜欢
      • 2011-11-28
      • 1970-01-01
      • 2021-06-19
      • 2013-08-21
      • 2012-06-05
      • 2013-03-25
      • 2011-11-12
      • 1970-01-01
      相关资源
      最近更新 更多