set isTouchInside to false - ios

I need to check if UITextField has received some kind of focus such as isTouchInside then after the code in the if statement is executed i want to set the isTouchInside to false. Is this possible? Or something else should be used here?
I want to add 3 different values to the array when the textfield is selected by user. When i do it like below and touch the txt1 and after that txt2. txt 1 is also added to the array and i don't want that to happen
if self.txt1.isTouchInside{ //this sets isTouchInside to true
print("txt1 touched")
self.array.append(value1)
//how to set isTouchInside to false here?
}else if self.txt2.isTouchInside{
print("txt2 touched")
self.array.append(value2)
//how to set isTouchInside to false here?
}else if self.txt3.isTouchInside{
print("txt3 touched")
self.array.append(value3)
//how to set isTouchInside to false here?
}

That is a read-only property. You can't change the value of isTouchInside. Full stop. not possible.
Explain what you are trying to accomplish at a higher level and perhaps we can help you.

Related

How to enable a button when all textfield is filled

Initially button.isHidden = true, however, I would like to set the button.isHidden = false, when all textfields are filled. Where should I put the If-Else condition in, most probably not in viewDidLoad, please advise.
You might want to take a look at this post UITextField text change event
and then in textFieldDidChange function you should check texts of each textfield

Setting UISwitch isOn programmatically from IBAction calls IBAction again

I just noticed that setting a UISwitch's isOn in its IBAction causes the IBAction to be called again. So the following code:
class ViewController: UIViewController {
var count = 0
#IBOutlet weak var mySwitch: UISwitch!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
mySwitch.isOn = false
}
#IBAction func buttonTapped(_ sender: UIButton) {
mySwitch.isOn = !mySwitch.isOn
}
#IBAction func switchChanged(_ sender: UISwitch) {
print("\(count) pre: \(mySwitch.isOn)")
mySwitch.isOn = !mySwitch.isOn
print("\(count) post: \(mySwitch.isOn)")
count += 1
}
}
prints the following when the switch is turned on one time:
0 pre: true
0 post: false
1 pre: false
1 post: true
switch is turned off in viewDidLoad
switch is turned on by the user
switch is on now when switchChanged (IBAction) is called
0 pre: true is printed
switch is turned off programmatically in switchChanged
0 post: false is printed
switchChanged is called again by the system
switch is off now in switchChanged, and 1 pre: false is called
switch is turned on programmatically
1 post: true is printed
Why is the IBAction called by the system a second time? How does one get around this, say, for example, when wanting to negate the user's action based upon some internal state? I feel like I am missing something embarrassingly obvious, but I'm pretty sure similar code used to work. Is this an iOS bug? It's being run on an iOS 10.2 iPhone 5s simulator, Xcode Version 8.2.1 (8C1002)
It's interesting to note that when the button tied to buttonTapped is tapped (calling that same method), the switch's IBAction is not called.
Your IBAction is presumably hooked up to valueChanged, which doesn't indicate a particular touch event, just exactly what it says, that the value was changed.
I'd suggest setting a variable called something like var didOverrideSwitchValue = false, set it to true just before setting the new switch value, then when the function is called, check for that variable. If it's set to true, then set it to false and return.
Or, if you wish to negate the new setting only when it's turned on, then you could do if (switch.isOn), and then if so then you can respond to it by turning it off, if required.
I've been battling the same issue and found a workaround...
Check the "selected" property on the sender in your switch handler. I've found that it's true the first time through and false the second time, so you can tell if you're really being called by the user action.
I'm guessing whatever is teeing up the event to fire the second time isn't the switch itself, or maybe this property gets cleared after the first event is handled. Maybe a UIKit guru could chime in.
The UISwitch docs for -setOn:animated: say
Setting the switch to either position does not result in an action message being sent.
Seems clear enough. Feels like an OS bug.
Anyway, this seems to work but it makes me uneasy because I don't fully understand why the problem exists in the first place, nor exactly why this fixes it, and I worry that either could change in a future OS update.
UPDATE
This works fine in my little test app but not in my real app, which has a more complex UI hierarchy with a nav bar, tabs, etc. This just reinforces my uneasiness with this solution.

iOS Swift Like Button in UITableView 's Cell

I have a tableView in my app. and in my app users can like a post (best example is instagram). when I like a post my button state change to active and button's color change. but when I scroll the tableView button state back to normal. I know it happen because cell reload again, but what is the best solution for this problem? am I have to overtime that I press like button app send like request and tableview reloadData? (this is not good for server)
EDIT: I have a flag for button, but when I press like button I have to send request and fetch dataArray again. but how can change button state and send like request but not fetch dataArray. it will happen when user manually refresh data.
thanks.
You can set using if and else condition. see below example
if model.data.isFav == true
{
btnFav.selected = true
}
else
{
btnFav.selected = false
}
If you have any flag for button with Posts in your dataArray from which you are loading cells data then you can use that flag to set the color of button. It will take care at the time of scroll.
Set button type to custom in the navigation inspector, then set the background image for default and selected. then use the code below:
// MARK: Set fav button Action
func setFavButtonAction(_ sender : UIButton) {
print(sender.tag)
if sender.isSelected == true {
sender.isSelected = false
}else{
sender.isSelected = true
}
}

Swift update UILabel with dispatch_async not working

Why doesn't this work?
dispatch_async(dispatch_get_main_queue(), {
self.timeStringLabel.text = "\(self.timeStringSelected)"
println(self.timeStringLabel.text)
})
I'm trying to update a label in Swift but the UI for the label never changes. I keep googling it, but I can't find any responses that don't use dispatch_async. What am I doing wrong?
1st Edit: I was mistaken. I'm not printing the updated text. The text never changes. It always prints out Optional("0") if that helps. The default value is 0 as defined in the Storyboard.
I have tried it with and without dispatch_async without any success. I also tried adding
self.timeStringLabel.setNeedsDisplay()
Immediately after updating the text, but that also doesn't work.
Edit 2: Here's the complete function + UILabel declaration
#IBOutlet weak var timeNumberLabel: UILabel!
#IBAction func timeNumberButtonPressed(sender: UIButton) {
println("Number Selected. Tag \(sender.tag)")
dispatch_async(dispatch_get_main_queue()) {
self.timeNumberOneButton.selected = false
self.timeNumberTwoButton.selected = false
self.timeNumberThreeButton.selected = false
self.timeNumberFourButton.selected = false
if sender.tag == 0{
self.timeNumberSelected = 0
} else if sender.tag == 1 {
self.timeNumberSelected == 5
} else if sender.tag == 2 {
self.timeNumberSelected == 10
} else {
self.timeNumberSelected == 24
}
sender.selected = true
self.timeNumberLabel.text = "\(self.timeNumberSelected)"
self.timeNumberLabel.setNeedsDisplay()
println(self.timeNumberLabel.text)
}
}
The label is clearly visible as shown in this picture. I didn't think it would be this hard to implement, but I was very wrong. I'm willing to bet it's something really simple that I'm missing.
Try adding the line
self.timeStringLabel.setNeedsDisplay()
(after the label change)
Because the code is run asynchronously, the interface-updating methods may miss the change and not display it (especially if a time-consuming bit of code occurs before the label change). Adding this code forces the interface to check for and display any changes it may have missed.
This should be used after any asynchronous task that changes the interface, as the task's running may overlap with the interface methods, resulting in a missed change.
*Thanks to the iOS Development Journal

Disable editing of a text field

I want to start by saying this is my first project and I am trying hard to find answers myself before posting here. I thought I had found the code to accomplish this. I have no errors but when I run, the field is still editable. So, how can I disable editing of my field when the rule I have set is true?
if sport.count == 1 {
enterSport.text = sport[0] as String //need to convert to a string
enterSport.editing; false //do not allow editing
} else {
//do nothing
}
I have defined the array previously so the if statement is true. Thank you for you assistance.
enterSport.userInteractionEnabled = false instead of editing.
editing is a read-only property to indicate if the field is currently being edited or not.
Swift 5:
enterSport.isUserInteractionEnabled = false
To summarize the answers and comments above:
editing is a readonly property. You can't set it.
If it's a UITextView, it has an editable property which you can set to false.
If it's a UITextField, you need to use the enabled property or the userInteractionEnabled property. Either of those will prevent editing. As
With Swift 3 Apple changed the property userInteractionEnabled to isUserInteractionEnabled.
The code looks like this:
textfield.isUserInteractionEnabled = true
There is one more way of avoiding keyboard appearing
You can set an UITapGestureRecognizer like this:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.textFieldEditing))
self.enterSport.addGestureRecognizer(tap)
and
#objc func textFieldEditing() {
self.enterSport.resignFirstResponder()
}

Resources