2015-07-11 32 views
0

我試圖通過視圖控制器發送數據。在swift xcode中通過視圖發送數據

如果我點擊一個單元格,我有一個segue將我轉移到另一個視圖控制器。我有一個標籤,我想根據單元格中文本的內容進行更改。

這是我的時刻代碼:

import UIKit 

類FirstViewController:UIViewController的{

VAR storedString =字符串()

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
{ 
    tableView.deselectRowAtIndexPath(indexPath, animated: true) 

    var venue : VenueItems 

    if (tableView == self.searchDisplayController?.searchResultsTableView) 
    { 
     venue = self.filteredVenues[indexPath.row] 
     var name = venue.name 
     storedStadiumSelected = name 
    } 
    else 
    { 
     venue = self.venuesArray[indexPath.row] 
     var name = venue.name 
     storedStadiumSelected = name 
    } 


    println(storedStadiumSelected) 

} 

其間的代碼在這裏...

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

    var nextViewController: SelectBookingTypeViewController = segue.destinationViewController as SelectBookingTypeViewController 

    nextViewController.recievedStadiumSelected = storedStadiumSelected 

} 

我的第二個視圖控制器看起來是這樣的: 進口的UIKit

類SelectBookingTypeViewController:UIViewController的{

@IBOutlet weak var stadiumLabel: UILabel! 

var recievedStadiumSelected = String() 


override func viewWillAppear(animated: Bool) { 
    stadiumLabel.text = recievedStadiumSelected 
} 

這段代碼的問題是,我只裝我之前單擊選項。因此,例如,如果我點擊「item1」,標籤文本就沒有任何內容。 但是,如果我回去並點擊'item2',標籤現在是item1。我不知道爲什麼會發生這種情況!

我是比較新的編碼所以原諒我, 謝謝,任何幫助將熱情地採取:)

回答

0

尋找最好的地方是在Master-Detail Application,蘋果提供的Xcode模板。以下是您要查找的DetailViewController上的代碼。該

DetailViewController

import UIKit 

class DetailViewController: UIViewController { 

@IBOutlet weak var detailDescriptionLabel: UILabel! 


var detailItem: AnyObject? { 
    didSet { 
     // Update the view. 
     self.configureView() 
    } 
} 

func configureView() { 
    // Update the user interface for the detail item. 
    if let detail: AnyObject = self.detailItem { 
     if let label = self.detailDescriptionLabel { 
      label.text = detail.description 

     } 
    } 
} 

MasterViewController

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "showDetail" { 
     if let indexPath = self.tableView.indexPathForSelectedRow() { 
      let object = objects[indexPath.row] as! NSDate 
      let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController 
      controller.detailItem = object 
      controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem() 
      controller.navigationItem.leftItemsSupplementBackButton = true 
     } 
    } 
} 

的TableView代碼

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell 

    let object = objects[indexPath.row] as! NSDate 

    //This Code here tells the label to update for the cell selected. 
    cell.textLabel!.text = object.description 

    return cell 
} 

所以,你需要這你secondViewController

func configureView() { 
    // Update the user interface for the detail item. 
    if let detail: AnyObject = self.detailItem { 
     if let label = self.detailDescriptionLabel { 

      //Change the label name to match yours. 
      yourLabelName.text = detail.description 
     } 
    } 
} 
+0

我可以理解這段代碼,請你可以更改和編輯它適合我的代碼嗎? @Jason –

+0

我想我應該讓你澄清一下,你正試圖改變基於選定的單元格的label.text。你想要標籤文本是什麼?跟細胞一樣? – Jason

+0

是的,請@Jason –