Как удалить вложенный вид Когда нажимает кнопку UIBar в iPhone? - PullRequest
0 голосов
/ 26 октября 2010

Я занимаюсь разработкой приложения для iPhone. Я создал вид UIImage, щелкает по изображению, он перемещается по одному пользовательскому виду и загружает веб-представление. В этом представлении у меня была одна панель инструментов и одна панель кнопок на панели инструментов. Когда нажимает кнопку панели, он удаляет пользовательский вид. Моя проблема в том, что пользовательский вид не удаляется должным образом. Я не знаю, почему это не удалено, потому что методы вызывают правильно. Но «removeFromSuperview» не работает несколько раз. Я думаю, что экземпляры пользовательского представления создаются несколько раз, и как я могу избежать этого? Пожалуйста, помогите мне!

Вот мой код,

- (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?

     }

Пожалуйста, помогите мне.

Спасибо!

Ответы [ 2 ]

3 голосов
/ 26 октября 2010

Используйте тег для идентификации вида и удаления из superView.

Добавить следующую строку после,

  custView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 432)] autorelease];    
  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];
2 голосов
/ 26 октября 2010

может быть, вы добавите пользовательский вид более одного раза?добавить это:

[custView removeFromSuperview]; 

до

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