2017-08-23 37 views
0

我正在嘗試獲取具有以下信息的數組:[「Concert」,「Coachella」,「Date」]並將選定數組的文本轉換爲按鈕的文本。因此,當我選擇數組中的「日期」選項(以表格視圖呈現)時,應該將該數據發送回我的第一個VC,其中文本「日期」顯示爲按鈕上的文本。出於某種原因,我似乎無法從[String]中提取字符串。一旦我提取該字符串,我可以將該字符串設置爲我的UIButton的標題文本。如何從字符串數組中獲取文本並將其用作按鈕的文本?

下面是其中表視圖顯示的選項,並且用戶選擇的選項,並且數據被髮送到另一個VC的代碼:

var delegate : SentDataBack? 

@IBOutlet weak var occasionsTableView: UITableView! 
@IBOutlet weak var occasionSearchBar: UISearchBar! 

var addedOccasions = ["Coachella", "Concert", "Date"] 

var index = 0 

var selectedCell = [""] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    occasionsTableView.delegate = self 
    occasionsTableView.dataSource = self 

    // Do any additional setup after loading the view. 
} 

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = occasionsTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 

    //let addedOccasions = ["Coachella", "Concert", "Date"] 

    cell.textLabel?.text = addedOccasions[indexPath.row] 

    cell.textLabel?.highlightedTextColor = UIColor(red: 255/255, green: 77/255, blue: 91/255, alpha: 1) 

    return cell 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return(addedOccasions.count) 

} 

// need to create a method that determines what happens when you click on a cell. what it should do is highlight its text then either store that name into a variable or return what cell text was selected to the data sent back method 
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    index = indexPath.row 

    selectedCell = [addedOccasions[index]] 

} 

@IBAction func doneButtonPressed(_ sender: Any) { 

    delegate?.dataSentBack(occasionSentBack: selectedCell) 
    // sends back selected option 
    dismiss(animated: true, completion: nil) //can also add a function in here where it says nil, if you want something to happen upon completion of dismiss 
} 

而這裏是接收的數據和應該代碼操縱按鈕顯示所選[字符串]文:

func dataSentBack(occasionSentBack: [String]) { 

configureAddedOccasionButton(selectedOccasion: occasionSentBack) 

    // once you get back whatever data is sent back which should be one event, then this area should add that button as selected to the filter list and possibly should add occasion name to occasion array list 
} 

func configureAddedOccasionButton(selectedOccasion : [String]) { 

    //set look of occasion button 
    addedOccasion.isHidden = false 
    addedOccasion.titleLabel?.text = selectedOccasion.description 
    addedOccasion.frame = CGRect(x: 86, y: 33, width: 70, height: 43) 
    buttonSelected(buttonPressed: addedOccasion) 

} 
+0

設置WillAppear或DIdAppear在FirstVc按鈕上的文字爲駁回第二控制器後ViewWilApppear被稱爲firstVC –

回答

0

在SET按鈕標題中的問題,也如果selectedOccasion是你需要通過指數擺脫陣列文字傳遞給您的代理字符串數組

var delegate : SentDataBack? 

    @IBOutlet weak var occasionsTableView: UITableView! 
    @IBOutlet weak var occasionSearchBar: UISearchBar! 

    var addedOccasions = ["Coachella", "Concert", "Date"] 

    var index = 0 

    var selectedCell = "" 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     occasionsTableView.delegate = self 
     occasionsTableView.dataSource = self 

     // Do any additional setup after loading the view. 
    } 

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = occasionsTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 

     //let addedOccasions = ["Coachella", "Concert", "Date"] 

     cell.textLabel?.text = addedOccasions[indexPath.row] 

     cell.textLabel?.highlightedTextColor = UIColor(red: 255/255, green: 77/255, blue: 91/255, alpha: 1) 

     return cell 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return(addedOccasions.count) 

    } 

    // need to create a method that determines what happens when you click on a cell. what it should do is highlight its text then either store that name into a variable or return what cell text was selected to the data sent back method 
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     index = indexPath.row 

     selectedCell = addedOccasions[index] 

    } 

    @IBAction func doneButtonPressed(_ sender: Any) { 

     delegate?.dataSentBack(occasionSentBack: selectedCell) 
     // sends back selected option 
     dismiss(animated: true, completion: nil) //can also add a function in here where it says nil, if you want something to happen upon completion of dismiss 
    } 

//////////

func dataSentBack(occasionSentBack: String) { 

    configureAddedOccasionButton(selectedOccasion: occasionSentBack) 

    // once you get back whatever data is sent back which should be one event, then this area should add that button as selected to the filter list and possibly should add occasion name to occasion array list 
} 

func configureAddedOccasionButton(selectedOccasion : String) { 

    //set look of occasion button 
    addedOccasion.isHidden = false 
    addedOccasion.setTitle(selectedOccasion,for: .normal) 

    addedOccasion.frame = CGRect(x: 86, y: 33, width: 70, height: 43) 
    buttonSelected(buttonPressed: addedOccasion) 

} 
+0

的太感謝你了!還有一個問題:我實現了你的代碼,它在UIButton中顯示文本。但它顯示文本爲[「日期」],而不是僅顯示單詞日期。任何想法如何解決這個問題? –

+0

您不應該在數組上使用'description'來將結果呈現給用戶。 – rmaddy

+0

@ShaliniSingh檢查更新 –

相關問題