NSAttributedString - textview在segue之后没有显示属性文本

时间:2016-01-15 15:49:28

标签: swift segue xcode7

我遇到了segues或textview的问题。 有两个视图控制器:ViewController和TextViewController。两个视图控制器都显示UITextView和一个按钮。 当我按下ViewController上的“文本”按钮时,我创建了一个名为“textSegue”的segue,文本被传递给TextViewController并显示。在TextViewController上,我可以选择文本并按“粗体”按钮使文本变为粗体。当我按下“返回”按钮时,TextViewController被解除,ViewController将显示属性文本。 问题是,当我再次按下“文本”按钮并且TextViewController显示时,UITextView中的文本不会显示属性文本(它不再是粗体)。

我认为segue确实正确地传递了属性文本,但不知何故textview没有正确显示它。有谁知道我做错了什么?

的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()

    }
}

1 个答案:

答案 0 :(得分:1)

我添加了

textView.attributedText = myString

到ViewDidAppear(现在它在ViewDidAppear和ViewDidLoad中)。在ViewDidLoad中,它似乎不会加载属性,但在ViewDidAppear中它会加载。

也许是一个错误?