0

after some years I tried again to work with XCode to write some little apps for iOS.

My MainViewController contains these lines in viewdidload:

UIStoryboard* overviewStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *overviewController = [overviewStoryboard instantiateViewControllerWithIdentifier:@"Overview"];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:overviewController];

...

[self addChildViewController:nav];
[self.view addSubview:nav.view];
[nav didMoveToParentViewController:self];

the Controller behind the Overview contains the whole gesture recognition in view did load:

the property

@property (nonatomic, strong) UISwipeGestureRecognizer *swipeGestureUpDown;

viewdidload:

self.tableView.dataSource = self;
self.tableView.delegate = self;

// gesture recognizer top
self.swipeGestureUpDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreen)];
self.swipeGestureUpDown.numberOfTouchesRequired = 1;
self.swipeGestureUpDown.direction = (UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown);

[self.view addGestureRecognizer:self.swipeGestureUpDown];

and swipedScreen only an nslog:

- (void)swipedScreen:(UISwipeGestureRecognizer*)gesture
{
    NSLog(@"somewhere");
}

THE overviewcontroller contains a tableView with custom cells. The maincontroller passes this overviewcontroller as rootcontroller to a navigation, which should be slideUp if you swipeUp, and slideIn if you swipeDown. The maincontroller is calling the navigationcontroller with rootcontroller as you've seen above.

Nothing happens, no gesture is recognized, and in some tries it crashes with this message

unrecognized selector sent to instance

does somebody now what to do?

ahmet2106
  • 4,957
  • 4
  • 27
  • 38
  • You need to provide the complete error. The complete error states the class and method names. – rmaddy Jun 18 '13 at 02:46
  • I probably know what's happening. I don't want to jump the gun though. Could you post your swipedScreen function's definition? Ok, why don't you just post the whole swipedScreen function whatever it is. – zambrey Jun 18 '13 at 03:00
  • now i added the 5 lines of code :) – ahmet2106 Jun 18 '13 at 03:06
  • did i forgot an : at the end of @selector(swipedScreen)? I think yes, but really doesnt change something. – ahmet2106 Jun 18 '13 at 03:08
  • Yes you are right, that's what I thought. :) But didn't that resolve the error? – zambrey Jun 18 '13 at 03:11
  • no, now it doesnt crash, but the debug is not printing anything.. maybe because its UP and DOwn on an tableView.. but how to fix that? – ahmet2106 Jun 18 '13 at 03:11
  • Oh yes! You did the same mistake that I had done. self.swipeGestureUpDown.direction = (UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown); This does not work. See this http://stackoverflow.com/questions/3319209/setting-direction-for-uiswipegesturerecognizer You need to add two separate swipes. With current code NSLog should work for right swipe I think. – zambrey Jun 18 '13 at 03:16
  • doesnt work either if i only add one recognizer (only up). i really think it has to do something with uitableview which is scrollable in UP an DOWN and so its not that easy to recognize own swipes? – ahmet2106 Jun 18 '13 at 03:20
  • yeah right swipe and left are working -.- – ahmet2106 Jun 18 '13 at 03:21
  • It seems you are right ahmet2106. Looked around a little, swipes are delivered to uitableview at the highest priority. In fact I feel that's how it should be because having two different actions for same gesture in a single view would be a weird UI design. – zambrey Jun 18 '13 at 04:05
  • @ahmet2106 Could you please try setting scrollEnabled property to NO? "When scrolling is disabled, the scroll view does not accept touch events; it forwards them up the responder chain." This design will work only if your content in table fits in single screen. Please let me know. – zambrey Jun 18 '13 at 05:10
  • Funny, with scroll enabled false it works ;) but thats not what i need. and I also not want to write my own scroller if swipe recognized. so ill try maybe something else – ahmet2106 Jun 18 '13 at 09:40

1 Answers1

0

Answer to the question came up in the comments. Just consolidating it here. There were a few issues.
First:

self.swipeGestureUpDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreen)];

@selector(swipedScreen) is missing : at end of swipedScreen which makes it unrecognizable as the definition of the function is - (void)swipedScreen:(UISwipeGestureRecognizer*)gesture

Second:

self.swipeGestureUpDown.direction = (UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown);

Having single gesture recognizer for two directions of swipe does not work. For details see this. You will need to have a dedicated gesture recognizer for each direction.

Third:

Most important of all was trying to add Up and Down direction swipes on UITableView which won't work as long as scrolling is enabled in UITableView as it has its own default actions to handle these swipes which prevents it from being handled manually.
But if you have very limited content in the table and don't need scrolling, you can set scrollEnabled to false which will make UITableView stop using the gestures and forward the gestures higher up the responder chain. Refer scrollEnabled description here. (UITableView inherits from UIScrollView.)

Community
  • 1
  • 1
zambrey
  • 1,452
  • 13
  • 21