32

Multiple Outlets iOS Tutorial

 5 years ago
source link: https://www.tuicool.com/articles/hit/myQ7Nrz
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Creating Outlets for multiple Objects can be time-consuming and tedious. It is also possible for objects to share the same Outlets. In this tutorial multiple outlets will be created using the tag value of the buttons. This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

MvM7jiy.png!web

For product name, use IOSMultipleOutletsTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

yaEZZbM.png!web

Go to the StoryBoard and drag a button from the Object Library to the top-left of the main view. Go to the Attribute Inspector and in the View section change the Tag value to 10.

Mjqi2yq.png!web

Copy this button and place it next to the first button in the top-right corner, this button will have the same tag value. Next, drag another button from the Object Library to the main view, place the button below the top-left button. Select the button and go to the Attribute Inspector. In the View section change the Tag value to 20. Again, copy the button and place it on the right side next to this button. The Storyboard should look like this.

2eieqiY.png!web

Select the main view and select the "Resolve Auto Layout Issues" button on the bottom-right corner of the Interface Builder. Select the "Reset to Suggested Constraint option in the All Views section.

QVnUFbQ.png!web

Go to the ViewController.swift file and change the viewDidLoad mehod to

override func viewDidLoad() {
    super.viewDidLoad()

    for subview in view.subviews where subview.tag == 10 {
        let button = subview as! UIButton
        button.addTarget(self, action: #selector(changeColorRed), for: .touchUpInside)
    }

    for subview in view.subviews where subview.tag == 20 {
        let button = subview as! UIButton
        button.addTarget(self, action: #selector(changeColorGreen), for: .touchUpInside)
    }
}

The subview property can be used to traverse to the subviews of the main view. A target is added to each button. which was set with the tag value.Next, Implement the target methods.

@objc func changeColorRed(sender: Any) {
    let button = sender as! UIButton
    button.tintColor = UIColor.red
}

@objc func changeColorGreen(sender: Any) {
    let button = sender as! UIButton
    button.tintColor = UIColor.green
}

The color of the button will change when the button is selected. Build and Run the project and click on the buttons to change the colors.

jUnIFzB.png!web

You can download the source code of the IOSMultipleOutletsTutorial at the ioscreator repository on Github .


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK