如何将信息图标添加到导航栏并实现showInfoView方法?

发布于 2024-10-15 10:08:26 字数 1656 浏览 4 评论 0原文

我正在尝试在导航栏顶部添加一个信息按钮。信息按钮将用户引导至信息视图。以下代码将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许仙没带伞 2024-10-22 10:08:26

使用此代码:

UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
[infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = infoButton;

如果 nib 文件名是“InfoView.xib”而不是“InfoViewController.xib”,则 showInfoView: 方法应该可以正常工作

Use this code:

UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
[infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = infoButton;

The showInfoView: method should work fine provided that the nib file name is "InfoView.xib" not "InfoViewController.xib"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文