UIButton set titleLabel setValue(newLabel, forKeyPath: "titleLabel") - ios

let newLabel = UILbael()
let button = UIButton()
button.setValue(newLabel, forKeyPath: "titleLabel")
crash info
setValue:forUndefinedKey:]: this class is not key valuecoding-compliant for the key titleLabel
how do it if use kvc ?

You should use setTitle method to set button title for states.
button.setTitle("Your button title here", for: .normal)
setValue(_:forKeyPath:) is a method from NSObject class which UIButton is a subclass of. It is not recommended to use KVO. Read this thread for more information.

KVC is only supported for NSObjects, and Apple seems to be phasing it out in Swift. I don't recommend using KVC for new development.
You also shouldn't use the button's titleLabel to set the button's title. To quote the Apple docs on UIButton:
To set the actual text of the label, use setTitle(_:for:)
(button.titleLabel.text does not let you set the text).
If you have two buttons, firstButton and secondButton, and you're trying to copy the title from the first to the second, your code might look like this:
let title = firstButton.title(forState: .normal)
secondButton.setTitle(title, for: .normal)

I think u have not confirmed your outlet in your ViewController that's a reason when use button and set title there are not any references of a button in ViewController. it's like the use of any object without any initialize. So make sure and check button outlet. Please see code so we make sure what is actual problem.

Related

What is the difference between `UIbutton.setImage` and changing `UIbutton.imageView`?

What is the difference between UIbutton.setImage and changing UIbutton.imageView?
buttonA.imageView?.image = UIImage(named: "name")
buttonA.setImage(UIImage(named: "name"), for: .normal)
When I try to use setImage and then try to get it's position using frame.origin.x, the position returned are not what I expected and so I used .imageView?.image approach. But when I use that, I observed that clicking on the button changes the image for a brief time before continuing.
the function offers you the ability to set different icons for various states
setImage(imageNormal, for: .normal) //normal state
setImage(disabledImage, for: .disabled) //disable UI representation
UIButton.imageView according to documentation read-only property https://developer.apple.com/documentation/uikit/uibutton/1624033-imageview
For some clarification...
If you set the .image property directly:
buttonA.imageView?.image = UIImage(named: "name")
You have not informed the button class that it has a new image property.
The same thing applies to setting the title:
buttonA.titleLabel?.text = "My Title"
That will change the text of the title label, but only until the next UI update... at which point UIKit will use the value assigned via:
buttonA.setTitle("Button A", for: .normal)
That's why it's important to use both .setImage(...) and .setTitle(...) instead of setting the property itself.

Setting a button image depending on another variable? Swift

so I'm very new to swift. I currently have 16 buttons all set to individual outlets box1,box2,box3 etc.
Each box I have set a tag and what I am trying to do is set the image of a particular box based off another integer variable to determine which box I'm changing.
So say I do a calculation and index = 4.
Is there a way I can then set box(index).setImage?
I understand this probably isn't the best way to do it or even possible, maybe I can set each button to an array of objects instead? Any tips would be great.
If you want to set the image of button through some tag then you don't need any outlets for the buttons.
You can change your button image by finding the button through viewWithTag() property.
Here is the code
var button = self.view.viewWithTag(Your_Calculated_Index) as! UIButton
button.setImage(Your_Image, for: .normal)
You can try this. You need to create an array for the button.
#IBOutlet var allbtns: [UIButton]!
for buttons in allbtns{
if buttons.tag == 4{
print("Button 4 ");
buttons.setImage(UIImage(named: "imagname"), for: UIControlState.normal)
}else{
print("Other buttons except 4 ");
}
}

UIAlertAction font changes on the tap

I am setting UIAlertAction label attributedText in order to set custom Font in UIAlertController.It works but when i tap on the UIAlertAction it changes its font to default for some time and then disappears. Here is the code
let lb = (action.value(forKey: "__representer") as AnyObject)
let label = lb.value(forKey: "label") as? UILabel
label?.attributedText = myMutableString
I think best idea would be to create your own custom alert controller instead of accessing private properties to set your values. Not sure if Apple would accept it or not.
I don't know the solution for your problem but I know the problem which is
Problem: When you press UIAlertAction's button it changes its state. It behaves just like the states of UIButton. So if you know which property you should set for its highlighted state then you can set that to solve this issue.

Clickable username and hashtag in UILabel (Not UIWebView!)

The idea is for example there is a UILabel (like in Instagram)
"User1 started following User2"
I want that when we click either on chunk of text 'User1' or 'User2' it does some action(Not opening like usual link in UIWebView)
Tried TTTAttributedLabel , didn't find anything which will fit me exactly.
in case you haven't found something yet, you could also try ActiveLabel.swift which is an UILabel drop-in replacement supporting Hashtags (#), Mentions (#) and URLs (http://) written in Swift.
Here is a simple example:
import ActiveLabel
let label = ActiveLabel()
label.text = "This is a post with #hashtags and a #userhandle."
label.hashtagColor = .blueColor()
label.handleHashtagTap { print("Success. You just tapped the \($0) hashtag") }
Disclaimer: I'm the author of the library.
Using a method will force you to make two of them (User 1 and User 2) and align it every time you draw it. I think it's better to make a category ( or Swift extension) of UILabel that gets it's frame and add a gesture recognizer with that frame bounds
Just make a new button. It can do everything a label can do, and also have actions. You can easily update the text of a button by using:
exampleButton.setTitle("example", forState: .Normal)
And you can add an action like this:
#IBAction func myExample(sender: UIButton) {
//do action
}
Hope this helped.

Check UIButton already exists or not in Swift

I found Objective-C coding for this topic. But the problem is that most of the classes and functions in Objective-C is deprecated in swift programming language.
I am using a UIButton inside a UITableView with flagForAction[Boolean Value]. So what i want to achieve is that if the UIButton is created once , there is no need to recreate it. So for that i need to check whether the UIButton already exists or not. Somebody suggested me the concept of tag for this, applying a particular tag to this UIButton and checking that is exist on the view or not. But i don't know how to do that.
Setting a tag:
myButton.tag = 1234 // some unique value, probably better to define as an enum or constant
Retrieving a view by tag (likely in cellForRowAtIndexPath):
if let myButton = tableviewCell.viewWithTag(1234) { // change tableviewCell for whatever your tableview cell variable name is
// myButton already existed
} else {
// TableviewCell doesn't contain a myButton, so create one here and set the tag as above
}
in swift 2.0
for view in myview.subviews {
if let btn : UIButton = view as? UIButton {
// access button here, either tag or title etc..
}
}
I am using view as example. You can also identify a button with accessibilityIdentifier property of the button, if you dont want to use tag proprerty of button.
For example :
var button = UIButton(frame: CGRectMake(0, 0, 100, 44))
button.accessibilityIdentifier = "button 1"
self.view.addSubview(button)
Here, i created a button with accessibilityIdentifier "button 1"
While creating the next button, i will check in the subview if it contains the button with accessibilityIdentifier "button 1", as below
var subviews : NSArray = self.view.subviews
for button : AnyObject in subviews{
if(button .isKindOfClass(UIButton)){
if(button.accessibilityIdentifier == "button 1"){
println("Button Already Exists")
}else{
println("Create new button")
}
}
}

Resources