Swift 3 - 向 UIView 弹出窗口添加文本

Swift 3 - Add Text to UIView Pop up

我想显示一个弹出窗口,其中包含一些固定文本。

我为弹出窗口找到了这个。

    // Define a view
    var popup:UIView!
    func showAlert() {
    // customise your view
    popup = UIView(frame: CGRect(x: 100, y: 200, width: 200, height: 200))
    popup.backgroundColor = UIColor.redColor

    // show on screen
    self.view.addSubview(popup)

    // set the timer
    Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.dismissAlert), userInfo: nil, repeats: false)
  }

  func dismissAlert(){
    if popup != nil { // Dismiss the view from here
      popup.removeFromSuperview()
    }
  }

我似乎无法在 showAlert() 中添加标签并显示它。

我尝试在 showAlert 中调用一个单独的函数来获取标签文本,该函数有效但不会关闭。

如何在 showAlert 函数本身的弹出窗口中添加 text/string/Label?我想使用 UIView 本身,而不是 AlertController。

你可以试试

popup = UIView(frame: CGRect(x: 100, y: 200, width: 200, height: 200))

let lb = UILabel(frame: CGRect(x: 100, y: 200, width: 200, height: 200))
lb.text="anything"
popup.backgroundColor = UIColor.redColor

// show on screen
self.view.addSubview(popup)
popup.addSubview(lb)
lb.center = popup.center