gpt4 book ai didi

ios - 为什么我不能通过 preparetosegue 方法传递我在 ViewDidAppear/ViewDidLoad 中定义的任何计算和变量?我怎样才能做到这一点?

转载 作者:行者123 更新时间:2023-11-28 07:05:18 24 4
gpt4 key购买 nike

没错,正如标题。我开始将我的 HTTP 请求 session 放在 viewdidload 中,直到我意识到这是一个糟糕的地方,因为它在调用的方法层次结构中太低了,而且基本上没有希望在那里定义变量以传递给另一个 VC。

然而,我切换到 Viewdidappear,但当我通过 prepareforsegue 传递变量时,我仍然得到 nil。我可以使用另一种方法吗,或者您建议我如何实现我想要的?每当我按下按钮时,我都可以发布请求,这肯定会传递变量吗?

override func viewDidLoad() {
super.viewDidLoad()
activityIndicatorView.startAnimating()
}

override func viewDidAppear(animated: Bool) {

super.viewDidAppear(animated)

let request = NSMutableURLRequest(URL: NSURL(string: "http://127.0.0.1:5000")!)
request.HTTPMethod = "POST"
let postString = "color=\(self.finalDataPassed)&location=\(self.thirdDataPassed)&weather=\(self.dataPassed)&city=\(self.secondDataPassed)"

request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in

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

println("response = \(response)")

let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)

println(responseString)
var error: NSError?

let result = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary

var dict: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary


if(error != nil) {
println(error!.localizedDescription)
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Error could not parse JSON: '\(jsonStr)'")
}
else {
if let results = result?.valueForKey("result") as? NSArray {
dispatch_async(dispatch_get_main_queue(), {
println(results)
})
}

let parseJSON: AnyObject? = result!.valueForKey("result")
var stuff: AnyObject! = parseJSON!.valueForKey("price")
var otherstuff: NSString! = parseJSON!.valueForKey("YlowestURL") as! NSString
}

self.activityIndicatorView.stopAnimating()
self.performSegueWithIdentifier("calculateToDisplaySegue", sender: nil)
}
task.resume()

}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

var stuff: String!
var otherstuff: String!


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "calculateToDisplaySegue"){

var svc = segue.destinationViewController as! PriceViewController;
svc.dataPassed = self.stuff
svc.secondDataPassed = self.otherstuff

}
}

最佳答案

stuffotherstuff 是在方法本地定义的,而不是在类实例变量中。

IOW 使用:

self.stuff = parseJSON!.valueForKey("price")
self.otherstuff = parseJSON!.valueForKey("YlowestURL") as! NSString

当然 stuffotherstuff 必须声明为适当类型的 ivars 来代替:

var stuff: String!
var otherstuff: String!

关于ios - 为什么我不能通过 preparetosegue 方法传递我在 ViewDidAppear/ViewDidLoad 中定义的任何计算和变量?我怎样才能做到这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30808916/

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