我正在开发iPhone应用程序。我创建了UIImage视图,单击图像视图,导航一个自定义视图并加载Web视图。在该视图中,我在工具栏中有一个工具栏和一个条形按钮。单击栏按钮时,将删除自定义视图。我的问题是有时自定义视图没有正确删除。我不知道为什么它没有删除,因为方法调用正确。但是“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?
}
请帮助我。
谢谢!
答案 0 :(得分:3)
使用标记识别视图并从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];
答案 1 :(得分:2)
也许你不止一次添加自定义视图?加上这个:
[custView removeFromSuperview];
之前
custView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 432)] autorelease];