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
    3.9K+ Learners
    4.43  (129)
    Jenkins Tutorial
    1 hrs
    Beginner
    7K+ Learners
    4.54  (369)
    Dockerize Spring Boot Application
    1 hrs
    Intermediate
    2.9K+ Learners
    4.37  (111)
    Python Data Structures
    1 hrs
    Beginner
    23.9K+ Learners
    4.54  (1275)
    Fibonacci Series in Java
    2 hrs
    Beginner
    2.4K+ Learners
    4.38  (48)
    Priority Queue in C++
    1 hrs
    Beginner
    1.9K+ Learners
    4.32  (84)
    Introduction to MATLAB
    2 hrs
    Beginner
    18.1K+ Learners
    4.51  (1034)
    Packages in Python
    1 hrs
    Beginner
    5.8K+ Learners
    4.41  (221)
    Palindrome in Python
    2 hrs
    Beginner
    2.4K+ Learners
    4.69  (62)