views:

24

answers:

2

Hi Frieds,

I am developing on iPhone application. I have created UIImage view, on clicks the image view, it navigates one custom view and loading a web view. In that view, i had one tool bar and one bar buttons in the tool bar. When clicks the bar button, it removes the custom view. My problem is some time the custom view doesn't removed properly. I don't know why its not removed because the methods are calling properly. But "removeFromSuperview" is not working some times. I think the Custom view instances are creating multiple times and how can i avoid to that? Please help me out!

Here my code is,

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  (Touch Event for Image view)
{
     [super touchesBegan:touches withEvent:event];

   _imgView.userInteractionEnabled = YES;

     UITouch *touch = [[event allTouches] anyObject];

    if([touch view] == _imgView)
     {
        [self customWebview];

     }
}

    -(void) customWebview   
   {
      // Creating custom view and loading a web view  

         custView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 432)] autorelease];

        web.frame = CGRectMake(0, 0, 320, 432);

         web = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 432)] autorelease];

         web.delegate = self;

         web.scalesPageToFit =YES;

         [self.view addSubview:custView];

         [custView addSubview:web];

         UIToolbar *tool = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 432, 320, 48)] autorelease];

         tool.barStyle = UIBarStyleBlackOpaque; 

         UIBarButtonItem *closeBtn =[[[UIBarButtonItem alloc] initWi thBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(action)] autorelease];

         tool.items = [NSArray arrayWithObjects:space,closeBtn,nil];

         NSURL *url = [NSURL URLWithString:WebUrlString];

         NSURLRequest *req = [NSURLRequest requestWithURL:url];

         [web loadRequest:req];
     }

     -(void) action  
     {
         [custView removeFromSuperview];

       // This method is called properly, but some times the custom view is not removed, so how can i solve this issue?

     }

PLease Help me out.

Thanks!

+1  A: 

maybe you add the custom view more than once? add this:

[custView removeFromSuperview]; 

before

 custView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 432)] autorelease];
Alin
+1  A: 

Use Tag to Identify the view and remove from superView.

Add following line after custView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 432)] autorelease];//Your Code custView.tag = 10; // Any Number

For removing View UIView *tmpView = (UIView *)[self.view viewWithTag:10]; //ViewWithTag Number should be same as used while allocating [tmpView removeFromSuperview];

KiranThorat