objective-c - 在这种情况下如何实现 UINavigationController?

标签 objective-c ios uiviewcontroller uinavigationcontroller

我的项目的当前版本:

I have 5 different UIViewControllers in my app. I've set my FirstViewController to be the Initial 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 a UINavigationController I need to go into each UIButton-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/

相关文章:

ios - 球体上两点之间的圆柱方向,Scenekit,四元数 IOS

objective-c - 有人可以向我解释 (NSString *) 对 Obj-C 意味着什么吗?

ios - Storyboard:如何在更改布局约束时让 Xcode 自动更新框架?

ios - 如何在 Cocoa Touch 中任意两个 ViewController 之间转换?

ios - 如何使用 UITextview 增加 View 大小?

ios - XIB 和 NIB 有什么区别?

ios - 为 ipad 创建垂直选项卡栏

ios - 在 UITabBarController 中的不同选项卡之间共享公共(public) View

iPhone 使用附加数据初始化 UIViewController

iOS:在应用程序启动时播放视频