gpt4 book ai didi

ios - 使用 Button 快速传递数据

转载 作者:行者123 更新时间:2023-11-28 06:48:25 25 4
gpt4 key购买 nike

我尝试将数据从我的 ViewController 传递到 TableViewPlace.swift 使用按钮。当我选择按钮返回时不是什么东西

viViewController

class ViewController: UIViewController {

struct Country{
var id :Int
init(id:Int){

self.id = id
}
}
var CountrySelected = [Country]()
var countryArry = NSArray()



override func viewDidLoad() {
super.viewDidLoad()
// 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 airAction(sender: AnyObject) {
}


@IBAction func viewPlaceAction(sender: AnyObject) {


getParsePlaceView()
// json viewPlace


performSegueWithIdentifier("viewPlaceSegu", sender: sender)
}

@IBAction func tourAction(sender: AnyObject) {
}
/// Open the page
// parse json
func getParsePlaceView(){

let url = NSURL(string: "http://jsonplaceholder.typicode.com/posts")
NSURLSession.sharedSession().dataTaskWithURL(url!){ [unowned self] (data , repsonse , error) in
if error != nil {
print(error!)
} else {

do {
let posts = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! [[String:AnyObject]]

for post in posts {

if let id = post["userId"] as? Int{

// print(id)

let sets = Country(id: id)
self.CountrySelected.append(sets)
}


}
self.countryArry = posts
print(self.countryArry)// return data and currect

} catch let error as NSError {
print(error)
}
}
}.resume()

print(countryArry) // return nil why??
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

// get a reference to the second view controller
if segue.identifier == "viewPlaceSegu" {
if let secondViewController = segue.destinationViewController as? TableViewPlace {

// set a variable in the second view controller with the String to pass
print(countryArry)
secondViewController.tnt = countryArry
}

}
}


}

当我 print(countryArry) 返回 nil 为什么??

有人可以帮助我或给我更好的解决方案吗?

最佳答案

您正在使用此行创建一个 Post 对象:

let countryPlace = Post(userid: post["userId"] as! Int, title: post["title"] as! String)

并且您试图将此 Post 对象传递给您的 tableview 到您的 tnt 变量,这是一个 ViewController 类,这就是为什么您出现第一个错误:

secondViewController.tnt = Country 

然后,如果你想将你的字符串国家添加到你必须做的可变数组中:

if let title = tnt.title {
Country.append(title)
}

关于ios - 使用 Button 快速传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35596848/

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