iOS

iOS Actions and Outlets

iOS Actions and Outlets

Using Action and Outlet connections, UIViewController may communicate directly with its UIView.

  • When a View Controller needs to change the labelling text, it connects to the outlet.
  • When a button is pressed, it uses the action link to send a message to the View Controller.
  • It signifies that the View Controller's outlet is defined as a field, and the action is stated as a method.

Let's look at how the Action and Outlet function by making a new Single View App and calling it ActionAndOutletDemo.


Add one label and one button to the project once it's been created, as seen below:


  • So, when you press the button, it should run the code that changes the label's text.
  • That is to say; this button must be an action for something to happen.
  • Also, because you wish to update it from our Swift Controller code, the label must be an outlet.

Hide the left and right panels, then pick the storyboard and go into Assistant mode.

Hold down the Control key, then click and drag the label to your ViewController class, stopping just above any existing methods. You should now see a small pop-up window with the Connection set to an outlet. Give it the name myLabel, and then click the Connect button.

Next, go to the storyboard icon, keep the control button down, click the button, and drag it down to where we've already constructed the basic.Label


Make careful to change the Connection from an outlet to action this time when the pop-up appears. ChangeText is the name you should give it. The following lines of code have been added to our ViewController class. The outlet is one thing, and the action is another.

Top course recommendations for you

    Factorial Program in C
    2 hrs
    Beginner
    4.5K+ Learners
    4.42  (154)
    Jenkins Tutorial
    1 hrs
    Beginner
    8.4K+ Learners
    4.49  (575)
    Dockerize Spring Boot Application
    1 hrs
    Intermediate
    3.8K+ Learners
    4.4  (243)
    Python Data Structures
    1 hrs
    Beginner
    30.3K+ Learners
    4.51  (2109)
    Fibonacci Series in Java
    2 hrs
    Beginner
    2.7K+ Learners
    4.33  (58)
    Priority Queue in C++
    1 hrs
    Beginner
    2.2K+ Learners
    4.3  (97)
    Introduction to MATLAB
    2 hrs
    Beginner
    21.8K+ Learners
    4.42  (1620)
    Packages in Python
    1 hrs
    Beginner
    7.1K+ Learners
    4.38  (449)
    Palindrome in Python
    2 hrs
    Beginner
    2.8K+ Learners
    4.63  (83)