如何将信息图标添加到导航栏并实现showInfoView方法?
我正在尝试在导航栏顶部添加一个信息按钮。信息按钮将用户引导至信息视图。以下代码将 iPhone 默认信息按钮添加到导航栏。它被放置在 RootViewController.m 的 viewDidLoad 函数中。
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
现在需要实现showInfoView方法,使用如下代码:
- (IBAction)showInfoView:(id)sender {
// Create the root view controller for the navigation controller
// The new view controller configures a Cancel and Done button for the
// navigation bar.
InfoViewController *addController = [[InfoViewController alloc]
initWithNibName:@"InfoView" bundle:nil];
// Configure the RecipeAddViewController. In this case, it reports any
// changes to a custom delegate object.
addController.delegate = self;
// Create the navigation controller and present it modally.
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:addController];
[self presentModalViewController:navigationController animated:YES];
// The navigation controller is now owned by the current view controller
// and the root view controller is owned by the navigation controller,
// so both objects should be released to prevent over-retention.
[navigationController release];
[addController release];
}
但是当程序构建并运行时,点击info按钮会导致程序崩溃。
以前有人遇到过这个问题吗?我进行了广泛的搜索,没有发现任何人在实现 showInfoView 方法时遇到问题。
I am attempting to add an info button to the top of the navigation bar. The info button directs the user to the information view. The following code adds the iPhone default info button to the navigation bar. It is placed inside RootViewController.m 's viewDidLoad function.
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
Now, the showInfoView method needs to be implemented, and the following code is used:
- (IBAction)showInfoView:(id)sender {
// Create the root view controller for the navigation controller
// The new view controller configures a Cancel and Done button for the
// navigation bar.
InfoViewController *addController = [[InfoViewController alloc]
initWithNibName:@"InfoView" bundle:nil];
// Configure the RecipeAddViewController. In this case, it reports any
// changes to a custom delegate object.
addController.delegate = self;
// Create the navigation controller and present it modally.
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:addController];
[self presentModalViewController:navigationController animated:YES];
// The navigation controller is now owned by the current view controller
// and the root view controller is owned by the navigation controller,
// so both objects should be released to prevent over-retention.
[navigationController release];
[addController release];
}
But when the program builds and runs, clicking the info button results in the program crashing.
Did anyone come across this problem before? I have searched extensively and I haven't seen any body with trouble implementing the showInfoView method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用此代码:
如果 nib 文件名是“InfoView.xib”而不是“InfoViewController.xib”,则
showInfoView:
方法应该可以正常工作Use this code:
The
showInfoView:
method should work fine provided that the nib file name is "InfoView.xib" not "InfoViewController.xib"