Swift 3:Delegate Within Tapgesturerecognizer in Generics Doesn't Get Called

Swift 3 : Delegate within TapGestureRecognizer in Generics doesn't get called

You are never setting the submitAgeDelegate of the GenericController. Having a member of the same name in your MyCell class doesn't help.

You need to get a reference to your GenericController in order to set its delegate; there's no way around that. (This doesn't have anything to do with generics; it's the same for non-generic classes.) Since it looks like you're using it as a UICollectionViewCell, you might use the reference that you make in tableView:cellForRowAtIndexPath: or similar.

How to tell whether a UIPageViewController page turn is due to a swipe/pan or a tap

The question is really how to determine which of several gesture recognizers is responsible for something happening.

The key thing to understand is that any gesture recognizer can have more than one target/action installed. The UIPageViewController sets its own target/actions internally, but that doesn't preclude others being added.

When setting up a sub-class of UIPageViewController after it has been loaded, or instantiated, add the following code (or Swift code equivalent):

for (UIGestureRecognizer *gr in self.gestureRecognizers) {
if ([gr class] == [UIPanGestureRecognizer class])
[gr addTarget:self action:@selector(panGestureOccurred)];
else if ([gr class] == [UITapGestureRecognizer class])
[gr addTarget:self action:@selector(tapGestureOccurred)];
}

Then, add the two target/action methods to the sub-class:

- (void)tapGestureOccurred
{
// Set a flag here to rely on later after the page turn
}

- (void)panGestureOccurred
{
// Reset a flag here to rely on later after the page turn
}

This obviously generalizes for any other types of gestures, but currently only the pan and tap gestures appear to be supported by a UIPageViewController.

panGestureOccurred will be called multiple times while the user is touching the screen and moving a finger/stylus. The tapGestureOccurred is only called once.

But only one of the two methods will be called, depending on which gesture recognizer the page view controller in its infinite wisdom decides wins. This all seems to work much more robustly than the too-low level touchesBegan and touchesMoved idea posited in the original question.

didDeselectItemAt For UIcollectionView is not called but didSelectItemAt is called?

I'm going to guess that your code is different from what you have shown us, and in that in your real code, you have not given the correct signature for didDeselect. Here's why. Look carefully at the code you have shown:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("didSelectItemAt")

}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
print("DESELECT")
}

Now ask yourself: Why did the compiler permit the second func to stand, even though you forgot to say override? I'm guessing it's because it is not an override. There is something wrong with the signature, so it's just a meaningless function that doesn't conform to UICollectionViewDelegate.

Try using code completion to re-enter this function. If all goes well, it will be an override and it will start working.


To illustrate more precisely: This compiles, but the second method will never be called:

class CV : UICollectionViewController {
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("didSelectItemAt")
}
func colectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
print("DESELECT")
}
}

But this doesn't compile, because the signature is correct but we forgot override:

class CV : UICollectionViewController {
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("didSelectItemAt")
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
print("DESELECT")
}
}

But in that second one, if we do say override, it compiles and works.

UIImagePickerController crashes app | Swift3, Xcode8

Add this key to your info.plist,

Key : Privacy - Photo Library Usage Description [ NSPhotoLibraryUsageDescription ]
String Value : We need access to your camera roll and photo library, so that we can do operations on it. [ Customise it in your own way]

That's it, Clean & Run the project.

Sample Image

Dismiss modal view form sheet controller on outside tap

Ah ok. So I'm afraid thats not quite possible using the presentModalViewController: method. The whole idea of a "modal" view/window/message box/etc. pp. is that the user cannot do anything else than processing whatever the view/window/message box/etc. pp. wants him/her to do.

What you want to do instead is not present a modal view controller, but rather load and show your form view controller the regular way. Note in your master controller that the form is just showing e.g. with a BOOL variable and then handle there any taps that might occur. If your form is showing, dismiss it.

iOS - Dismiss keyboard when touching outside of UITextField

You'll need to add an UITapGestureRecogniser and assign it to the view, and then call resign first responder on the UITextField on it's selector.

The code:

In viewDidLoad

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];

In dismissKeyboard:

-(void)dismissKeyboard 
{
[aTextField resignFirstResponder];
}

(Where aTextField is the textfield that is responsible for the keyboard)

Swift 3 version looks like that

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard (_:)))
self.view.addGestureRecognizer(tapGesture)

For dismissKeyboard

@objc func dismissKeyboard (_ sender: UITapGestureRecognizer) {
aTextField.resignFirstResponder()
}

Swift 3 protocol extension using selector error

This is a Swift protocol extension. Swift protocol extensions are invisible to Objective-C, no matter what; it knows nothing of them. But #selector is about Objective-C seeing and calling your function. That is not going to happen because your on(tap:) function is defined only in the protocol extension. Thus the compiler rightly stops you.

This question is one of a large class of questions where people think they are going to be clever with protocol extensions in dealing with Cocoa by trying to inject Objective-C-callable functionality (selector, delegate method, whatever) into a class via a protocol extension. It's an appealing notion but it's just not going to work.



Related Topics



Leave a reply



Submit