简体   繁体   中英

How to access UIView from the same xib using swift

Let me explain my problem here, I am working on a project in which I am having a xib file with a UIView. What I have done is, I created another UIView inside the same xib and I need to show that in my xib view in a button action using swift.

Note : If I tend to do this via programatically it is working fine but the problem while I am doing with my xib. (testview) is a view that I have created inside the 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)
     }
 }

Thanks in Advance. Please let me know, your answers and valuable ideas.

I solved the above stated problem by initializing my UIView in ViewDidLoad method. Here, is the lines I wrote on my didLoad method

class tableClass: UIViewController {

@IBOutlet var testview : UIView = nil

override func viewDidLoad() {
   super.viewDidLoad()
   testview.frame = CGRectMake(0, 0, 0, 0)
   self.view.addSubview(testview)
 }}

After doing that, I can able to change the UIView frame anywhere from the class, like below. Happy Coding..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM