我的项目的当前版本:
I have 5 different
UIViewControllers
in my app. I've set myFirstViewController
to be theInitial View Controller
using the Attributes Inspector. I move back and forth from one ViewController to another by using buttons to which I assign modal segues, from one ViewController to another, using the StoryBoard
我想要改变的内容:
I want to keep the navigation buttons obviously, delete the modal segues and use a
UINavigationController
instead. If I understand the concept correctly, when using aUINavigationController
I need to go into eachUIButton-IBAction
and at the very end of the method I have to push the next ViewController I want to move to, onto my NavigationController (do I also have to pop the current one first?). However, I can't figure out how to implement all that correctly.
到目前为止我所做的事情:
- 我从 Storyboard中删除了所有模态转场,并保留了导航按钮及其相应的 IBActions
- 我取消选中了属性检查器中的复选框,该复选框使我的 FirstViewController 成为我的应用程序的初始 View Controller
- 我进入我的 AppDelegate.m 并尝试在那里创建导航 Controller 并使我的 FirstViewController 成为 RootViewController
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;
}
- 然后我尝试通过进入 IBAction 来测试上述内容是否有效 我的 FirstViewController 上的导航按钮并实现了 以下以便移动到我的 SecondViewController 时 按钮被按下:
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];
}
但什么也没发生。我做错了什么?
最佳答案
您没有链接您的 XIB 文件。请将您的导航 Controller 添加为
UIViewController *myFirstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:myFirstViewController];
使用以下代码从一个 View 移动到另一个 View
UIViewController *mySecondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:mySecondViewController animated:YES];
关于objective-c - 在这种情况下如何实现 UINavigationController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12281392/