Ios 在单个应用程序中添加并导航到新视图

Ios 在单个应用程序中添加并导航到新视图,ios,uinavigationcontroller,Ios,Uinavigationcontroller,此问题可能已得到回答,如果是,请共享链接 我已经创建了一个单视图应用程序,它运行良好,但现在我添加了一个新视图,单击一个按钮,希望新视图出现 这是单击操作的代码 SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:[NSBundle mainBundle]]; [self.

此问题可能已得到回答,如果是,请共享链接

我已经创建了一个单视图应用程序,它运行良好,但现在我添加了一个新视图,单击一个按钮,希望新视图出现

这是单击操作的代码

    SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:[NSBundle mainBundle]];

    [self.navigationController pushViewController:settingsViewController animated:YES];
默认的ViewController现在在.h文件中看起来像这样

    @interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
我可以像这样向“单视图应用程序”添加另一个视图吗?还是应该为我的项目选择另一个模板


谢谢。

那很好。单视图应用程序模板只是一个基本模板。您可以向其中添加任何类型的导航。

您需要在
AppDelegate
中创建
UINavigationController
。然后使您的
ViewController
成为
UINavigationController
rootViewController
。然后,您将能够推送和弹出视图

以下是创建
rootViewController
的代码,其中
mainNavigationController
AppDelegate中的
UINavigationController

ViewController *vc = [[ViewController alloc] init];
mainNavigationController = [[UINavigationController alloc] initWithRootViewController:vc];

一旦您将
ViewController
设置为
rootViewController
,它将符合
UINavigationController
push-and-pop方法来创建一个
UIViewController
s的堆栈。

在iOS 5中,在视图之间切换的工作方式会有所不同

我已经用上面提到的代码创建了一些应用程序来切换视图

但是现在,我必须这样写才能工作:

    SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:[NSBundle mainBundle]];
    [self presentModalViewController:settingsViewController animated:YES];

您是使用情节串连板或界面生成器构建视图,还是以编程方式完成的?我创建了一个没有情节串连板的新项目,并使用界面生成器设计视图。我建议您从一个空的应用程序开始,然后从那里开始。对于上面的代码,您是否看到任何错误,因为我无法让它在点击对接时切换到settingView。您答案中的代码显示了一个ModalViewController。问题中的代码将ViewController推送到NavigationController堆栈上。如果设置正确,这两种方法都可以工作。然后我可能在代码中出错,我会再试一次。是的,你是对的,现在这两种方法都可以工作,这有助于我解决问题
    SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:[NSBundle mainBundle]];
    [self presentModalViewController:settingsViewController animated:YES];