簡體   English   中英

如何在沒有導航的情況下在導航欄中添加欄按鈕 controller。

[英]How to add Bar Button in navigation bar without navigation controller.

我是 iOS 開發的新手。我在我的 iPad 應用程序視圖中創建了一個導航欄。我不需要導航 controller 這就是我只添加導航欄的原因。 現在我想在那個導航欄中添加按鈕。我嘗試了很多但沒有成功。 是否可以僅添加帶按鈕的導航欄? 如果是,那么建議我一些示例代碼。

“我沒有導航 controller 也不需要它。我只想在一個視圖中添加導航欄。”

波紋管是我為在 ViewDidLoad() 中添加導航欄而編寫的代碼

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1026, 50)];
[navBar setTintColor:[UIColor blackColor]];
[navBar setDelegate:self];
[self.view addSubview:navBar];

提前致謝....

我使用 Interface Builder 在 UITableViewController 中制作了一個 static tableView。 此 UITableViewController 以模態方式顯示。 然后我添加了一個 NavigationBar,后面沒有 UINavigationController,如下所示:

//Creating the plain Navigation Bar
UINavigationBar *headerView = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

//The UINavigationItem is neede as a "box" that holds the Buttons or other elements
UINavigationItem *buttonCarrier = [[UINavigationItem alloc]initWithTitle:@"Sign-In"];

//Creating some buttons:
UIBarButtonItem *barBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Zurück" style:UIBarButtonItemStyleDone target:self action:@selector(signInBackPressed:)];
UIBarButtonItem *barDoneButton = [[UIBarButtonItem alloc] initWithTitle:@"Fertig" style:UIBarButtonItemStylePlain target:self action:@selector(signInDonePressed:)];

//Putting the Buttons on the Carrier
[buttonCarrier setLeftBarButtonItem:barBackButton];
[buttonCarrier setRightBarButtonItem:barDoneButton];

//The NavigationBar accepts those "Carrier" (UINavigationItem) inside an Array
NSArray *barItemArray = [[NSArray alloc]initWithObjects:buttonCarrier,nil];

// Attaching the Array to the NavigationBar
[headerView setItems:barItemArray];

// Adding the NavigationBar to the TableView 
[self.tableView setTableHeaderView:headerView];

我希望這對某人有幫助!

UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editButton)];

bi1.style = UIBarButtonItemStyleBordered;
bi1.tintColor = [UIColor colorWithWhite:0.305f alpha:0.0f];

self.navigationItem.rightBarButtonItem = bi1;

[bi1 release];

您可以將按鈕添加到導航欄,如下所示:

UIBarButtonItem *btnSave = [[UIBarButtonItem alloc] 
                                initWithTitle:@"Save"
                                style:UIBarButtonItemStyleBordered 
                                target:self 
                             action:@selector(save_Clicked:)];
 navBar.rightBarButtonItem = btnSave;
 [btnSave release];

 UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] 
                                initWithTitle:@"Cancel"                                    
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(cancel_Clicked:)];
 navBar.leftBarButtonItem = btnCancel;
 [btnCancel release];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM