How to make turtle.onscreeenclick() return true or false answers - turtle-graphics

I was wondering how can I make the function:
turtle.onscreenclick()
return me a True or False answer.
For example, if I click on the turtle window I want it to return True

Related

Can't change UIView.isHidden property after action is called by UIBarButton

I am using UIView's isHidden property to show/hide controls on a posting interface I'm developing. I use this to change the controls available when the keyboard is up/down, post types are selected, etc. I have written a method triggered by notifications that sets the view properties (mainly .isHidden) and animates changes, which is called when the keyboard appears, or when the user selects a post type which then changes the controls available to them. This usually works perfectly fine, and the properties change as intended, but when the method is triggered by a UIBarButton action, the view being set to visible becomes unresponsive. After hitting bar button I am unable to change the isHidden property of the view (even by explicitly setting isHidden to true or false... it does not change).
Here is the method that changes the view properties:
#objc func animateActionViewChange(_ sender: Notification) {
// DEBUG
print("\n[BEFORE]\nnormalActionView.isHidden: \(normalActionView.isHidden)\nkeyboardActionView.isHidden: \(keyboardActionView.isHidden)\nkeyboardUp: \(keyboardUp)\nactionViewActive: \(actionViewActive)")
// save prev state
let normalTemp: Bool = normalActionView.isHidden
let keyboardTemp: Bool = keyboardActionView.isHidden
// set new state based on env vars
normalActionView.isHidden = keyboardUp ? true : !actionViewActive
keyboardActionView.isHidden = keyboardUp ? !actionViewActive : true
// DEBUG
print("[AFTER]\nnormalActionView.isHidden: \(normalActionView.isHidden)\nkeyboardActionView.isHidden: \(keyboardActionView.isHidden)\nkeyboardUp: \(keyboardUp)\nactionViewActive: \(actionViewActive)")
// animate opacity changes
if normalActionView.isHidden != normalTemp {
let targetAlpha: CGFloat = normalTemp ? CGFloat(1) : CGFloat(0)
UIView.animate(withDuration: actionViewAnimationDuration / 2) {
self.normalActionView.alpha = targetAlpha
}
}
if keyboardActionView.isHidden != keyboardTemp {
let targetAlpha: CGFloat = keyboardTemp ? CGFloat(1) : CGFloat(0)
UIView.animate(withDuration: actionViewAnimationDuration / 2) {
self.keyboardActionView.alpha = targetAlpha
}
}
}
and the action called by the UIBarButton (this issue concerns when the button title is 'back'):
// back/cancel button action
#IBAction func cancel(sender: UIBarButtonItem) {
if sender.title == "Cancel" {
// cancel ongoing request if there is one
if let request = ongoingRequest {
if !request.isFinished {
request.cancel()
}
}
self.performSegue(withIdentifier: "cancel_unwindFromNewPostView", sender: self)
} else {
// reset post type to default (general)
postType = .general
// set actionViewActive bool to set visibility going forwards
actionViewActive = true
// hide datePicker
self.datePickerView.isHidden = true
actionViewAnimationDuration = 0.35
NotificationCenter.default.post(name: NSNotification.Name("animateActionViewChange"), object: nil)
UIView.animate(withDuration: 0.35) {
// layout
self.view.layoutIfNeeded()
}
// revert button text to 'cancel'
cancelButton.title = "Cancel"
}
}
Here is the output of the debugging flag before the 'back' button is hit:
[BEFORE]
normalActionView.isHidden: false
keyboardActionView.isHidden: true
keyboardUp: true
actionViewActive: true
[AFTER]
normalActionView.isHidden: true
keyboardActionView.isHidden: false
keyboardUp: true
actionViewActive: true
after:
[BEFORE]
normalActionView.isHidden: true
keyboardActionView.isHidden: true
keyboardUp: false
actionViewActive: true
[AFTER]
normalActionView.isHidden: true
keyboardActionView.isHidden: true
keyboardUp: false
actionViewActive: true
As you can see above, the isHidden property of the normal action view is not changing, even though it is being set to 'false'. I originally thought this was because a reference was being lost somewhere (though I thought that would result in a nil reference, which I don't have), so I made the references to normalActionView and keyboardActionView strong. This did not fix the problem, obviously, so I put the changes to isHidden in a method called by a notification to ensure it was always on the same thread (which it is, I checked by printing the current thread, it is always main), but that did not help either.
isHidden behaves as though it is cumulative. For example, if you set it to true twice in a row, you would need to set it to false twice.
I usually use a check for the opposite value before setting a value.
if view.isHidden == false {
view.isHidden = true
}
Why did you bind uiview animation to keyboard notifications ? Is that a required part of your application UI ? I think you can animate your view after a button pressed. Because it's highly difficult to catch the exact keyboard animation speed together working with any view. What I mean is that keyboard closes fast while notification center tries to catch and animate view.
Thanks.

set isTouchInside to false

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.

disable textfield editing without blocking the clear button

do you know how I can make an edittext get non-editable for the user, but allow it to use the textfield's own clearbutton to delete it?
I tried using the:
textField.isEnabled = false
and
textField.isUserInteractionEnabled = false
but it did not work very well because it disables everything
Return false from the text field delegate’s textFieldShouldBeginEditing.

XLForm dynamically change section title

I just started using XLForm and faced some problems. I have a section which title I have to change depending on field above it.
So when I select an option, I need the same title for section.
In code I do:
if formRow.tag == "subCategory" {
switch newValue.description {
case lightVehicle:
vehicleSection.hidden = false
mileageRow!.hidden = false
break
case heavyVehicle:
mileageRow!.hidden = true
mileageRow!.value = nil
vehicleTypeRow!.hidden = true
break
case bikes:
mileageRow?.hidden = true
vehicleTypeRow!.hidden = false
break
default:break
}
// Here I set the title
vehicleSection.title = newValue.description
}
So I know that the value is changed, but for displaying it I need smth like reloadFormRow(). Unfortunately there is no such method for sections
I found hacky way to do that is tableView.reloadData(), but in documentation they not recommend do any manipulations directly with tableView itself.

Why textFieldShouldReturn is still working when i return false?

I am hiding keyboard on return key pressed by implementing function textFieldShouldReturn and calling resignFirstResponder() inside this function. It works perfectly but when I return false in this function, still then keyboard is hiding. Now here I do not understand that what is use of returning true or false if keyboard is hiding in both case?
The keyboard is being dismissed due to the call to resignFirstResponder. It has nothing at all to do with the return value of textFieldShouldReturn.
In my experience, the return value of textFieldShouldReturn almost has no use. In the vast majority of cases it makes no difference whether you return true, or false.
I return false in all my uses of testFieldShouldReturn. If you return true and your implementation of textFieldShouldReturn sets the first responder to a UITextView, the text view ends up getting the newline added to it. Returning false from textFieldShouldReturn ensures this won't happen. But this was based on experience with iOS 4 or 5 a few years ago. I don't know if that still happens with the latest versions of iOS.
The return value from textFieldShouldReturn answers a different question. If the text field has an action for the control event editingDidEndOnExit, that will cause the keyboard to dismiss unless textFieldShouldReturn is implemented to return false.
I have tried that wether true or false, the keyboard do dismiss, here is the code:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return false
}
The keyboard do dismiss or not depends on "textField.resignFirstResponder()",
if you delete it, the keyboard does no longer dismiss, whether true or false.
It's been a long time since I worked on Native iOS. Nearly 2 years. I've just picked up swift 5 and working through the forgotten and new parts right from the basics. Here's what i have observed.
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
if searchTextField.text != "" {
return true
}
searchTextField.placeholder = "Type Something"
return false
}
Here when I press the return key without typing a single letter in it, Keyboard does not dismiss. Rather the placeholder text changes to notify user to type something. The magic of returning false from textFieldShouldEndEditing.
If I press the same return key after entering at the very least a single character, it simply dismisses the keyboard. Courtesy of returning a true from the same textFieldShouldEndEditing.
I also didn't observe any difference when I placed textField.resignFirstResponder() regardless if I return true or false.
Conclusion:
true dismisses the keyboard. false stops the keyboard from dismissiong.

Resources