目标 C:第二次打开子视图的问题

发布于 2024-10-30 01:05:49 字数 410 浏览 1 评论 0原文

我有这段代码来打开一个子视图

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请叫√我孤独 2024-11-06 01:05:49

因为您的 list 不是 nil,所以它不会进入 if (list == nil) 内部。

将其更改为 if (list.superview == nil)

Because your list is not nil so it doesn't go inside if (list == nil).

Change it to if (list.superview == nil).

画▽骨i 2024-11-06 01:05:49

这是因为 if 语句。将其更改为:

if ( list == nil ) {
    list = [[ListClient alloc] initWithNibName:@"ListClient" bundle:nil];
    [list setDelegate:self];
}
[self.view addSubview:list.view];

就可以了。

PS。要修复此问题的内存管理,请向类添加保留属性list,这样您就可以

self.list = [[[ListClient alloc] initWithNibName:@"ListClient" bundle:nil] autorelease];

self.list = nil;< /code> 在使用完对象后释放该对象。 (例如,在解雇时或在您的dealloc方法中)

It's because of the if statement. Change it to:

if ( list == nil ) {
    list = [[ListClient alloc] initWithNibName:@"ListClient" bundle:nil];
    [list setDelegate:self];
}
[self.view addSubview:list.view];

And it will work.

PS. To fix the memory management of this, add a retained property list to the class so you can say

self.list = [[[ListClient alloc] initWithNibName:@"ListClient" bundle:nil] autorelease];

And you can say self.list = nil; to release the object when you're done with it. (eg while dismissing or in your dealloc method)

热血少△年 2024-11-06 01:05:49
-(IBAction) closeListClient{
[self.view removeFromSuperview];
if ( list != nil ) {
    [list release];
    list=nil;
}}
-(IBAction) closeListClient{
[self.view removeFromSuperview];
if ( list != nil ) {
    [list release];
    list=nil;
}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文