I have a problem with segues or my textview. There are two view controllers: ViewController and TextViewController. Both view controllers are showing a UITextView and a button. I've created a segue called "textSegue" when I press the "Text" button on the ViewController the text is passed to the TextViewController and displayed. On the TextViewController I can select the text and press the "Bold" button to make the text bold. When I press the "Back" button, the TextViewController is dismissed and the ViewController will show the attributed text. The problem is, when I press the "Text" button again and the TextViewController shows, the text in the UITextView doesn't show the attributed text (it is'n bold anymore).
I think the segue did pass the attributed text properly, but somehow the textview doesn't show it properly. Is there anyone who knows what I'm doing wrong?
ViewController:
import UIKit
class ViewController: UIViewController, TextViewControllerDelegate {
var newtext = NSAttributedString()
@IBOutlet var textView: UITextView!
@IBAction func btnText(sender: AnyObject) {
self.performSegueWithIdentifier("textSegue", sender: nil)
newtext = textView.attributedText
print("viewcontroller btnText pressed")
print("newtext :", newtext)
}
@IBAction func btnLocation(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
newtext = textView.attributedText
print("ViewController ViewDidLoad")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "textSegue"{
let vc = segue.destinationViewController as! TextViewController
vc.myString = newtext
vc.delegate = self
print("open Textview:")
print(newtext)
}
}
func setText(controller: TextViewController, nieuwetext: NSAttributedString) {
newtext = nieuwetext
print("Segue ontvangen1: ", newtext)
textView.attributedText = newtext
controller.navigationController?.popViewControllerAnimated(true)
}
}
TextViewController:
import UIKit
protocol TextViewControllerDelegate {
func setText(controller: TextViewController, nieuwetext: NSAttributedString);
}
class TextViewController: UIViewController {
var delegate : TextViewControllerDelegate! = nil
var myString = NSAttributedString(string: "")
@IBOutlet var textView: UITextView!
@IBAction func btnTextBack(sender: AnyObject) {
myString = textView.attributedText
print("textview btnTextBack pressed: ")
print(myString)
if (delegate != nil) {
delegate!.setText(self, nieuwetext: myString)
}
dismissViewControllerAnimated(true, completion:nil)
}
@IBAction func btnBold(sender: AnyObject) {
textView.toggleBoldface(self)
}
override func viewDidLoad() {
super.viewDidLoad()
textView.attributedText = myString
print("textView viewDidLoad")
print("myString: ", myString)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}