I have a tableview controller with dynamic cells. One of the cells has a button to toggle editing the table, so that the user is less likely to delete something and has to actively enable editing.
When they press the button it is supposed to toggle editing the table and the text of the button. Editing the table works, but the text for the button just flashes then goes back to the original text.
#IBAction func deleteTouched(sender: UIButton) {
editing = !editing
let indexPath = NSIndexPath(forRow: 1, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath) as! FieldInfoCell
cell.deleteButton.titleLabel?.text = editing ? "Done" : "Delete Estimates"
}
I first tried referencing the sender instead of the button in the cell
#IBAction func deleteTouched(sender: UIButton) {
editing = !editing
sender.titleLabel?.text = editing ? "Done" : "Delete Estimates"
}
Why does the text flash to Done and then back to Delete Estimates when I enable editing?
Instead of setting text button's title label use setTitle method like
button.setTitle("<#your title#>", forState: .Normal)
Because button needs title, titleColor, image, backgroundImage, attributedTitle etc for each state(.Normal,.Selected,.Highlighted etc). Each time button changes its state these properties get refreshed based on the state - even if you set titleLabel.text or imageView?.image. You can set font,layer properties for titleLabel and imageView
Related
I have created a .xib view cell view and it contains a button, label, and some text inside, how do I make the button clickable and get the details of the view ie, the label, and text?
A way to do this is by adding a tag and target to the button.
cell.moreBtn.tag = indexPath.row
cell.moreBtn.addTarget(self, action: #selector(showClickedBtn(sender:)),
for: .touchUpInside)
After that, create a function that will receive the action.
#objc func showClickedBtn(sender: UIButton) {
print("Button \(sender.tag) Clicked")
}
I want to animate a card when pressed.
I made a table view, and added a custom cell to it.
and in custom cells, I created a UIButton and applied .touchUpInside gesture on the button.
when the button is pressed, the background color of the card will turn to red.
but weird thing is, if I press the first button,
6th, 10th, 15th and more cards' background-color also changed.
I check the function called only once but it happens to several cards.
I assume this might be related to dequeueReusableCell cell but not sure...
can anybody please help me out?
#objc func cardPressed(){
changeCardBackground()
}
private func changeCardBackground(){
var color:UIColor = .red
if(isCardRed){
color = .white
}
isCardRed = !isCardRed
UIView.animate(withDuration: 0.3) {
self.cardButton.backgroundColor = color
}
}
TableviewCells are reused you need to add prepareForReuse Method like :
override func prepareForReuse() {
super.prepareForReuse()
self.cardButton.backgroundColor = defaultColor
}
I have swift4 in my project and set an image for button state selected. But, when the button is in the selected state the image is not getting changed. I have changed the image in storyboard as well as code but nothing works.
ChkBoxBtn.setImage(UIImage(named: "unCheck"), for: [])
ChkBoxBtn.setImage(UIImage(named: "alertCheck"), for: .selected)
When a user taps a button the associated action will be called.
Within that action or method you have to change the .isSelected property of the concerned UIButton and set it to true
yourButton.isSelected = true
This will then reflect the image you have set in storyboard for the selected state of the UIButton
Create an action outlet to view controller, then add the below code. This helps you to switch between selected and deselected states easily.
#IBAction func ChkBoxBtnTapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
}
I've got a NSTextView in my view that works as it should, but when I show a NSAlert and close it, it becomes uneditable (with text still selectable). The NSAlert is a save/cancel alert that updates the textView's string when the user selects save, the string is not updated when the user presses cancel. In both cases the textView was uneditable, the alert is shown when the users has made changes and wants to change the tableView selection.
It feels like the textView refuses first responder but when breaking and checking in the console its "true", I also checked some other values after the view was uneditable:
isEditable was True
isSelectable was True
canBecomeKeyView was True
acceptsFirstResponder was True
acceptsTouchEvents was False, tested True but did not work
My "test" setup:
Video, same when triggering the popup from a tableview selection change and a button : video
My popup code
func dialogOKCancel(question: String, text: String) -> Bool {
let myPopup: NSAlert = NSAlert()
myPopup.messageText = question
myPopup.informativeText = text
myPopup.alertStyle = NSAlertStyle.warning
myPopup.addButton(withTitle: "OK")
myPopup.addButton(withTitle: "Cancel")
return myPopup.runModal() == NSAlertFirstButtonReturn
}
let answer = self.dialogOKCancel(question: "Ok?", text: "Choose your answer.")
also tried:
let a = NSAlert()
a.messageText = "Delete the document?"
a.informativeText = "Are you sure you would like to delete the document?"
a.addButton(withTitle: "Delete")
a.addButton(withTitle: "Cancel")
a.alertStyle = NSAlertStyle.critical
a.beginSheetModal(for: self.view.window!, completionHandler: { (modalResponse) -> Void in
if modalResponse == NSAlertFirstButtonReturn {
print("Document deleted")
}
})
Stuff I tried:
Remove any textView updates, showing the alert still breaks it
Dragging a new "untouched" textView in my storyboard but now both textViews became uneditable
I tried showing the NSAlert when clicking a button instead of when changing the tableView's selection. Here the textView I was editing stays first responder but as soon as Ieave the textView, its uneditable again.
I tried triggering just an animation instead of the NSAlert, here the textView keeps working
I Tried replacing the NSAlert with a overlay View that had a title/description/buttons. when showing this dialog, the textView also became uneditable
I'm stuck on this for a long time and any help is greatly appreciated,
Thanks
After long time of debugging, I found this line to be the one that broke the textfields, I will leave this post online in case someone else stumbles upon this weird problem
window?.styleMask = NSFullSizeContentViewWindowMask
removing this line fixed the problem.
I've got custom buttons on a custom tableview cell, when button is pressed the image changes to checked/unchecked (see code below).
Issues is: How do I add the cell row data thats checked to an array that will be its own table (eg. a table with only checked data).
#IBAction func tickAction(sender: UIButton) {
println("SSSS")
if (sender.selected) {
sender.setImage(UIImage(named:"Unchecked.png"), forState: .Normal)
sender.selected = false
}
else {
sender.setImage(UIImage(named:"Checked.png"), forState: .Normal)
sender.selected = true
}
}
A solution would be to extend UITableViewCell into your own class that holds the other data or holds a reference to a model that holds the data.
Ideally, the view controller should hold the models and should get notified by a view that the action took place to act accordingly. The view controller should then add the data to the other array.