gpt4 book ai didi

ios - 警报错误并动态显示隐藏的 UILabel

转载 作者:行者123 更新时间:2023-11-29 02:15:31 25 4
gpt4 key购买 nike

我有两个不同的问题,

  1. 我有一个隐藏在 viewDidLoad() 中的标签,稍后我试图将隐藏值设置为 false 但它不起作用。
  2. 尝试显示警报但收到此错误“由于未捕获的异常‘NSInvalidArgumentException’而终止应用程序,原因:‘应用程序试图以模态方式呈现事件 Controller ’

下面是我的代码

class HomeViewController: UIViewController ,UITextFieldDelegate {

@IBOutlet weak var userName: UITextField!
@IBOutlet weak var passWord: UITextField!
@IBOutlet weak var errorMessage: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
self.errorMessage.hidden = true
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func login(sender: AnyObject) {

let alertController = UIAlertController(title: "Error", message:
"Wrong username or password", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))

let url = NSURL(string: "myurl")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
var error: NSError?
let jsonData: NSData = data /* get your json data */
let json = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) as NSDictionary

if let login: AnyObject = json["login"] {
if (login as NSObject == 0){

self.errorMessage.hidden = false
self.presentViewController(alertController, animated: true, completion: nil)

}

}
}

task.resume()
}

最佳答案

好的,最后我使用 dispatch_async(dispatch_get_main_queue()) { } 解决了这个问题,所以 if 语句将是这样的

if let login: AnyObject = json["login"]  {
if (login as NSObject == 0){
dispatch_async(dispatch_get_main_queue()) {
self.errorMessage.hidden = false
self.presentViewController(alertController, animated: true, completion: nil)
}
}

}

关于ios - 警报错误并动态显示隐藏的 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28793844/

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