如何在没有导航控制器的情况下向导航栏添加栏按钮。

8
我是一个新手iOS开发者。我在我的iPad应用程序视图中创建了一个导航栏,但我不需要导航控制器,所以我只添加了导航栏。现在我想在导航栏中添加一个按钮。我尝试了很多次,但没有成功。能否仅添加带有按钮的导航栏?如果可以,请向我提供一些示例代码。
"我没有导航控制器或需要它。我只想在一个视图中添加导航栏。"
以下是我编写的用于在ViewDidLoad()中添加导航栏的代码。
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1026, 50)];
[navBar setTintColor:[UIColor blackColor]];
[navBar setDelegate:self];
[self.view addSubview:navBar];

感谢您的预先参考。
3个回答

12

我使用Interface Builder在UITableViewController中制作了一个静态的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];

我希望这能帮助某人!


5
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];

1

您可以如下将按钮添加到导航栏中:

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

2
在上面的代码中,这一行给我错误:navBar.rightBarButtonItem = btnSave; 错误信息:语义问题:在类型为“UINavigationBar *”的对象上未找到属性“rightBarButtonItem”。 - Bhavin_m
你可以在XIB文件中添加导航栏,然后向其中添加条形按钮项。 - Abdullah Md. Zubair

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接