gpt4 book ai didi

ios - 执行某些说明后无法从一个 View 转到另一个 View

转载 作者:行者123 更新时间:2023-11-30 12:27:49 31 4
gpt4 key购买 nike

我的应用程序的一部分有这个实现:

为什么当我点击“cerca per ISBN”按钮时,它会转到 tableView,而不执行我编写的说明?

这是我点击的“cerca per ISBN”按钮的代码:

@IBAction func ISBNButtonClick(_ sender: Any) {

let libro = Libro.text! as String

if Libro.text!.isEmpty {

//Alert per segnalare i campi mancanti, oltre a caselle rosse

var myAlert = UIAlertController(title:"Attenzione\n", message:"Inserire il codice ISBN", preferredStyle:UIAlertControllerStyle.alert);

let okAction = UIAlertAction(title:"Ok", style:UIAlertActionStyle.default){ action in }

myAlert.addAction(okAction);
self.present(myAlert, animated:true, completion:nil);

// placeholder rosso se la text è vuota

Libro.attributedPlaceholder = NSAttributedString(string:"Digita qui...", attributes: [NSForegroundColorAttributeName: UIColor.red])

//se tutti i campi obbligatori sono stati inseriti, proseguo ad altri controlli
}else{
if(isNumeric(string: libro)){
if((libro.characters.count) < 13 || (libro.characters.count) > 13){

//Alert per segnalare l'ISBN più corto di 13 numeri

var myAlert = UIAlertController(title:"Attenzione\n", message:"L'ISBN deve essere di 13 cifre", preferredStyle:UIAlertControllerStyle.alert);

let okAction = UIAlertAction(title:"Ok", style:UIAlertActionStyle.default){ action in }

myAlert.addAction(okAction);
self.present(myAlert, animated:true, completion:nil);
}else{
//inviare dati al server
let myUrl = NSURL(string:"http://chuadiv.ddns.net/easytoschool/fetch_book_detailed.php");

let request = NSMutableURLRequest(url:myUrl as! URL);
request.httpMethod = "POST";
let postString = "name=\(libro)&mail=\(UserDefaults.standard.object(forKey: "userEmail") as? String)";
request.httpBody = postString.data(using: String.Encoding.utf8);

let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in

if error != nil{
print("error=\(error)")
return
}

var err: NSError?

do{
var json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSArray


if let parseJSON: NSArray = json{

for index in 0...parseJSON.count-1 {

print("ciao")
let libro = parseJSON[index] as! [String:Any]
print("\n\n",index,":\n")

let book = resultCell.init(bookId: libro["id"] as! String,bookName: libro["name"] as! String,bookAuthor: libro["author"] as! String,bookSchool: libro["school"] as! String,bookPrice: libro["price"] as! String,bookStatus: libro["status"] as! String,bookISBN: libro["isbn"] as! String,bookType: libro["type"] as! String,bookIdSeller: libro["idSeller"] as! String,bookNameSeller: libro["nameSeller"] as! String,bookSurnameSeller: libro["surnameSeller"] as! String)

book.printBook();

HomeViewController.booksArray.append(book)
}

}
}catch{
print("error=\(error)")
return
}
}
task.resume();

performSegue(withIdentifier: "homeToListBook", sender: self)

}
}else{
var myAlert = UIAlertController(title:"Attenzione\n", message:"Inserire solo numeri per la ricerca attraverso codice ISBN", preferredStyle:UIAlertControllerStyle.alert);

let okAction = UIAlertAction(title:"Ok", style:UIAlertActionStyle.default){ action in }

myAlert.addAction(okAction);
self.present(myAlert, animated:true, completion:nil);
}
}
}

我希望当我按下按钮时它会设置我的数组并在它进入 tableView 后,但我不能

最佳答案

问题是 dataTask 的完成 block 将调用 async,因此它将稍后调用,并且您当前正在其完成 block 之外执行 segue。因此,您需要做的是在 Main 线程上的 dataTask 完成 block 内调用 performSegue 。还可以使用 URLURLRequest 而不是 NSURLNS(Mutable)URLRequest

let myUrl = URL(string:"http://chuadiv.ddns.net/easytoschool/fetch_book_detailed.php")

let request = URLRequest(url:myUrl!)
request.httpMethod = "POST";
let postString = "name=\(libro)&mail=\(UserDefaults.standard.string(forKey: "userEmail")!)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in

if error != nil{
print("error=\(error)")
return
}

var err: NSError?

do{
var json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? [[String:Any]]


if let parseJSON = json {
for libro in in parseJSON {
let book = resultCell.init(bookId: libro["id"] as! String,bookName: libro["name"] as! String,bookAuthor: libro["author"] as! String,bookSchool: libro["school"] as! String,bookPrice: libro["price"] as! String,bookStatus: libro["status"] as! String,bookISBN: libro["isbn"] as! String,bookType: libro["type"] as! String,bookIdSeller: libro["idSeller"] as! String,bookNameSeller: libro["nameSeller"] as! String,bookSurnameSeller: libro["surnameSeller"] as! String)

book.printBook();

HomeViewController.booksArray.append(book)
}

//Perform segue now
DispatchQueue.main.async {
performSegue(withIdentifier: "homeToListBook", sender: self)
}
}
}catch{
print("error=\(error)")
return
}
}
task.resume()

注意:在 Swift 中使用 Swift native 类型数组和字典,而不是 NSArrayNSDictionary

关于ios - 执行某些说明后无法从一个 View 转到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43951774/

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