Swift disable touchesEnded - swift

I am trying to disable the TouchesEnded event in my Code when someone clicks on a button. And when the user clicks on another button the TouchesEnded should be activated again.
So for understanding what my app does, every time you click on the screen the text on the screen changes. But when the user clicks the Button a, this touch event should be disabled. And when the user clicks on button b it should be activated again.
i have already tried self.view.isUserInteractionEnabled = false , but then I am not able to click on the button to activate the userInteraction again.
Does anybody as an idea how to fix this problem?

From the behavior you're describing, I'm assuming that your button is a subview of the view that's toggling isUserInteractionEnabled? Setting that flag to false will prevent user interaction on the view's subviews as well. You can fix this by adjusting the view hierarchy so that your button and your self.view are siblings: aka create a container view that has both the button and your referenced self.view as children.

It doesn't make sense to "disable the TouchesEnded event".
You need to enable or disable your buttons and let the events be sent as normal. Alternatively, as you say, you could use isUserInteractionEnabled=false to make a button no longer respond to taps.
Edit:
After rereading your post, I think I understand what you are saying:
"every time you click on the screen the text on the screen changes" - this means clicking anywhere but on the buttons.
"But when the user clicks the Button a, this touch event should be disabled. And when the user clicks on button b it should be activated again."
So you want button clicks to disable/enable taps elsewhere on the screen.
I would suggest using a tap gesture recognizer (UITapGestureRecognizer) attached to the content view for your view controller. Those trigger an action when they fire, and have an isEnabled property. If you set that to false the tap gesture recognizer will stop responding to taps.

Related

How to keep button functionality when adding other views on top of said button in a subclass?

I have a button in my app, but I have create a UIButton subclass that adds another view and a few labels on top of it. The problem is that now in my app the button does not respond to taps. How would I go about fixing this?
I was able to fix this problem by setting the view that was covering my button with the property
userInteractionEnabled = true
That allowed for the text to go through my view back to my button

IOS - Prevent page curl in PageViewController

I have a UIPageViewController that displays images. I've set an opportunity to mail a desired image that the user is currently viewing. For this if user taps once the central region of the screen a tab bar appears with a bar button item titled "Mail". And when this button is pressed the imaged is attached to a mail composer. But I have a little problem here: As the bar button is on the left edge when I tap it page curl takes place and I can't get the mail composer-more precisely I can't get the button tapped. How would I prevent page curl when this button is pressed?
There are several ways you can overcome this problem. The second solution is easier to implement.
The first one is to access the gesture recognizers defined for the page view controller (from the "gestureRecognizers" property, which returns an array of gesture recognizers), search for the "tap type" gestures and then assign them a delegate to your view controller. This delegate will implement the method gestureRecognizer:shouldReceiveTouch: and will check if the tap occurred in the tab bar position and will return NO in such case, so the gesture will not be triggered. Your delegate will return YES if you still want the tap but it is outside the tab bar region.
Another possibility is to define a new tap gesture recognizer and assign it to the tab bar. In such case when you tap over the button (which is in the tab bar) the target selector that you assigned to your tap gesture will be triggered. Clearly, this selector will do nothing as its purpose is just to avoid propagation to the gestures which are beyond it (this is the default behavior).
I think my answer came too late from your request, so if you have found a better solution please post it!

How do I stop the keyboard from blocking an external notification's close buttons on iOS?

I have a UIAlertView with a text field in it. This view works correctly; it appears in the top half of the screen, so when the keyboard comes up, both buttons and the text entry are still visible and tappable.
However, if a local or remote notification from another application comes in while the text field is the first responder and the keyboard is up, it hides my alert and places the new alert behind the keyboard. If the alert is more than one line long, its buttons cannot be clicked and the user cannot dismiss it. It's not dismissing my alert view via any of the normal mechanisms - neither alertView:clickedButtonAtIndex: or alertView:cancel: get called on my delegate - but rather simply hides it temporarily using some unknown mechanism.
Can I get notified when a notification hides my alert view, so I can dismiss the keyboard and let the user deal with the notification? Are there any other events I can hook into to catch this case?
Register for the application’s UIApplicationWillResignActiveNotification; when you get that, dismiss your alert view. From the docs:
An active application can be said to have focus. It gains focus after being launched, loses focus when an overlay window pops up or when the device is locked, and gains focus when the device is unlocked.
Alert views, such as those presented by notifications from other apps, are overlay windows. Watch for that notification, close your alert so the keyboard doesn’t get in the way, and be prepared to re-display the alert if your app then gets the UIApplicationDidBecomeActiveNotification.
I hope this will help...
- (void)willPresentAlertView:(UIAlertView *)alertView {
if (newAlertViewWhichBlocks.tag == alertView.tag) {
[yourTextField resignFirstResponder];
}
}
Do remember to set delegate of alert view.

How can I dismiss the view appeared by touching Add item in UINavigationController?

I have added add(+ symbol button) button to my navigation controller.
When I click it a view appears from bottom. I added a navigation bar and two buttons to it.
One save and one cancel button. And the view have one textEdit box. After editing I can save or cancel. If I touch cancel I need the view to disappear like it should go down again.
I think all iPhone , iPodTouch users use it. Like when they touch Add item then a view appears from bottom and when they cancel it goes down again. How can I make in this way in my application.
Maybe you're talking about a UIActionSheet?!?
http://developer.apple.com/IPhone/library/documentation/UIKit/Reference/UIActionSheet_Class/Reference/Reference.html

Change UIActionSheet after doing it's job

I have to import some XML data into my app.
Now I open a UIActionSheet and add as a subview a UIActivityIndicatorView. Initially I show a progress wheel and a button with 'Cancel'.
When the XML has been imported, I want to hide the progress wheel (this it's easy) and change the button into a 'Done' button, all in the same UIActionSheet.
What I'm doing now is closing the UIActionSheet with the option dismissWithClickedButtonIndex, but I don't like this way because it's the same as pressing 'Cancel', and then I show an UIAlertView displaying "All data has been imported".
Is this possible ?
You shouldn't be doing that, when it loads correctly just dismiss the ActionSheet. On the other hand if an error occurs then display an alert.
Think about the user who will use the app multiple times a day, a Done message each time will be a waste of time.
UPDATE
As i understand your goal is to use the ActionSheet just as a popup (with Cancel ability), if so, just call dismissWithClickedButtonIndex:animated: when your XML loading is done.
If its successful then just call the dismiss method, if its unsuccessful then call the dismiss and popup an alert
This is a bit of a hack but should work. Note that there is a good chance that this will result in your app not being accepted into the app store as you're messing around with the action sheet in ways Apple didn't intend.
Initially display the action sheet with both the 'Done' and 'Cancel' buttons. Before displaying the sheet hide the 'Done' button using its hidden property. To see how to access the 'Done' button see this question.
Then when you're hiding the UIActivityIndicatorView, also change the hidden property of both the 'Cancel' and 'Done' buttons, so that the 'Done' button becomes visible. If the 'Done' button appears in the wrong position move it by modifying its centre property.