How to connect 3 UIButton to one outlet - ios

I try to do something like this #IBoutlet var buttons : [UIButton]
but I can't drag button to this outlet
Any idea, how can I achieve that?

It's possible to do that.
Here is how:
Create an array of IBOutlets
Add multiple UIElements (Views) in your Storyboard ViewController interface
Select ViewController (In storyboard) and open connection inspector
There is option 'Outlet Collections' in connection inspector (You will see an array of outlets there)
Connect if with your interface elements
-
class ViewController2: UIViewController {
#IBOutlet var collection:[UIView]!
override func viewDidLoad() {
super.viewDidLoad()
}
}

To add a button to an existing array outlet, drag the other way 'round: from the circle in the margin of the view controller to the button. (No need to hold the control key while doing this drag.)

Related

How to I create an IBOutlet for a progress bar that is inside a collectionviewcell?

I have a progress bar that is in a collectionviewcell. I want to add a border radius to the progress bar, but whenever I add the progress bar to the view controller, it says "Illegal configuration" and "The progressBar outlet from the ViewController to the UIProgressView is invalid. Outlets cannot be connected to repeating content."
#IBOutlet var progressBar: UIProgressView!
That line of code is what causes the error message.
You should put it inside the UICollectionViewCell and set the outlet with YourCollectionViewCell. Don't forgot to set the CustomClass name for your UICollectionViewCell in storyboard to make it into the effect.
class YourCollectinoViewCell: UICollectionViewCell {
#IBOutlet var progressBar: UIProgressView!
}
Hope it helps!
Answer from Bhavin Kansagara is correct, but also note that you can set corner radius in xib file like shown here Is it possible to set UIView border properties from interface builder?

Swift: inputAccessoryView show blank white screen for UIToolbar when called

I'm trying to add UIToolbar to my keyboard with inputAccessoryView, but once the user tap the UITextField I get an Blank white screen or load White view if i put it in viewDidLoad(). I have tried few stackoverflow QA and non of them seems to be working for me. I'm also doing this with removeFromSuperview() method.
Initialization:
#IBOutlet var MessegeView: UIToolbar!
#IBOutlet var MessegeTextField: UITextField!
Assign UIToolbar to UITextField: This code result in blank white view when it get loaded.
override func viewDidLoad() {
super.viewDidLoad()
MessegeTextField.inputAccessoryView = MessegeView
MessegeView.removeFromSuperview()
}
WITHOUT removeFromSuperView(): I get the following error and that make sense kind of.
ERROR:
should have parent view controller:<APPNAME.ChatViewController:XXXXXXXX> but requested parent is:<UIInputWindowController: XXXXXXXXXX>
A few Stackoverflow QA I follow (but no result):
Error when adding input view to textfield iOS 8
Leaving inputAccessoryView visible after keyboard is dismissed iOS8?
How views are setup:
If you setup your UI in a Storyboard then you don't own the objects and shouldn't alter the hierarchy. That's why Xcode auto-generated properties from storyboards are #IBOutlet weak var by default, you don't own them and shouldn't manage the memory.
To fix your problem you should either use var MessegeView = UIToolbar() or you should initialize the toolbar from a separate .xib.

Have Outlet of many Button iOS

I have a Calculator app. if a user press equal I want to change the background color of all my button(digit 1 to 9) should i have an outlet for each button(each button represent a digit) or there is a way to have one outlet that covers all buttons ?
You want an IBOutletCollection.
In Objective-C, it would look like this:
#property (nonatomic, strong) IBOutletCollection(UIButton) NSArray* buttons;
In Swift:
#IBOutlet var buttons: [UIButton] = []
Then connect each of your buttons to the buttons outlet in Interface Builder, and you can iterate through each easily.
You can loop all the elements in the UIView and change it programmatically using the code below
for view in self.view.subviews as [UIView] {
if let btn = view as? UIButton {
//do your changes here
btn.backgroundColor = UIColor.redColor()
}
}
There is a way
control drag each button to the same outlet from the storyBoard. This should hook up each button to one outlet if I am remembering correctly, give it a try and then update your background colors to see if this works.
Of course yes, you can have an outlet for each button if you wish

Place AdMob ad at the bottom of tableview controller - swift

I've been trying to implement Admob on swift and everything is good, but i run out of problem because I'm using custom table view cell and "Table view controller" as the main swift file, so when i am dragging the BannerView into the storyboard which is the table view controller, the ad actually appear in the first cell of the table view ! instead i need to load it at the button of the tableview controller not inside the cells ?
#IBOutlet weak var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
self.bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
self.bannerView.rootViewController = self
var request: GADRequest = GADRequest()
request.testDevices = [GAD_SIMULATOR_ID]
self.bannerView.loadRequest(request)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
One solution could be to create a new instance of bannerView instead of having it as an outlet and then add it as a subview to the tableView's superview.
I had the same issue , i got it figured out by a slightly different method,
But it works like a charm.
So for all those Who are new to Swift and face this problem,
If Your using a story board, copy all your views( ex labels, UIImage's ) from the prototype cell
Go ahead and delete your View Controller. ( fear not , it will work eventually , trust me )
Drag a blank ViewController and in the class , mention your class associated with earlier tableView.
drag also tableview on it and selecting the tableview, put constraints form storyboard up & down-> 0 & left & right -> -20 .
drag a tableview cell, and selecting that cell, in class mention the cell class you were using for the earlier Tableview cell,
(tip-> You jus mite want to click on the module bellow the class and press enter, i got a stupid bug { unknown class in interface builder error , when i did it } )
now you can just select the cell and paste your earlier views in it and create a new connection with the cell class #IB outlets.
In the same Viewcontroller you can drag an empty UIView and make it a GADBannerView ( follow the Googles guide for admob iOS) Thats all in the IB
Now go to your tableview class , the class extension just make it to UIViewController , and confirm to protocols UITableViewDataSource & UITableViewDelegate.
ex: class Foo: UIViewController, UITableViewDataSource, UITableViewDelegate {
make a IB connection of your newly added UITableview in your main tableview (now viewController) class
#IBOutlet weak var Footableview: UITableView!
Now jus add these two lines in the viewDidLoad
Footableview.datasource = self
Footableview.delegate = self
Now just remove override form all tableview functions such as sections or RowForIndex functions, and wherever there is 'self.tableView' replace with 'self.Footableview'.
ScreenShot
Regards

How to make IBOutlets out of an array of objects?

I want to make an array with a bunch of UIImageViews I have in Interface Builder. Instead of having 20 or 30
IBOutlet UIImageView *img1;
and linking them all that way, and then putting them into an array, is there a way to declare an array of IBOutlet UIImageViews?
Just so I don't have so many declarations in my header file.
It is possible, it’s called outlet collection. This is the way to define an outlet collection:
#property(retain) IBOutletCollection(UIImageView) NSArray *images;
Now you can stick more than one object into the outlet in the Interface Builder, the array will be created for you when the interface is loaded.
I'm a little late here but it may be easier to set the tag property of each ImageView in IB, then access them like [some_superview viewWithTag:tag] rather than keep a separate handle to each one.
Here is more easier way to do it.
Follow these steps to create an array of outlets an connect it with IB Elements:
Create an array of IBOutlets
Add multiple UIElements (Views) in your Storyboard ViewController interface
Select ViewController (In storyboard) and open connection inspector
There is option 'Outlet Collections' in connection inspector (You will see an array of outlets there)
Connect if with your interface elements
-
class ViewController2: UIViewController {
#IBOutlet var collection:[UIView]!
override func viewDidLoad() {
super.viewDidLoad()
}
}
Swift 3 and above:
#IBOutlet var stuckLabels: [UIImageView]
There's not, unfortunately, but you can keep all of the declarations on a single line:
IBOutlet UIImageView *img1, *img2, *img3, *img4;
The other option (probably best, since you have so many of these) would be to create them programatically and store them in an array, then add them to the view from your view controller class, using, for each,
[self.view addSubview:img];
Also, keep in mind that if the elements are static (like background elements), and you don't actually need to access them, you don't need to declare outlets for each; you can just add them to the nib file and forget about them.
Same goes for UIButton instances. If you don't need to change anything about the button, you can access it from the method that it calls, like so:
-(IBAction) buttonPressed:(id)sender {
UIButton *button = (UIButton *)sender;
// method guts
// stuff with button -- access tag, disable, etc
}

Resources