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
    5.3K+ Learners
    4.46  (556)
    Jenkins Tutorial
    1 hrs
    Beginner
    8.7K+ Learners
    4.53  (710)
    Dockerize Spring Boot Application
    1 hrs
    Intermediate
    4.1K+ Learners
    4.46  (325)
    Python Data Structures
    1 hrs
    Beginner
    31.8K+ Learners
    4.5  (2546)
    Fibonacci Series in Java
    2 hrs
    Beginner
    2.8K+ Learners
    4.39  (69)
    Priority Queue in C++
    1 hrs
    Beginner
    2.2K+ Learners
    4.38  (110)
    Introduction to MATLAB
    2 hrs
    Beginner
    22.5K+ Learners
    4.41  (1798)
    Packages in Python
    1 hrs
    Beginner
    7.4K+ Learners
    4.33  (568)
    Palindrome in Python
    2 hrs
    Beginner
    2.9K+ Learners
    4.65  (92)