I have made a collection of UIButtons like this
#IBOutlet var btnChannle1:UIButton!
#IBOutlet var btnChannle2:UIButton!
#IBOutlet var btnChannle3:UIButton!
#IBOutlet var btnChannle4:UIButton!
#IBOutlet var btnChannle5:UIButton!
#IBOutlet var btnChannle6:UIButton!
#IBOutlet var btnChannle7:UIButton!
#IBOutlet var btnChannle8:UIButton!
#IBOutlet var channlesCollection:Array<UIView>!
override func viewDidLoad() {
super.viewDidLoad()
channlesCollection.append(btnChannle1)
channlesCollection.append(btnChannle2)
channlesCollection.append(btnChannle3)
channlesCollection.append(btnChannle4)
channlesCollection.append(btnChannle5)
channlesCollection.append(btnChannle6)
channlesCollection.append(btnChannle7)
channlesCollection.append(btnChannle8)
}
now I want to traverse the collection above and want some thing like this
for item in channlesCollection{
// Set Button Text Line to 2
// Set Button Text Like this in two line
/*
01
CNN
02
BBC
03
PTV ...
*/
}
but when Im trying like this.
for item in channlesCollection{
item.setTitle("Test", forState: UIControlState.Normal)
}
error raised UIView does not have member named setTitle.
You need to type-cast item as UIButton. Here is the code:
for item in channlesCollection{
var button:UIButton = item as UIButton
button.setTitle("Test", forState: UIControlState.Normal)
}
You are having array of UIView and they indeed don't having a setTitle method. Either make your collection of type UIButton. Or cast your current collection items to buttons:
for item in channlesCollection {
if var button = item as? UIButton {
button.setTitle("Test", forState: UIControlState.Normal)
}
}
UIView does not have a member function setTitle, but UIButton has, which is probably what you want.
You have declared your channlesCollection as array of UIView, why not as array of UIButton?
var channlesCollection: [UIButton]!
Related
I have a list of buttons inside an array. I want to replace the buttons position order using that array. I can do this in java, but cant do the same in swift. Here is my code:
#IBOutlet weak var menuView: UIView!
struct ButtonsIndex {
var button: UIButton
var index: Int
}
var footer: [ButtonsIndex] = []
//Set Buttons order etc
...
func setFooter(){
let count = 0
for buttons in menuView.subviews {
if var button = buttons as? UIButton {
button = footer[count].button
count += 1
}
}
}
Is it impossible to change the buttons position without connecting the outlets to my code? Thanks in advance.
When you add a UISegmentedControl to a view, UIAccessibility will focus on it and say:
"(Selected) ItemName Button 1 of 2"
"ItemName Button 2 of 2"
I have a custom control that has UIButtons that toggle similar to a UISegmentedControl. But what I'm trying to figure out is how to get the Voice Over to announce the n of n at the end.
The closest thing that I've found is assigning the .accessibilityTraits = .tabBar on the container. The issue is that it announces:
"ItemName Button Tab 2 of 2"
But to conform to our accessibility guidelines we can't have it announce "tab".
https://developer.apple.com/documentation/uikit/uiaccessibility/uiaccessibilitytraits/1648592-tabbar
Short of just writing a custom accessibilityLabel is there anything within UIAccessibility that can handle this logic?
Set the container view's accessibilityTraits = .tabBar
Set the container view's accessibilityElements = [button1, button2]
Set each button's accessibilityTraits = .selected or .none when necessary
I have a custom control that has UIButtons that toggle similar to a UISegmentedControl. But what I'm trying to figure out is how to get the Voice Over to announce the n of n at the end.
Put each one of your UIButton elements in the accessibilityElements array of the custom control that acts like a container.
By searching a specific element in this array, you will have an index 'x' inside a total amount 'N' of buttons: "item name button x of N".
In your UIButton, set accessibilityLabel by inserting the result of the previous research.
Here's a kind of logic that should help you reach your purpose with a little bit of code as follows for instance (Xcode 10.2.1, Swift 5.0, iOS 12):
class ButtonsViewController: UIViewController {
#IBOutlet weak var myCustomContainer: UIView!
#IBOutlet weak var btn1: UIButton!
#IBOutlet weak var btn2: UIButton!
#IBOutlet weak var btn3: UIButton!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
myCustomContainer.accessibilityElements = [btn1!, btn2!, btn3!]
let nbButtons = myCustomContainer.accessibilityElements?.count
for (index, elt) in (myCustomContainer.accessibilityElements?.enumerated())! {
let btn = elt as! UIButton
let btnName = btn.titleLabel?.text
btn.accessibilityLabel = btnName! + String(index + 1) + " of " + String(describing: nbButtons!)
}
}
}
I'm currently having problems for my label to read the addition of 3 textfield values automatically, without a button function action. As such i only want my textfield to be an Int input only. There's a screenshot attached below for better reference. Appreciate those who can help me with this. Thanks!
ViewController
import UIKit
class TryingoutController: UIViewController {
#IBOutlet var impact: UITextField!
#IBOutlet var rigour: UITextField!
#IBOutlet var response: UITextField!
#IBOutlet var total: UILabel!
One way is to add self as the target to the text fields, for the control event .editingChanged
impact.addTarget(self, action: #selector(textChanged), for: .editingChanged)
// do the same for other textfields
Then declare a textChanged method. This should handle what happens when the texts in the text fields change. One implementation would be to add up all the values in the text fields (if any, and is valid) and display it in the label.
func textChanged() {
let impactValue = Int(impact.text!)
let rigourValue = Int(rigour.text!)
let responseValue = Int(response.text!)
total.text = String(describing:
(impactValue ?? 0) + (rigourValue ?? 0) + (responseValue ?? 0)
)
}
Optionally, you can conform to UITextFieldDelegate:
class TryingoutController: UIViewController, UITextFieldDelegate {
}
and implement shouldChange according to this answer by Thuggish Nuggets. Then, set the delegates of the text fields to self:
impact.delegate = self
// do the same for other text fields.
I have a custom UITableViewCell and one of its subviews is a button (with the title "RSVP"):
It is connected to the following code:
class SelectedEventsTableViewCell: UITableViewCell {
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var dateLabel: UILabel!
#IBOutlet weak var descriptionLabel: UILabel!
#IBOutlet weak var attendanceLabel: UILabel!
#IBOutlet weak var attendanceButton: UIButton!
}
I am trying to change the background color of the button by adding this code to the SelectedEventsTableViewCell class:
override func awakeFromNib() {
super.awakeFromNib()
attendanceButton.backgroundColor = UIColor.greenColor()
}
However, it doesn't work. The button gets a white background:
Does anybody know why this is happening? It also doesn't work when I set the button's background color programmatically when the button is pressed.
Your help is appreciated.
Check somewhere you set the button background color white.
attendanceButton.backgroundColor = UIColor.whiteColor()
Check where you use SelectedEventsTableViewCell's object in TableView's data source or delegate methods in viewController.
ex.
cell.attendanceButton.backgroundColor = UIColor.whiteColor()
There are several possible reasons.
1.Did you make that outlet accessible? If not, CTRL-DRAG from storyboard.
2.Did you change that after awakeFromNib()? Maybe you set it again in cellForIndexPath(table view data source).
3.Cell was reused but awake once. It means you should recover its state after reuse. prepareForReuse() is designed for this.
I know this issue is old, but I ran into the same thing on iOS 15. The solution is to use the new UIButton.Configuration struct.
if #available(iOS 15.0, *) {
var config = UIButton.Configuration.filled()
config.baseBackgroundColor = UIColor.greenColor()
attendanceButton.configuration = config
} else {
attendanceButton.backgroundColor = UIColor.greenColor()
}
Run something like that in your prepareForReuse function or after you dequeue the cell and it should start working again. There are a lot more customisation options in iOS 15 using that configuration struct so have a dig into it for configuring all other aspects of your buttons.
I'm new in programming and I'm stuck with this little problem. I created a table view with several items, that passes data to a label and an image view through a segue. It all works fine, but now I want to make the image clickable, in order to segue to another view controller to show this image expanded. How can I do that?
class ViewController: UIViewController {
#IBOutlet var titleLabel: UILabel!
#IBOutlet var descLabel: UILabel!
#IBOutlet var imageView: UIImageView!
var titleName: String?
var descName: String?
var imageName: String?
func configureView() {
if let poster = self.imageName {
if let imagePoster = self.imageView {
imagePoster.image = UIImage (named: poster)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = titleName
descLabel.text = descName
self.configureView()
}
The easiest thing to do is to use a UIButton instead of an image view. Set it's type to custom and install the image into the button in IB (Interface Builder).
That way you can trigger an IBAction just like any other button. It also highlights on a touch like you'd expect, triggers on touch up rather than touch down, etc.
If you don't want to use a button-with-image, you have to set userInteractionEnabled = YES on the image view and install a tap gesture recognizer on it. See the docs on UITapGestureRecognizer for more information.