0

The following code I have for a shake gesture works perfectly fine, it just when I have it across three separate uitableviews and once you leave one tableview to go to the next one, the shake gesture no longer works. Any idea how to make it work across all three viewcontrollers? Plus I have the option to tweet out information on the "detailviewcontroller" however, once you leave one of the table views it disables the keyboard for twitter. Any ideas to how to fix this? Thanks!

//BEGIN SHAKE GESTURE CODE
- (void)viewDidAppear:(BOOL)animated {

    [self becomeFirstResponder];
}

- (void)viewDidDisappear:(BOOL)animated {

    [self resignFirstResponder];

}


- (BOOL)canBecomeFirstResponder {
    return YES;

}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    if (event.subtype == UIEventSubtypeMotionShake) {

        int section = 0;
        int row = arc4random() %36;
        NSIndexPath * path = [NSIndexPath indexPathForRow:row inSection:section];
        [self handleSelectedRow:path.row];

        [self.tableView selectRowAtIndexPath:path animated:YES scrollPosition: UITableViewScrollPositionNone];


    }
}

-(void)handleSelectedRow:(int)row;
{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.tag = row;
    [self performSegueWithIdentifier:@"showRecipeDetail" sender:btn];
}

//END SHAKE GESTURE CODE

1 Answers1

0

Why not make a base class view controller (derived from UIViewController) and put your shake motion detection ("motionEnded:withEvent:") function into that, and then subclass your three various view controllers off your Shake-Motion-code-included view controller class?

In other words, put your shake detection code into a class that looks like:

@interface HenryViewController : UIViewController

and then in your three various view controllers, you can declare them like this:

@interface FirstViewController : HenryViewController

@interface SecondViewController : HenryViewController

As for not being able to Tweet out information, the keyboard is likely disappearing because you're calling "resignFirstResponder" on your DetailViewController as it disappears.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215