Ramesh
Ramesh

Reputation: 177

How to Hide a Child View From Parent View when touch outside of Child View in iOS?

i am newbie in iOS i make an app that contain Two Views i want to make when Button Was Pressed then Popup Child View so for this i write a cod e like as

-(IBAction)mapButtonPressed:(id)sender
 {
self.mapTypeVIew.hidden=FALSE;
 }

and in my viewDidLoad method i write as

[self.mkMapView addSubview:self.mapTypeVIew];
self.mapTypeVIew.hidden=TRUE;

so it was hide on viewDidLoad method and only shown when button pressed but here i want when button was pressed and subView is also Shown but i want if user touch outside of subview then subview is also hide for that i write a cod like as

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  {
NSLog(@"touches began");
UITouch *touch = [touches anyObject];
if(![touch.view isKindOfClass:[self.mapTypeVIew class]])
{
    self.mapTypeVIew.hidden=TRUE;
}
}

But it is Not hidden please give me Solution for when user touch outside of Child view then it was hide.

thanks.

Upvotes: 0

Views: 1107

Answers (2)

DilumN
DilumN

Reputation: 2897

It's not the efficient way to set a UIView hidden. You can Add SubView & RemoveFromSuperView is the optimum way.

Add it to your Super View

[self.mkMapView addSubview:self.mapTypeVIew];

To remove it from SuperView

[self.mapTypeVIew removeFromSuperview];

Upvotes: 0

Saif
Saif

Reputation: 2958

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  {
   NSLog(@"touches began");
   UITouch *touch = [touches anyObject];
   if(![touch.view isKindOfClass:[self.mapTypeVIew class]])
   {
      self.mapTypeVIew.hidden = YES;
      self.mapTypeVIew.alpha = 0.0;
   }
}

and while showing ,

-(IBAction)mapButtonPressed:(id)sender
 {
    self.mapTypeVIew.hidden = NO;
    self.mapTypeVIew.alpha = 1.0;
 }

Upvotes: 1

Related Questions