How to handle UITapGestureRecognizer in a subview? - iphone

I have a viewcontroller (vc1) in which I added another viewcontroller (vc2) as a subview. An UITapGestureRecognizer is triggered when you tap on a view (tappingView) contained in the subview (vc2). I would like to handle this tap inside the subview controller (vc2), and not from the first viewcontroller (vc1)... but it keeps crashing when I click on my view.
Here is the hierarchy sum up :
-vc1
--vc2
---tappingView
And here is a sample of code :
ViewController1 (viewDidLoad):
- (void)viewDidLoad
{
ViewController2 *vc2 = [[ViewController2 alloc] init];
[[self view] addSubview:[vc2 view]];
}
ViewController2 (viewDidLoad):
- (void)viewDidLoad
{
UIView *tappingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60.0, 80.0)];
[[self view] addSubview:tappingView];
UITapGestureRecognizer *tapGestureRecognize = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureRecognizer:)];
[tappingView addGestureRecognizer:tapGestureRecognize];
}
ViewController2 (singleTapeGestureRecognizer:) :
- (void)singleTapGestureRecognizer:(id)sender {
NSLog(#"Tap gesture");
}
When I click in my view (tappingView), I keep getting a crash (BAD_ACCESS).
Anyone have an idea why ?

You shouldn't just add another view controller's view to your view -- you should make that controller a childViewController of vc1. Change the viewDidLoad in vc1 to this:
- (void)viewDidLoad {
ViewController2 *vc2 = [[ViewController2 alloc] init];
[self addChildViewController:vc2];
[vc2 didMoveToParentViewController:self];
[[self view] addSubview:[vc2 view]];
}
It's not clear why you're doing it this way in the first place. Why not present vc2 as a modal view controller instead?

In this block
- (void)viewDidLoad
{
ViewController2 *vc2 = [[ViewController2 alloc] init];
[[self view] addSubview:[vc2 view]];
}
your ViewController2 is not retained (addSubview only retains the view, not the view controller).
To fix it, declare vc2 as a strong property, instead of a variable.
self.vc2 = [[ViewController2 alloc] init];
Or better, as someone suggested, add vc2 as a child view controller.

Targets of UITapGestureRecognizer take the UIGestureRecognizer argument, not id. I.e.:
- (void)singleTapGestureRecognizer:(UIGestureRecognizer *)sender {
NSLog(#"Tap gesture");
}
Further, you're setting frames in viewDidLoad, which you should do in viewWillAppear: instead.

use below code
- (void)singleTapGestureRecognizer:(UIGestureRecognizer *)sender
instead of
- (void)singleTapGestureRecognizer:(id)sender.
As it must have UIGestureREcognizer instead of id.`

Related

Unexpected UIView offset in UIViewController hierarchy with container controllers and UINavigationController

I have a UINavigationController with a number of child view controllers, some of which are container controllers. The UIView frame layout of the last added child UIViewController (green background) has its parent background (yellow) through when it should not.
How can I layout everything so all of the views fit within the bounds of the screen below the Navigation bar without the weird offset?
UIViewController hierarchy, started in my AppDelegate:
UINavigationController rootViewController:FrontFacadeViewController < UIViewController
view (0,0) dim:(320x460)
=> StandardCoverViewController < UIViewController
added as addSubview of FrontFacadeViewController's view]
view: (0,0) dim:(320x460)
=> ConfigurationViewController < UIViewController
added by pushing onto self.navigationController
view: (0,20) (320x460)
=> GearViewController < UIViewController
added as subview of ConfigurationViewController's view
view: (0,0) (320x416)
Here's how all of the view controllers are created:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
FrontFacadeViewController *frontController = [[FrontFacadeViewController alloc]
initWithBundle:[NSBundle mainBundle]];
_appNavigationController = [[UINavigationController alloc]
initWithRootViewController:frontController];
}
FrontFacadeViewController:
// FrontFacadeViewController determines a particular view controller to present
// based on some business logic.
// Because of this, it adds child UIViewControllers using container controllers.
//
- (void)viewDidLoad {
StandardCoverViewController *controller = [StandardCoverViewController alloc] initWithNibName:nil bundle:self.nibBundle];
[self addChildViewController:controller];
[controller didMoveToParentViewController:self];
[self.view addSubview:controller.view];
[controller didMoveToParentViewController:self];
}
- (IBAction)onButtonPress {
ConfigurationViewController *controller = [[ConfigurationViewController alloc] initWithNibName:nil bundle:self.nibBundle];
[self.navigationController pushViewController:controller animated:YES];
}
ConfigurationViewController: (yellow background)
- (void)viewDidLoad {
[self.view setBackgroundColor:[UIColor yellowColor]];
GearViewController *controller = [[GearViewController alloc] initWithNibName:nil bundle:self.nibBundle];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
[controller didMoveToParentViewController:self];
}
GearViewController: (green background)
- (void)viewDidLoad {
[self.view setBackgroundColor:[UIColor greenColor]];
// nothing special
}
Am I doing this all wrong?

Accessing TabBarController on Button Press

I have programatically created a TabBarController with views etc. Now i want to show this TabBarController on Button Press. How do i do that? Currently i am presenting it modally but it doesn't work - throws sigtrap errors.
This is my code for the TabBarController
#implementation TabBarViewController
- (void) loadView
{
HomeViewController * homeViewController = [[HomeViewController alloc]initWithNibName:#"HomeViewController" bundle:nil];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
// Set each tab to show an appropriate view controller
[tabBarController setViewControllers:[NSArray arrayWithObjects:homeViewController, homeViewController, nil]];
[self.view addSubview:tabBarController.view];
[homeViewController release];
[tabBarController release];
}
This is my code for accessing this tabBarController from a Button Press event from my mainViewController -
- (IBAction)quickBrowse:(UIButton *)sender
{
TabBarViewController * tabBarController = [[TabBarViewController alloc]init];
[self presentModalViewController:tabBarController animated:YES];
[tabBarController release];
}
You should only override the method loadView if you are not using IB and if you want to create yours view manually. And when you do that you must assign your root view to the view property of UIViewController.
I believe in your case you don't need to override this method, you can use the viewDidLoad method to create your UITabBarController and store it in a variable, so when the event gets called all you need to do is pass the variable to the method presentModalViewController:animated:
Your final code would look like this:
- (void) viewDidLoad
{
[super viewDidLoad];
HomeViewController * homeViewController = [[HomeViewController alloc]initWithNibName:#"HomeViewController" bundle:nil];
// you can't pass the same view controller to more than one position in UITabBarController
HomeViewController * homeViewController2 = [[HomeViewController alloc]initWithNibName:#"HomeViewController" bundle:nil];
// local variable
self.modalTabBarController = [[UITabBarController alloc] init];
// Set each tab to show an appropriate view controller
[self.modalTabBarController setViewControllers:[NSArray arrayWithObjects:homeViewController, homeViewController2, nil]];
}
- (void)viewDidUnload
{
self.modalTabBarController = nil;
[super viewDidUnload];
}
- (IBAction)quickBrowse:(UIButton *)sender
{
[self presentModalViewController:self.modalTabBarController animated:YES];
}

Adding UINavigationController to View Other Than Main View

I'd like to add a UINavigationController to my app info view (NOT to my main view). I've watched/read a number of tutorials showing how to add it to the main window through the AppDelegate using IB. In my case, I only want it to appear when a user presses the info button and is brought to the infoView. Here is how I switch to the infoView within my MainViewController:
- (IBAction)infoButtonPress:(id)sender
{
// Create pointer to instance of InfoViewController
InfoViewController *infoView = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
// Add view switching animation
infoView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
// Change view using animation
[self presentModalViewController:infoView animated:YES];
}
At this point the infoView is displayed and I would like THIS to be the RootView of the UINavigationController. I have tried adding the line:
UINavigationController *infoNavController = [[UINavigationController alloc]
initWithRootViewController:infoView];
after creating an instance of InfoViewController, but the app crashes. Is it possible to add UINavigationController to views other than the main view?
Thanks.
You are very close with your implementation. Try it in this order.
InfoViewController *infoView = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
UINavigationController *infoNavController = [[UINavigationController alloc]
initWithRootViewController:infoView];
[infoView release]; // skip this if using ARC
infoView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:infoNavController animated:YES];
UPDATE
To kill this modal, you will have to add your buttons to the main modal view.
InfoViewController.m
-(void)cancel:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
-(void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *cancelButton =
[[UIBarButtonItem alloc] initWithTitle: #"Cancel"
style: UIBarButtonItemStylePlain
target: self
action: #selector(cancel:)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
}

dismissModalView doesn't respond to touches

in MyAppDelegate, when a button is pressed it presents a modal view:
- (void) showInfoPanel:(id)sender {
infoViewController = [[InfoViewController alloc] init];
UINavigationController *infoNavController = [[UINavigationController alloc] init];
infoNavController.navigationBarHidden = YES;
[window addSubview:infoNavController.view];
[infoNavController presentModalViewController:infoViewController animated:YES];
}
in InfoViewController i have to dismiss the modal view:
- (void) exitInfoPanel:(id)sender {
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
although it works, the MyAppDelegate window doesn't respond to touches anymore.
instead in the InfoViewController if:
[self.view removeFromSuperview];
It respond to touches but I lose the animation of dismissing the modal view.
What I am doing wrong, why it doesn't respond to touches when the modalview is dismissed? ?
thanks

Implement Back action into UITabBarController

I have a controllerView (MenuControllerView) with a button inside, when I click on the button a new ViewController will appear with a TabBarController created programmatically like this:
UIView* topView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
tabBarController = [[UITabBarController alloc] init];
viewController1 = [[ViewController1 alloc] init];
viewController2 = [[ViewController2 alloc] init];
viewController3 = [[ViewController3 alloc] init];
viewController4 = [[ViewController4 alloc] init];
tabBarController,viewControllers = [NSArray arrayWithObjects:viewController1 , viewController2 , viewController3 ,viewController4, nil];
[[self tabBarController] setSelectedIndex:1];
[topView addSubView:[tabBarController view]];
Instead of displaying ViewController1 for the first button Item, I want to put an action Back in it to return to my MenuViewController, but I don't know how how to do it.
Thanks
Have you considered presenting the UITabBarController as a modal view controller and implementing UITabBarControllerDelegate? e.g. this seems to work for me (I make the third tab return to MenuViewController here):
#interface MenuViewController : UIViewController <UITabBarControllerDelegate>
...
- (IBAction) onButtonPressed:(id)sender
{
UITabBarController* tabBarController = [[UITabBarController alloc] init];
viewController1 = [[ViewController1 alloc] init];
viewController2 = [[ViewController2 alloc] init];
viewController3 = [[ViewController3 alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1 , viewController2 , viewController3 , nil];
[[self tabBarController] setSelectedIndex:1];
tabBarController.delegate = self;
[self presentModalViewController:tabBarController animated:NO];
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
{
if (viewController == viewController3)
{
[self dismissModalViewControllerAnimated:NO];
return NO;
}
return YES;
}
I doubt that this approach is a good one. You'll gonna break typical iPhone behaviour which will confuse users. The TabBarController is designed (functionally and technically) to change between views while a NavigationController is for pushing and popping views (go forth and back). Of course you can combine those (which is not always easy), but you shouldn't use TabBar as NavigationBar.
if I understand right, you can just remove your tabbar's view from superview. smth like
[[tabBarController view] removeFromSuperview];
if you just want to handle selection of tabbar item, you can use tabBar:didSelectItem: method of th UITabBarDelegate protocol.
Is this what you're trying to do?
This automatically created with a UINavigationController upon pushing to a child view controller.
[self.navigationController pushViewController:yourChildViewController animated:YES];