0

I've been working on my book app, but I have a UIBarButtonItem dilemma. I have set the 3 bar buttons on the toolbar as you can see in the image below. They work fine until I set the single tap recognizer, the last 2 lines in the code. What I want to do is get out of this modal screen by a single tap, not a double tap, not by adding another 'Cancel' button on the toolbar. After I set the single tap recognizer, none of the bar button items work. How can I get out of this dilemma. Can any one tell me how to get around this? Thank you for your time.

UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 410, 320, 50);
toolbar.tintColor = _label.backgroundColor;

self.title = @"Test For Toolbar";
UIBarButtonItem* TofC = [[UIBarButtonItem alloc] initWithTitle:@"T of C"
                                                        style:UIBarButtonItemStyleBordered
                                                        target:self
                                                        action:@selector(showTofC)];


UIBarButtonItem* bookMark = 
    [[UIBarButtonItem alloc] initWithTitle:@"Book Mark"style:UIBarButtonItemStyleBordered
                                                      target:self
                                                      action:@selector(bookMarkIt)];


UIBarButtonItem* searchBtn = [[UIBarButtonItem alloc] initWithTitle:@"Search"
                                                      style:UIBarButtonItemStyleBordered
                                                      target:self
                                                      action:@selector(searchIt)];


UIBarButtonItem* spacer = [[UIBarButtonItem alloc]      
                  initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                       target:self
                                        action:nil];





NSArray* buttons = 
           [NSArray arrayWithObjects:TofC, spacer, bookMark, spacer, searchBtn, nil];


self.navigationController.toolbarHidden = NO;


[toolbar setItems:buttons animated:NO];
[self.view addSubview:toolbar];

/*
// Single Tap
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]
                                      initWithTarget:self
                                      action:@selector(handleTapGesture:)];
[self.view addGestureRecognizer:tapGesture];

*/ 

// Single Tap
Dum2ViewController* dum2ViewController = [[Dum2ViewController alloc] init];
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]
                                      initWithTarget:dum2ViewController
                                      action:@selector(handleTapGesture:)];
[dum2ViewController.view addGestureRecognizer:tapGesture];
boochan224
  • 371
  • 1
  • 5
  • 13

1 Answers1

0

You've added the toolbar as a subview of your main view and then attached a gesture recognizer to the main view.

So as soon as you touch the toolbar, the touch goes to the main view first and is intercepted the gesture recognizer before it can be passed down to the toolbar items.

What I'd recommend doing is setting a gesture recognizer on whatever content (another subview?) that is inside your current view. That way a touch in the toolbar will be routed to the toolbar and a touch within the view will go to the (content) subview and be caught by the gesture recognizer.

Makes sense?

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • hi, thanks for your advice. I did like you suggested but not luck. Please look at the code I added above. Thanks again. – boochan224 Aug 03 '13 at 10:30