2016-01-11 14 views
1

我正在開發一個應用程序,在該應用程序中以編程方式創建按鈕。當我點擊一個按鈕時,它會向數據庫請求數據並在另一個視圖中顯示它。在swift中單擊按鈕時在新視圖中加載數據

我使用button.tag來確定要請求的數據,並且一旦點擊按鈕,我就只能獲取標籤。

但是,當我點擊按鈕時,它第一次沒有顯示任何東西。我必須再次點擊才能看到我想要的數據。

override func viewDidLoad() { 
    super.viewDidLoad() 

    //parseJSON(tagId) 
    createButton() 

    // Do any additional setup after loading the view, typically from a nib. 
} 
func createButton(){ 

    var j:CGFloat=60 
    for var i:Int = 0 ; i < myImages.count;i = i+1 { 
     let myButton = UIButton() 
     myButton.setImage(UIImage(named: "carasusto.jpg"), forState: UIControlState.Normal) 
     myButton.setTitleColor(UIColor.blueColor(), forState: .Normal) 
     myButton.frame = CGRectMake(j, j+60, 50, 50) 

     myButton.tag = i //assign a tag to every button 
     myButton.addTarget(self, action: "segueToCreate:", forControlEvents: UIControlEvents.TouchUpInside) 
     self.view.addSubview(myButton) 

     j=j+60 
     print(myImages[i]) 

    } 
} 

@IBAction func segueToCreate(sender: UIButton){ 
    tagId = String(sender.tag)//tagId needs to fetch the information 
    parseJSON(tagId) 
    performSegueWithIdentifier("segueToView", sender:self) 
} 
func parseJSON(tagID:String){ 

    Alamofire.request(.GET, "http://smarttags-001-site1.btempurl.com/SmartTagsRequests.aspx", parameters: ["AjaxFunc":"GetTagAttr","TagID":"\(tagID)"]).validate().responseJSON{ response in 
     switch response.result{ 
     case .Success: 
      if let value = response.result.value { 

       let json = JSON(value) 

       print("JSON: \(json)") 
       self.TagName = json[0]["TagName"].stringValue 
       NSLog("\(self.TagName)") 
       self.ContentTitle = json[0]["ContentTitle"].stringValue 
       NSLog("\(self.ContentTitle)") 
      } 
     case .Failure(let error): 
      print(error) 
     }enter code here 
    } 
} 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

    var ViewTest : ViewTwo = segue.destinationViewController as! ViewTwo 
    var TagNameLabel = UILabel() 
    TagNameLabel.frame = CGRectMake(74, 112, 182, 64) 
    ViewTest.view.addSubview(TagNameLabel) 
    TagNameLabel.text = TagName 
    var ContentTitleLabel = UILabel() 
    ContentTitleLabel.frame = CGRectMake(74, 180, 182, 64) 
    ViewTest.view.addSubview(ContentTitleLabel) 
    ContentTitleLabel.text = ContentTitle 
} 

} 

回答

1

要跟進MirekE's answer,在這裏你可能要考慮一些其他步驟:

  • 考慮代替硬編碼的幀使用自動佈局,讓您的UI會適應不同大小的類。

  • 考慮用於顯示交互式列表(圖像)而不是以編程方式添加按鈕的替代方法。例如,您可以使用原型(表格視圖或集合視圖)單元格。單元格可選,可代替按鈕。其他好處包括:

    • 可滾動的容器,如果你有更多的按鈕比適合在屏幕上。
    • 故事板原型單元處理單個segue(或選擇),而不是使每個程序化「按鈕」執行segue(或動作)。既然你知道哪個單元格被選中,你不再需要標籤來弄清楚。
  • 考慮將參數傳遞給目標視圖控制器,而不是試圖在prepareForSegue中實例化並創建控件。目的地的視圖在這一點上沒有加載。

  • 考慮允許UI感覺更具響應性,例如執行segue並顯示佔位符詳細信息,然後您可以在網絡請求完成後更新這些詳細信息。否則,用戶可能不得不在等待網絡響應之前等待網絡響應(這可能使他們認爲什麼都沒有發生,並不必要地再次點擊,導致額外的網絡請求)。

在一般情況下,蘋果公司(或其他人)已經提供了一些方法做你想要什麼,最好導致你必須寫更少的代碼,調試,測試和維護,以完成你想要的你應用程序來做。

1

問題的最可能的原因是你對parseJson通話之後prepareForSegueparseJson是異步的,您在調用prepareForSegue之前不會從您的服務中獲取數據。作爲第一步,將prepareForSegue移至parseJson的完成塊。

相關問題