目标 C:第二次打开子视图的问题
我有这段代码来打开一个子视图
- (IBAction) showList:(id) sender {
if( list == nil){
list = [[ListClient alloc] initWithNibName:@"ListClient" bundle:nil];
[list setDelegate:self];
[self.view addSubview:list.view];
}
}
,这段代码用来关闭这个子视图
-(IBAction) closeListClient {
[self.view removeFromSuperview];
}
第一次可以,但是第二次当我想打开子视图时,它不起作用,为什么?
I have this code to open a subview
- (IBAction) showList:(id) sender {
if( list == nil){
list = [[ListClient alloc] initWithNibName:@"ListClient" bundle:nil];
[list setDelegate:self];
[self.view addSubview:list.view];
}
}
and this code to close this subview
-(IBAction) closeListClient {
[self.view removeFromSuperview];
}
it's ok for the first time, but at second time when i want to open the subview, it don't work, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为您的
list
不是nil
,所以它不会进入if (list == nil)
内部。将其更改为
if (list.superview == nil)
。Because your
list
is notnil
so it doesn't go insideif (list == nil)
.Change it to
if (list.superview == nil)
.这是因为
if
语句。将其更改为:就可以了。
PS。要修复此问题的内存管理,请向类添加保留属性
list
,这样您就可以说
self.list = nil;< /code> 在使用完对象后释放该对象。 (例如,在解雇时或在您的dealloc方法中)
It's because of the
if
statement. Change it to:And it will work.
PS. To fix the memory management of this, add a retained property
list
to the class so you can sayAnd you can say
self.list = nil;
to release the object when you're done with it. (eg while dismissing or in yourdealloc
method)