how to create one iboutlet for multiple buttons in swift
you can use IBoutlet Collection
!
You can connect several same types of objects after declaring as IBoutlet Collection.
And if you want to run the same code, you can use the for statement.
for item in myButton {
item.layer.cornerRadius = 3
}
Can I connect multiple objects with different tags to the same IBOutlet?
Use IBOutletCollection to add an outlet collection to your view controller, like this:
@property (retain, nonatomic) IBOutletCollection(UIButton) NSMutableSet* buttons;
This will let you connect all your buttons to one outlet. The property buttons
will be a NSMutableSet containing all your buttons. You can continue to identify individual buttons using the button's tag
property. This is handy if you want to iterate through all your buttons, perhaps to set up each button's image:
for (UIButton *b in self.buttons) {
b.imageView.image = [self imageForTag:b.tag];
}
(You'll need to supply the -imageForTag:
method to provide the right image for a given tag, or find some other way to map from tags to images.)
Of course, if you already know the range of tag values for all your buttons, and if you've taken care to make the tags unique inside the view containing all the buttons, you can also just fetch each button individually using -viewWithTag:
. This is probably not as fast as having the whole set of buttons already created, as you have with the outlet collection described above, but it does mean that there's one less thing to maintain.
Swift ios connect multiple items to the same IBOutlet
Yes, this is ok as long as you don't plan on doing any operations on those labels.
The correct way to do it, is by creating an array IBOutlet:
@IBOutlet var collectionOfLabels:[UILabel]?
Connect all your labels to this labels array outlet.
Then access the labels via the array.
Swift. Changing multiple UIButtons with IBOutlet
Set your button tags in series like 101 to 125 and on click of any button perform this operation
for(UIButton *button in <parent view of buttons>.subviews){
if(button.tag >= 101 && button.tag <= 125){
button.setTitleColor(UIColor.redColor(), forState: .Normal()
}
}
For Swift
for subview in self.createBugView.subviews {
if let button = subview as? UIButton {
// this is a button
if(button.tag >= 101 && button.tag <= 125){
button.setTitleColor(UIColor.red, for: .normal)
}
}
}
Why do IBOutlets only respond to one of the multiple view controllers?
Make sure you specified the correct "Custom Class" for each of your view controllers.
That is, set ViewController as the custom class for each of your view controllers.
Like this:
Related Topics
Finding It Difficult to Pass Data to Separate Viewcontroller
Objective-C Call Swift Function
How to Handle Oauth-Style Log Ins with Imessage Apps in iOS 10, Xcode 8
When Should I Use Anyobject Insted of Uibutton in Swift
Auto-Sizing Uicollectionview Headers
Validate Jwt Token with Rs256 or Rs512 with Swift iOS
In iOS Avplayer, Addperiodictimeobserverforinterval Seems to Be Missing
Xcode 6 Project Crashing After Segue on iOS 7.1
Make a Uiwebview as Height as Its Content with Auto Layout
Periodically Call an API with Rxswift
Buttonwithtype' Is Unavailable: Use Object Construction 'Uibutton(Type:)
Exc_Bad_Access in Parent Class Init() with Xcode 10.2
Load Large 3D Object .Scn File in Arscnview Aspect Fit in to the Screen Arkit Swift iOS
How to Get User Data from Facebook Sdk on iOS
Settitletextattributes Doesn't Work for Uitabbaritem When It Is Unselected in Swift