ios - 在 swift iOS 中管理用户登录 segues

标签 ios iphone swift view

我正在尝试为 iPhone 应用程序构建初始用户登录系统。我创建了一个包含所有用户数据的 API。我已经成功地建立了从我的 swift 代码到我的 API 的 HTTP 连接,我在其中向用户发送登录详细信息,API 对用户进行身份验证并将用户数据作为 JSON 发送。

我的 Storyboard中有 2 个 View ,如下所示:

enter image description here

我想控制 segue 转换,所以它只会在用户成功通过身份验证时转到下一个 View ,否则它会停留在同一 View 上,但会在与 相同的 View 上输出一条消息“错误登录详细信息”。

有什么办法可以实现吗?

如果我在问题中犯了任何错误,我深表歉意。如果您需要更多信息来回答问题,请告诉我。

更新:遇到错误!

在进行下面建议的更新后,我遇到了以下错误。知道如何修复它们。

我的代码:

enter image description here

错误:

enter image description here

最佳答案

我建议您使用以下代码在用户成功登录时启动 View Controller :

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("nextViewController") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)

通过这种方式,您可以轻松地控制导航,如果用户没有输入正确的信息,那么您可以向其他部分显示警报,如下面的代码所示:

dispatch_async(dispatch_get_main_queue()) {

        if user successfully logIn {

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("nextViewController") as! UIViewController
            self.presentViewController(vc, animated: true, completion: nil)


        } else {

            var alert = UIAlertController(title: "Alert", message: "Please enter correct Information", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)

        }
    }

希望对您有所帮助。

关于ios - 在 swift iOS 中管理用户登录 segues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30885938/

相关文章:

Swift 初学者需要详细说明语法 - 传递和返回函数

objective-c - XCTAssertEquals 在 Swift 中有两个字典

ios - 将 FirebaseUI/Storage pod 添加到 iOS 应用程序会抛出框架未找到架构 x86_64 的 SDWebImage

iphone - 获取启动选项而不覆盖 didFinishLaunchingWithOptions :

ios - 仅允许 UITextField 输入数字

ios - 从不同的 Xcode 项目加载 Storyboard

ios - 检查现有数据的核心数据。 fatal error : cannot index empty buffer

ios - "iPhone only"应用程序的 iPad iOS 7 图标

ios - 如何在 uitextview 中丢失空间