Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITextView不显示文本_Ios_Swift_Swift2_Uitextview - Fatal编程技术网

Ios UITextView不显示文本

Ios UITextView不显示文本,ios,swift,swift2,uitextview,Ios,Swift,Swift2,Uitextview,我已经创建了一个UITextView类,我正在touch上添加UITextView。出现UITextView,但是设置的文本不会出现,当我触摸UITextView时,键盘会出现,但当我开始写入文本时,它不会出现 这是我的UITextView课程: import UIKit class TextAnnotation: UITextView { override init(frame: CGRect, textContainer: NSTextContainer?) {

我已经创建了一个
UITextView
类,我正在touch上添加
UITextView
。出现
UITextView
,但是设置的文本不会出现,当我触摸
UITextView
时,键盘会出现,但当我开始写入文本时,它不会出现

这是我的
UITextView
课程:

import UIKit

class TextAnnotation: UITextView {

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        self.text = "Text Field"
        self.textColor = UIColor.blackColor()
        self.userInteractionEnabled = true
        self.editable = true
        self.frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: 200, height: 50)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
下面是我如何调用这个类来添加touch上的UITextView:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    let textCenter = touches.first!.locationInView(view)
    let textView = TextAnnotation(frame: CGRect(x: textCenter.x, y: textCenter.y, width: 200, height: 50), textContainer: NSTextContainer())

    self.view.addSubview(textView)
}
覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?){
让textCenter=touchs.first!.locationInView(视图)
让textView=TextAnnotation(帧:CGRect(x:textCenter.x,y:textCenter.y,宽度:200,高度:50),textContainer:NSTextContainer())
self.view.addSubview(textView)
}

当我开始键入时,如何使设置的文本显示,以及为什么不显示?确定,
触摸开始
函数-初始化子视图并将其添加到superview是一个不好的地方,因为它可以被多次调用。
我猜您有一个以上的
textView
实例,并且每个新实例都覆盖了以前的实例


我的建议是将
textView
初始化和布局代码替换为
init
viewDidLoad
等功能,隐藏
textView
,并显示触控事件

如果将textView.text的值设置在初始值设定项之外,该怎么办?例如,在将其添加到视图之前?您确定您的初始值设定项调用了吗?将有多个UITextView,因此如果我在viewDidLoad中创建它,我将不知道要加载多少。哦,明白了,您需要>1
textView