- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用我的数据库验证用户的登录。当按下登录按钮时,我想说根据从数据库返回的信息是否会发生转场。我首先将可变决策设置为 true,如果我无法验证用户,我想将其设置为 false 并防止继续。这是我的代码,但有一个问题。最后的 return 语句始终为 true。基本上发生的事情是在从数据库返回响应之前首先调用 .resume() 之后最后的 return 语句。有人可以澄清为什么会发生这种情况
override func shouldPerformSegue(withIdentifier identifier: String,sender:
Any?) -> Bool
{
var decision = true
let url = URL(string:"http://192.23.25.98/login/php")
var request = URLRequest(url: url!)
request.httpMethod = "POST"
let body = "UserNameLogIn=\(userName.text!.lowercased())&PasswordLogIn=\(passWord.text!.lowercased())"
request.httpBody=body.data(using: String.Encoding.utf8)
URLSession.shared.dataTask(with: request) { (data:Data?, response:URLResponse?, error:Error?) in
if (error == nil)
{
DispatchQueue.main.async(execute: {
do
{
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? Dictionary<AnyHashable,AnyObject>
guard let parseJson = json else{
print ("error parsing")
return
}
let status = parseJson["status"]
if (status != nil)
{
if (parseJson.count>3)
{
decision = true
}
else
{
decision = false
}
}
}
catch
{
print("error: \(error)")
}
})
}
else
{
decision = false
}
}.resume()
return decision
}
最佳答案
您应该让按钮定义一个 @,而不是在点击按钮时启动 Segue 并尝试在
执行验证请求并以编程方式启动 segue(如果成功)。shouldPerformSegue(withIdentifier:sender:)
中捕获异步进程。 IBAction
因此:
从按钮中删除转场。
通过control在 View Controller 之间添加segue - 从第一个场景上方栏中的 View Controller 图标拖动到第二个场景:
选择 Segue 并在属性检查器中为其命名:
从按钮中删除 Segue 后,您现在可以将按钮连接到 @IBAction
方法:
您的登录操作应该 performSegue(withIdentifier:sender:)
当您完成验证逻辑时:
performSegue(withIdentifier: "NextSceneSegue", sender: self)
例如
@IBAction func didTapLoginButton(_ sender: Any) {
let url = URL(string:"http://192.23.25.98/login/php")
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.httpBody = ["UserNameLogIn": useridTextField.text!, "PasswordLogIn": passwordTextField.text!]
.map { $0.key + "=" + $0.value.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed)! }
.joined(separator: "&")
.data(using: .utf8)
URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async {
guard let data = data, let httpResponse = response as? HTTPURLResponse, (200 ..< 300) ~= httpResponse.statusCode, error == nil else {
// handle basic network errors here
return
}
guard let json = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else {
// handle json parsing errors here
return
}
if json["status"] == nil {
// handle missing status here
return
}
guard json.count > 3 else {
// handle incorrect count here
return
}
self.performSegue(withIdentifier: "NextSceneSegue", sender: self)
}
}.resume()
}
请注意,我对请求正文中的值进行了百分比编码。特别是,密码可能包含保留字符。我没有错误地使用 .urlQueryAllowed
,而是使用 .urlQueryValueAllowed
,其中我提取了一些保留字符:
extension CharacterSet {
/// Returns the character set for characters allowed in the individual parameters within a query URL component.
///
/// The query component of a URL is the component immediately following a question mark (?).
/// For example, in the URL `http://www.example.com/index.php?key1=value1#jumpLink`, the query
/// component is `key1=value1`. The individual parameters of that query would be the key `key1`
/// and its associated value `value1`.
///
/// According to RFC 3986, the set of unreserved characters includes
///
/// `ALPHA / DIGIT / "-" / "." / "_" / "~"`
///
/// In section 3.4 of the RFC, it further recommends adding `/` and `?` to the list of unescaped characters
/// for the sake of compatibility with some erroneous implementations, so this routine also allows those
/// to pass unescaped.
static var urlQueryValueAllowed: CharacterSet = {
let generalDelimitersToEncode = ":#[]@" // does not include "?" or "/" due to RFC 3986 - Section 3.4
let subDelimitersToEncode = "!$&'()*+,;="
var allowed = CharacterSet.urlQueryAllowed
allowed.remove(charactersIn: generalDelimitersToEncode + subDelimitersToEncode)
return allowed
}()
}
关于json - 执行 segue swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53947720/
我一整天都遇到同样的错误,我知道这是在说 Storyboard 中没有识别出 segue,但确实如此。以下是我认为相关的代码:1.为 segue 发出信号的按钮的 IBAction。 -(IBActi
我有一个从 A View Controller 到 B View Controller 的 unwind segue。 在B中做了一个网络操作,操作完成后,响应会显示在A View Controlle
我正在创建一个应用程序,家长可以在其中登录并查看有关其本地托儿中心的信息。但是,为登录运行 View Controller 的代码无法正常工作。一旦用户按下登录按钮,我希望我的代码使用标识符“Show
刚刚遇到一个奇怪的问题。我有一个启动画面,它自动使用自定义 segue (fade-to-segue) 转到主页 ViewController。自定义 segue 工作正常,但一旦实现,我在主页上的其
以编程方式创建的 segue 在 performSegueWithIdentifier: 上使应用程序崩溃,不过我真的不想使用 Storyboard。 - (void)viewDidLoad { [s
我想我一定在这里遗漏了一些简单的东西,但我不知道它是什么。 我必须支持来自同一个 View Controller 的多个 segue,所以我很自然地想使用 segue 标识符。我的代码由于某种原因无法
当用户在警告时点击“确定”按钮时,我如何才能将用户带到自定义 View Controller ? 我有一个主 Controller -> 导航 Controller -> 第二个 View Contr
在 TableView Controller 中,我试图获取在我继续之前选择的行的索引(最终将传递此信息以准备继续)。我已经实现了这个功能: var selectedQuestionCell: Ind
我正在 Xamarin Studio 中开发一个应用程序(所以它是 C#),它有一个包含多个 TextField 的表单,我想在 segue 之前验证整个屏幕> 到下一个屏幕执行。单击按钮时,它正在使
我有一个带有标识符的 segue。 当我按下函数 shouldPerformSegueWithIdentifier 中的按钮时,我得到一个空标识符 什么会导致这样的问题?我设置错了什么? 这是我的代码
我有一个奇怪的问题。我在 Storyboard中创建了 2 个 View Controller :testSegueViewcontroller 和 nextPageViewController。 t
我下面的代码试图将一个值从 twoviewcontroller 转移到 View Controller 。 segue 正在工作,我可以从 View Controller 转到 View Contro
我正在尝试使用自定义segue,当单击按钮时,它会使 View 向右或向左“滚动”。我添加了一个如下所示的自定义类 class horizontalSegue : UIStoryboardSegue
我有一个带有 Root View Controller 的 UINavigationController。我的 UINavigationController 也设置为我的应用程序的 Initial V
您好,我正在尝试使用 prepare for segue 方法执行一个 segue。我在关注这个问题 this但我无法成功注册。当我单击注册按钮时,它所做的只是注册用户并停在那里。我从 View Co
当 Storyboard上有一个按钮连接退出时,我了解如何使用展开转场。但是在我的应用程序中,我使用推送转场从 TableViewController 转到 ViewController。所以我的导航
我要实现的目标的背景: MainViewController 呈现 TableVC。我将点击一个单元格以显示 DetailVC。能够通过 TableVC 或 DetailVC 上导航栏上的 doneB
在我的 Storyboard中,模态转场将用户带到登录屏幕(从主屏幕)。然后,我从注册屏幕添加了一个 PUSH segue 到下一个屏幕(创建配置文件),该屏幕嵌入在导航 Controller 中(创
我有大约 6 个 segue 在 prepareForSegue 方法中执行相同的操作,但是,我想为我的 unwind segue 跳过此操作,但我无法弄清楚如何确定传递的 segue 是 unwin
因此,我开始在XCode 4.3.2中使用 Storyboard 。我从Master-Detail应用程序(iPad的拆分 View 应用程序)开始。在详细信息 View (名为DetailVC)中,
我是一名优秀的程序员,十分优秀!