2012-04-24 37 views
0

我想顯示一個自定義欄,出現在我的應用程序的每個屏幕上,並帶有可用的按鈕。我將initView方法中的CustomViewController添加到我的類中,除非在分析我的應用程序時發生潛在的內存泄漏,否則一切正常。CustomViewController在整個應用程序

當我釋放[customViewController版本]時,CustomViewController上的按鈕將不再起作用。在沒有內存泄漏的情況下實施此解決方案的正確方法是什麼?

#import "CustomViewController.h" 

@implementation CustomViewController 

- (IBAction)doSomething:(id)sender 
{ 
    // Perform an action 
} 

@end 

一個我創建CustomViewController的ViewController:

- (id)initWithNibName:(NSString *)nibNameOrNil 
       bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     CustomViewController *customViewController = [[CustomViewController alloc] initWithNibName:@"CustomViewController" bundle:nil]; 
     UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)]; 
     [bar addSubview:customViewController.view]; 
     [self.view addSubview:bar]; 
     [bar release]; 
    } 
} 

回答

0

正確的解決方案是創建一個容器視圖,並將我的自定義任務欄放在該視圖中。

-1

你說你的酒吧有按鈕?當它自己被釋放時它釋放這些按鈕嗎?檢查它的viewDidLoad函數。

+0

按鈕在界面生成器中連接,因此沒有 – Vikings 2012-04-24 18:31:06

0

你似乎正在執行這個錯誤的方式。你實際需要做的是創建CustomViewController並將你的工具欄添加到該視圖。應用程序中的每個其他視圖控制器都應該作爲CustomViewController的一個子類。

如果自定義導航欄是您使用此超類的唯一方法,那麼我會建議您直接在您的應用使用的UINavigationController上對該欄進行樣式設置。

+0

它不是自定義導航欄,而只是一個可以顯示元素的欄。根據特定的視圖控制器,該欄也會改變位置。 – Vikings 2012-04-24 23:16:09

+0

那麼一定要創建你的CustomViewController超類。將你的UIView * bar屬性添加到該接口並@synthesize它。從那裏,你的子類可以根據需要定位/修改「欄」。 – 2012-04-25 01:48:30