Objective C:打开和关闭子视图

发布于 2024-10-30 16:37:47 字数 629 浏览 1 评论 0原文

这是我的代码,有两个 IBAction 用于打开和关闭两个不同类中的子视图

- (IBAction) showListClient:(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];
}

是可以的,但是如果我想使用更多时间我的列表,我必须在 closeListClient 中编写

list = nil;
[list release];

现在我的问题是这个“列表”仅在 ListClient 类中声明,因为

ListClient *list;

当我在 CloseListClient 中写入列表时出现错误...我该怎么办?

This is my code with two IBAction for open and close a subview in two different classes

- (IBAction) showListClient:(id) sender {

if( list == nil){

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

}

and for close

-(IBAction)closeListClient {
[self.view removeFromSuperview];
}

now it's ok for the first time, but if I want use more time my list I must write in closeListClient

list = nil;
[list release];

now my problem is this "list" is declared only in the class ListClient as

ListClient *list;

and when I write list in CloseListClient is an error...what can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

惟欲睡 2024-11-06 16:37:47

在ListCLient.h中为其委托定义一个协议:

@protocol ListClientDelegate<NSObject>
@optional
- (void)listClientDidClose:(ListClient *)listClient;
@end

并修改delegate的属性定义:

@property (nonatomic, assign) id<ListClientDelegate> delegate;

然后在调用closeListClient操作时向委托发送消息(在ListClient.m中):

-(IBAction)closeListClient {
    [self.view removeFromSuperview];
    [self.delegate listClientDidClose:self]
}

然后最后在SomeController中.m 实现委托方法:

-(void)listClientDidClose:(ListClient *)listClient {
    [list release];
    list = nil;
}

我希望能解决您的问题。

In ListCLient.h define a protocol for its delegate:

@protocol ListClientDelegate<NSObject>
@optional
- (void)listClientDidClose:(ListClient *)listClient;
@end

and modify the property definition for delegate:

@property (nonatomic, assign) id<ListClientDelegate> delegate;

Then send a message to the delegate when the closeListClient action is called (in ListClient.m):

-(IBAction)closeListClient {
    [self.view removeFromSuperview];
    [self.delegate listClientDidClose:self]
}

Then finally in SomeController.m implement the delegate method:

-(void)listClientDidClose:(ListClient *)listClient {
    [list release];
    list = nil;
}

I hope that solves your proplem.

千寻… 2024-11-06 16:37:47

我想指出一些可能可以解决您的问题的问题。我对你的问题有点迷惑,尤其是打开和关闭视图的措辞。我确信您只想根据按下的按钮隐藏和显示视图。

这段代码是不正确的

-(IBAction)closeListClient {
    [self.view removeFromSuperview];
}

//I am sure you want to remove the list view
-(IBAction)closeListClient {
   [list.view removeFromSuperview];
}

并且释放和零操作在这里向后

list = nil;
[list release];

//Change to
[list release];
list = nil;

你应该结束

-(IBAction)closeListClient {
    [list.view removeFromSuperview];
    [list release];
    list = nil;
}

I would like to point out a few issues that may be fix your problem. I am a little lost about your question especially the wording of Opening and Closing a view. I am sure you just want to hide and show a view based on which button is pressed.

This code is incorrect

-(IBAction)closeListClient {
    [self.view removeFromSuperview];
}

//I am sure you want to remove the list view
-(IBAction)closeListClient {
   [list.view removeFromSuperview];
}

And the release and nil operations are backwards here

list = nil;
[list release];

//Change to
[list release];
list = nil;

You should end up with

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