[英]Button not show in the navigation bar
我嘗試將此代碼用於導航欄
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0,320, 70)];
對於按鈕,我嘗試了這段代碼
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(SaveButtonClicked:)];
self.navigationItem.rightBarButtonItem=add;
在這段代碼之后,我還要聲明SaveButtonClicked方法。
當我嘗試此代碼時,此代碼中沒有錯誤並成功編譯,但是該按鈕未顯示在導航欄中。
為我提供正確的代碼和建議。
1)您不需要創建UINavigationBar。 您所在的導航控制器將提供該功能。
2)如果您確實認為自己需要一個,那可能意味着您的視圖根本不在導航控制器中。 檢查self.navigationItem
的值是什么。 我想您會發現它為零。 這意味着您需要重做界面,以便該視圖實際上位於導航控制器中。 Interface Builder中“編輯器”下的“嵌入導航控制器中”可能對您有幫助。
創建導航欄之后
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0,320, 70)];
然后,您需要創建導航項。 並將導航項添加到導航欄
UINavigationItem *item = [[UINavigationItem alloc]init];
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(SaveButtonClicked:)];
item.rightBarButtonItem=add;
navbar.items = [NSArray arrayWithObjects:item, nil];
[self.view addSubview:navbar];
希望這可以幫助!
您可以使用以下一行代碼來實現:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveButtonClicked:)];
在此之后添加此方法
-(void)saveButtonClicked:(UIBarButtonItem *)sender{
}
有關更多信息,您可以通過編程方式將“ 添加”按鈕添加到導航欄
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.