Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.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 UILabel文本不是';当模态重新出现时,t更新_Ios_Iphone_Swift_Ios8 - Fatal编程技术网

Ios UILabel文本不是';当模态重新出现时,t更新

Ios UILabel文本不是';当模态重新出现时,t更新,ios,iphone,swift,ios8,Ios,Iphone,Swift,Ios8,我已经创建了一个可重用的模式UIView作为单例对象,但是在更改它包含的标签文本时遇到了问题。当模态第二次出现时,新文本出现在旧文本上。我猜这是因为标签只实例化了一次?如果是这样,为什么我可以添加文本而不删除它?代码如下: class TransparentModal: UIView { static let modal = TransparentModal() class func modalInView(view: UIView, forSelectionOrPlacement: Str

我已经创建了一个可重用的模式UIView作为单例对象,但是在更改它包含的标签文本时遇到了问题。当模态第二次出现时,新文本出现在旧文本上。我猜这是因为标签只实例化了一次?如果是这样,为什么我可以添加文本而不删除它?代码如下:

class TransparentModal: UIView {

static let modal = TransparentModal()

class func modalInView(view: UIView, forSelectionOrPlacement: String) -> TransparentModal {
    let f = CGRectMake(0, view.bounds.size.height, view.bounds.size.width, view.bounds.size.height)
    modal.frame = f
    modal.alpha = 0.7
    modal.backgroundColor = UIColor.darkGrayColor()

    let button = UIButton(frame: CGRectMake(0, 0, 116, 50))
    button.center = CGPointMake(view.bounds.size.width / 2, view.bounds.size.height / 1.6)
    button.backgroundColor = UIColor.lightGrayColor()
    button.layer.cornerRadius = 4
    button.setTitle("Ok", forState: UIControlState.Normal)
    button.addTarget(self, action:"dismiss:", forControlEvents: UIControlEvents.TouchUpInside)
    modal.addSubview(button)

    let label = UILabel(frame: CGRectMake(0, 0, 260, 100))
    label.center = CGPointMake(view.bounds.size.width / 2, view.bounds.size.height / 3)
    label.numberOfLines = 0
    label.text = ""

    // This is where I'm going wrong
    if forSelectionOrPlacement == "selection" {
        label.text = "" // Attempting to remove previous text
        label.text = "Text for condition one."
    } else if forSelectionOrPlacement == "placement" {
        label.text = ""
        label.text = "Text for condition two."
    }
    modal.addSubview(label)
    view.addSubview(modal)

    self.animateWithDuration(0.5, animations: { () -> Void in
        self.modal.frame.origin.y -= view.bounds.size.height
    })

    return modal
}

class func dismiss(sender: UIButton) {
    self.animateWithDuration(0.2, animations: { () -> Void in
        self.modal.alpha = 0
    }) { (Bool) -> Void in
        self.modal.removeFromSuperview()
    }
  }
}
我知道这是一个有点复杂的方式来创建一个简单的模式。这更像是一个创建可重用对象的练习。模态还需要出现在其他模态视图上,没有导航控制器的视图等等,所以我想尝试使它更灵活


更新:下面的答案是正确的,除了变量只能作为
静态添加到类中。在
dismise
函数中删除标签,允许在模态下一次出现时用新文本重新实例化标签

正如kursus在一篇评论中所写:您不会将按钮和标签作为子视图从其父视图(实际的模态视图)中删除。如果将再次显示,将创建两个新实例,并将其放置在以前的实例之上。我猜你只看到标签,因为它们基本上是透明的,按钮完全覆盖

要解决此问题,请向类中添加两个变量:

var button:UIButton!
var label:UILabel!
然后将
modalInView
中的两行更改为

if label == nil {
    label = UILabel(frame: CGRectMake(0, 0, 260, 100))
}

这将导致按钮和标签仅在以前未创建的情况下创建

另一种方法是删除
discouse
函数的
success
块中的两个视图,如

self.animateWithDuration(0.2, animations: { () -> Void in
    self.modal.alpha = 0
}) { (Bool) -> Void in
    self.modal.removeFromSuperview()
    button.removeFromSuperview()
    label.removeFromSuperview()
}

每次显示视图时都可能添加子视图。您需要在init时添加它或每次在方法中删除它。为了使第二个函数能够访问
按钮
标签
,我必须将它们作为
静态
变量添加到类中。如果添加到类函数中,
disclose:
无权访问它们。这两种解决方案似乎都不起作用。我代码中的愚蠢错误<代码>按钮
标签
作为
静态
变量添加到类中,然后在
解除
函数中删除标签将清除标签,以便添加新文本。
self.animateWithDuration(0.2, animations: { () -> Void in
    self.modal.alpha = 0
}) { (Bool) -> Void in
    self.modal.removeFromSuperview()
    button.removeFromSuperview()
    label.removeFromSuperview()
}