导航信息按钮到另一个屏幕Xcode

问题描述:

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

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

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

现在写了下面的代码在你自定义的方法 -

- (void) navigateToAboutView:(id)sender 
{ 
    AboutViewController *aboutViewCntrl =[[AboutViewController alloc]initWithNibName:@"AboutViewController" bundle:nil]; 
    [self.navigationController pushViewController:aboutViewCntrl animated:YES]; 
} 

我不知道为什么你已经通过viewWillAppear:作为选择,当你添加一个目标按钮,而是选择定义哪种方法当按钮被点击时调用。 viewWillAppear:是每次视图即将显示时调用的方法,因此在轻按按钮时调用它是没有意义的。因此,它更改为类似openAnotherView,创建这样一个方法:如果你想解雇有关视图控制器,然后使用此行AboutViewController

- (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] 
} 

[self dismissModalViewControllerAnimated:YES]