Adding IBOutlet or action


I have seen numerous tutorials showing how to add this and connect them but none or maybe I am not understanding correctly why you put them where you do in the code. Some go above view did load some go in and some after. What am I missing here? Where or really how do you determine where to put them in your code? Some reference or good blog posts would be awesome.Thanks!


You need to read about Public, Class, Private and Instance methods and variables.
Any IBOutlet or UIButton or any element/variable will be intialized differently and could be accessed differently,
depending on where in code it’s placed.
A UIButton that is within ViewDidLoad { } will initialize once the View loads. You will not be able to access it from outside the class.
If a UIButton is initialized right after the class name, but not in any method ( i.e. ViewDidLoad() ) , then it will initialize as soon as an instance of this class has been called. For example if you have a UIViewController and you drag you IBOutlet right after the class name, it will load as soon as the ViewController has been initialized. This will also give you the opportunity to call your Outlet from outside the class - for example if you have:

class myVC: UIViewController {

var myButton: UIButton = UIButton()

you could make a call to this button from another ViewController pointing to an instance of myVC, which would look something like:

let someNewVC = myVC()
someNewVC.myButton.hidden = true // for example…

There is more to it, you can read more about it if you feel like it :slight_smile:


Thanks it is something that is never really gone in depth worth it most videos. I will look into it.


@jel111 Here’s a video and short post I made to help explain how to create and setup your Actions and Outlets.


Thank you Paul that was very cool of you to personally address this for me! I think it did clear things up somewhat. I am putting to much thought into how it actually works and like you said all I really need to know is that it just works!