【问题标题】:How to access UIView from the same xib using swift如何使用 swift 从同一个 xib 访问 UIView
【发布时间】:2014-07-09 07:42:55
【问题描述】:

让我在这里解释一下我的问题,我正在处理一个项目,其中我有一个带有 UIView 的 xib 文件。我所做的是,我在同一个 xib 中创建了另一个 UIView,我需要在我的 xib 视图中使用 swift 在按钮操作中显示它。

注意:如果我倾向于通过编程方式执行此操作,它工作正常,但我在使用我的 xib 时会出现问题。 (testview) 是我在 xib 中创建的视图。

 class tableClass: UIViewController {

    @IBOutlet var testview : UIView

    override func viewDidLoad() {
       super.viewDidLoad()
       println(testview)    **///// Return nil  /////**
       testview=UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) as UIView

       println(testview)    **///// Return nil  /////**
       self.view.bringSubviewToFront(testview)
       self.view.addSubview(testview)
     }
 }

提前致谢。请告诉我,您的答案和宝贵的想法。

【问题讨论】:

    标签: ios uiview interface-builder swift


    【解决方案1】:

    我通过在 ViewDidLoad 方法中初始化我的 UIView 解决了上述问题。这是我在 didLoad 方法上写的行

    class tableClass: UIViewController {
    
    @IBOutlet var testview : UIView = nil
    
    override func viewDidLoad() {
       super.viewDidLoad()
       testview.frame = CGRectMake(0, 0, 0, 0)
       self.view.addSubview(testview)
     }}
    

    之后,我可以在类的任何位置更改 UIView 框架,如下所示。快乐编码..

    testview.backgroundColor = UIColor.greenColor()
    testview.frame = CGRectMake(50, 50, 50, 50)
    

    【讨论】:

      猜你喜欢
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 2011-05-03
      • 2011-08-27
      • 2016-02-29
      相关资源
      最近更新 更多