在ViewController中重新访问方法之间的随机数据

时间:2014-12-19 02:15:56

标签: ios xcode swift parse-platform

对于我的Swift应用程序,我希望didReceiveMemoryWarning函数中访问的数据来自viewDidLoad函数检索的相同随机数据列,这是通过let randNumber = Int(arc4random_uniform(UInt32(count)))完成的。我的应用程序是一个民意调查应用程序,用户可以投票选择不同的民意调查选项,然后当他们点击“下一步”按钮时,它会随机进行另一次民意调查。 didReceiveMemoryWarning函数下的代码用于从轮询中添加投票(并检索它),但我需要该轮询与viewDidLoad函数显示的轮询相同。我怎么做?出于某种原因,无论我检索什么民意调查(薄饼或煎饼,可口可乐或百事可乐,巧克力或香草等),它只会增加选票并从“薄饼或煎饼”调查中检索结果。就像用户获得投票“可口可乐或百事可乐”并且他们选择可口可乐一样,它将添加投票到折痕投票计数并从该投票中检索结果。如何从检索到的轮询中检索数据?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

            var query = PFQuery(className: "VoteCount")
            query.countObjectsInBackgroundWithBlock {
                (count: Int32, error: NSError!) -> Void in
                if error == nil {
                    let randNumber = Int(arc4random_uniform(UInt32(count)))
                    query.whereKey("pollNumber", equalTo: randNumber)
                    query.getFirstObjectInBackgroundWithBlock {
                        (voteCount1: PFObject!, error: NSError!) -> Void in
                        if error != nil {
                            NSLog("%@", error)
                        } else {
                            let votes = voteCount1["votes"] as Int
                            let votes2 = voteCount1["votes2"] as Int
                            let option1 = voteCount1["optionName"] as String
                            let option2 = voteCount1["optionName2"] as String
                            self.showOption1.text = "\(option1)"
                            self.showOption2.text = "\(option2)"
                        }
                    }
                } else {
                    println("error \(error)")
                }
            }

        }

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

@IBOutlet weak var pollResults: UILabel!

@IBAction func addVote1(sender: AnyObject) {
    for button in self.buttons {
        button.enabled = false
    }
    var query = PFQuery(className: "VoteCount")
    query.getFirstObjectInBackgroundWithBlock {
        (voteCount1: PFObject!, error: NSError!) -> Void in
        if error != nil {
            NSLog("%@", error)
        } else {
            voteCount1.incrementKey("votes")
            voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
            let votes = voteCount1["votes"] as Int
            let votes2 = voteCount1["votes2"] as Int
            self.pollResults.text = "\(votes)                                                       \(votes2)"
        }
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以将randomNumber设为属性而不是局部变量。但是,我认为您实际上要做的是确保您在后面的方法中使用与PFObject中相同的viewDidLoad。要做到这一点,您不需要从Parse重新获取。只需保留对PFObject的引用:

var voteCount : PFObject?

在viewDidLoad的完成块中:

(voteCount1: PFObject!, error: NSError!) -> Void in
    if error != nil {
        NSLog("%@", error)
    } else {
        self.voteCount = voteCount1
        // The rest of your code...
        let votes = voteCount1["votes"] as Int 

然后,稍后,您只需使用voteCount属性:

,而不是再次提取
@IBAction func addVote1(sender: AnyObject) {
    for button in self.buttons {
        button.enabled = false
    }

    voteCount.incrementKey("votes")
    voteCount.saveInBackgroundWithTarget(nil, selector: nil)
    let votes = voteCount["votes"] as Int
    let votes2 = voteCount["votes2"] as Int
    self.pollResults.text = "\(votes)                    \(votes2)"
}