2011-04-05 20 views
1

這是我的代碼有兩個IBAction爲開放,並在兩個不同的類目標C:打開和關閉子視圖

- (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]; 
} 

現在是OK的第一次,但如果我想用更多的時間我的名單,我必須在closeListClient寫

list = nil; 
[list release]; 

現在我的問題是這樣的「名單」聲明Ø NLY在類ListClient作爲

ListClient *list; 

,當我在寫CloseListClient名單是一個錯誤......我該怎麼辦?

回答

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

I希望能解決你的問題。

+0

謝謝你非常非常 – CrazyDev 2011-04-05 16:15:03

0

我想指出一些問題,可能會解決您的問題。關於你的問題,特別是開幕和閉幕式的表述,我有點失落。我相信你只是想隱藏並顯示基於哪個按鈕被按下的視圖。

此代碼是不正確

-(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; 
} 
+0

確定但我在兩個不同類中使用showListClient和closeListClient,然後當我在showListCient中使用「list」時沒問題,但是在closeListClient中,我無法使用它,因爲此方法在另一個類中,你明白嗎? – CrazyDev 2011-04-05 15:32:05