2014-12-19 58 views
0

對於我的Swift應用程序,我希望didReceiveMemoryWarning函數中訪問的數據來自與viewDidLoad函數檢索的數據相同的隨機列,這是使用let randNumber = Int(arc4random_uniform(UInt32(count)))完成的。我的應用程序是一個民意調查應用程序,用戶可以投票選擇不同的民意調查選項,然後當他們點擊「下一步」按鈕時,它會隨機選擇另一輪民意調查。 didReceiveMemoryWarning函數下的代碼用於從投票中添加投票(並檢索它),但我需要該投票與viewDidLoad函數顯示的投票相同。我怎麼做?出於某種原因,無論我檢索什麼民意調查(薄餅或煎餅,可樂或百事可樂,巧克力或香草等),它只會增加投票並從「薄餅或煎餅」調查中獲取結果。就像用戶得到民意調查「可口可樂或百事可樂」並選擇可口可樂一樣,它會爲可麗餅投票計數添加投票並從投票中檢索結果。如何從檢索的民意調查中檢索數據?重新訪問ViewController中的方法之間的隨機數據

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)" 
     } 
     } 
    } 

回答

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)" 
} 
+0

是的,我的意思是該方法下面的代碼。如果所有代碼都在viewDidLoad方法的下面,第二次getFirstObjectInBackgroundWithBlock實現時,是否會引用由getFirstObjectInBackgroundWithBlock隨機調用的相同值?還是必須檢索不同的值? – hamza1234 2014-12-19 03:10:18

+0

如果您使用相同的查詢,它將是相同的,但如果這是您想要的,只需將您從第一次調用返回的對象存儲在屬性中。 – jrturton 2014-12-19 04:11:58

+0

如何將randNumber轉換爲屬性?我認爲我已經把它做成一個常量。 – hamza1234 2014-12-19 04:38:05

相關問題