0

我在导航栏的左侧创建了一个信息按钮。单击信息按钮后,如何允许用户导航到另一个名为“AboutViewController”的视图?请帮忙!!(以下是我的代码)

UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(viewWillAppear:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
[self.navigationItem setLeftBarButtonItem:modalButton animated:YES];
[modalButton release];
4

2 回答 2

0

我想告诉你的第一件事是@selector用另一个合适的名字改变你的方法,就像我给出的这种情况一样navigateToAboutView:。因为您在代码中给出的方法名称可能出现在您要调用的同一视图中!

现在在您的自定义方法中编写以下代码 -

- (void) navigateToAboutView:(id)sender
{
    AboutViewController *aboutViewCntrl =[[AboutViewController alloc]initWithNibName:@"AboutViewController" bundle:nil];
    [self.navigationController pushViewController:aboutViewCntrl animated:YES];
}
于 2012-08-13T10:07:41.983 回答
0

我不确定为什么在viewWillAppear:向按钮添加目标时作为选择器传递,但该选择器定义了在点击按钮时调用的方法。viewWillAppear:是一个在每次视图即将显示时调用的方法,所以当你的按钮被点击时调用它是没有意义的。因此,将其更改为类似openAnotherView并创建一个类似这样的方法:

- (void)openAnotherView {
    // Instantiate the view controller.
    AboutViewController *aboutViewController = [[AboutViewController alloc] init]; // or initWithNibName:bundle: if you use Interface Builder.

    // Present the view controller modally.
    [self presentModalViewController:aboutViewController animated:YES]
}

如果您想关闭 about 视图控制器,请使用以下行AboutViewController

[self dismissModalViewControllerAnimated:YES]
于 2012-08-13T10:08:28.590 回答