1

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?

4

2 回答 2

0

在 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;
}

我希望能解决你的问题。

于 2011-04-05T16:07:49.133 回答
0

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

此代码不正确

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

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

而且 release 和 nil 操作在这里倒退

list = nil;
[list release];

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

你应该最终得到

-(IBAction)closeListClient {
    [list.view removeFromSuperview];
    [list release];
    list = nil;
}
于 2011-04-05T15:28:37.770 回答