我以编程方式创建了一些 UINavigationController 并将它们添加到 UITabBarController。一切似乎都正常,但我想向导航 Controller 添加一个取消按钮,但它从未出现过。我尝试了多种方法,但似乎根本无法影响导航项的显示,并且我已经遵循了此处和其他站点的多个示例,但没有任何反应。
MyTableViewController *mtvc = [[MyTableViewController alloc] init];
UINavigationController *myNavController = [[[UINavigationController alloc] initWithRootViewController:mtvc] autorelease];
myNavController.navigationBar.barStyle = UIBarStyleBlackOpaque; // this works
[mtvc release];
// TODO: figure out why added buttons aren't showing
UIBarButtonItem *closeButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(shutDown)] autorelease];
myNavController.navigationItem.leftBarButtonItem = closeButton; // never shows up
我也试过用这种方式添加按钮
[myNavController.navigationItem setLeftBarButtonItem:closeButton animated:NO]; // also doesn't do anything
我开始感到沮丧,所以我也尝试了一些其他的东西,看看我是否能影响任何东西,但无济于事
myNavController.title = @"test"; // does nothing
在将导航 Controller 添加到 UITabBarController 之前和之后,我都尝试过这样做,但没有帮助。 我也试过 rightBarButtonItem 并尝试使用 initWithTitle: 而不是 initWithBarButtonSystemItem。
有人可以照亮我吗?显然,我这样做是错误的方式。
最佳答案
尝试在 MyTableViewController
的 loadView
方法中添加条形按钮,如下所示。
UIBarButtonItem *closeButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(shutDown)] autorelease];
self.navigationItem.leftBarButtonItem = closeButton;
我想这应该可行。
关于iphone - 尝试以编程方式向 UINavigationController 添加按钮,但它从未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5062088/