我项目的当前版本:
我的应用中有5个不同的
UIViewControllers
。我设定了我的FirstViewController
使用Initial View Controller
属性检查员。我从一个ViewController来回移动 另一个是通过使用我分配模态segue的按钮 ViewController到另一个,使用StoryBoard
我想要改变的内容:
我想明显保留导航按钮,删除模态segue并使用 改为
UINavigationController
。如果我理解这个概念 正确地说,当使用UINavigationController
时,我需要进入每个UIButton-IBAction
并且在方法的最后我必须推动下一个 ViewController我想移动到我的NavigationController上(我也是 必须首先弹出当前的一个?)。但是,我无法弄清楚如何 正确地实现所有这一切。
到目前为止我做了什么:
MyAppDelegate.m
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *myFirstViewController = [[FirstViewController alloc] init];
UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:myFirstViewController];
[myNavigationController pushViewController:myFirstViewController animated:YES];
// Override point for customization after application launch.
return YES;
}
FirstViewController.m
- (IBAction)goRightButton:(UIButton *)sender
{
// some code drawing the ButtonIsPressed UIImageView on the current View Controller
UIViewController *mySecondViewController = [[SecondViewController alloc] init];
[self.navigationController pushViewController:mySecondViewController animated:YES];
}
但没有任何反应。我做错了什么?
答案 0 :(得分:2)
您没有链接您的XIB文件。请将您的导航控制器添加为
UIViewController *myFirstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:myFirstViewController];
使用以下代码从一个视图移动到另一个视图
UIViewController *mySecondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:mySecondViewController animated:YES];
答案 1 :(得分:0)
如果您使用的是故事板,则只需将导航控制器拖入其中并将其连接到您的应用代表即可。只要它是主要故事板,并且您已确定首先加载视图控制器,您就不需要在应用程序委托中加载任何视图。
为了以编程方式推送故事板中的视图,您需要执行以下操作:
//bundle can be nil if in main bundle, which is default
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyCustomViewController *customVC = (MyCustomViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"customVC"];
//standard way
[self.navigationController pushViewController:customVC animated:YES];
//custom animation
[UIView transitionWithView:self.navigationController.view duration:0.5 options:UIViewAnimationOptionTransitionCurlUp animations:^{
[self.navigationController pushViewController:customVC animated:NO];
} completion:nil];
使用在storyboard编辑器中添加的标识符标识视图控制器。下面是一些帮助显示我的意思的截图。