Connect UITextView in Nib to ViewController

I’m trying to connect a UITextView in a Nib to the ViewController - it’s displayed as part of a UITableViewCell.

I can’t work out how I can pass the text inserted by the user to the ViewController when a button is pressed.

Below is the code for the nib, but what do I put in the View Controller? (All I need to do is get the text as a variable). I’ve tried to use let answerText = Answer.textView.text but get an error saying 'Instance Member ‘textView’ cannot be used on type ‘Answer’.

Thanks :slight_smile:

Claire

import UIKit

class Answer: UITableViewCell, UITextViewDelegate {

@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var textView: UITextView!



override func awakeFromNib() {
    super.awakeFromNib()
    let selectedView = UIView(frame: CGRect.zero)
    selectedView.backgroundColor = UIColor(red: 228/255, green: 0, blue: 58/255, alpha: 0.5)
    selectedBackgroundView = selectedView
    questionLabel.numberOfLines = 0
    self.textView.becomeFirstResponder()
    
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    
    // Configure the view for the selected state

}

}`

You have subclassed UITableViewCell, right?

You should be looking into the code of cellForRowAtIndexPath (CFRAIP) which is where you would be instantiating a uitableviewcell of type Answer, which is your subclassed uitableviewcell. The uitableviewcell then uses an instance of your nib uitextview.

Thank you - that’s so simple and logical… and obvious now you’ve said it :slight_smile:

It works (it takes the placeholder text at the moment, but I’m guessing I now need to use the UITextViewDelegate and didFinishEditing to get the text after).

Thanks again for your help.

Claire