【问题标题】:How do I implement a UINavigationController in this case?在这种情况下如何实现 UINavigationController?
【发布时间】:2012-09-05 12:25:55
【问题描述】:

我的项目的当前版本:

我的应用中有 5 个不同的 UIViewControllers。我已经设置了我的 FirstViewController 成为 Initial View Controller 使用 属性检查器。我从一个 ViewController 来回移动到 另一种是使用我分配模态序列的按钮,从一个 ViewController 到另一个,使用 StoryBoard

我想要改变的:

我想明显保留导航按钮,删除模态序列并使用 UINavigationController 代替。如果我理解这个概念 正确地,当使用UINavigationController 时,我需要进入每个 UIButton-IBAction 在方法的最后,我必须推动下一个 我想移动到的 ViewController 到我的 NavigationController 上(我也 必须先弹出当前的?)。但是,我无法弄清楚如何 正确实施所有这些。

到目前为止我做了什么:

  • 我从情节提要中删除了所有模态转场,并保留了导航按钮及其对应的 IBActions
  • 我取消选中了属性检查器中使我的 FirstViewController 成为我的应用程序的初始视图控制器的框
  • 我进入了我的 AppDelegate.m 并尝试在那里创建导航控制器并使我的 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];
}

但什么也没发生。我做错了什么?

【问题讨论】:

  • 一个赞成票只是为了您的问题清晰。

标签: objective-c ios uiviewcontroller uinavigationcontroller


【解决方案1】:

您没有链接您的 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];

【讨论】:

  • 您是故意还是误用了navigationController = ..而不是UINavigationController *navigationController =
  • 这仅在您不是从故事板加载视图界面,​​而是从单个 xib 文件加载时才有效
  • UINavigationController *navigationController 是肯定需要的,但是我在 AppDelegate.h 中声明了导航控制器,所以我没有在这段代码中编写它
  • 对 Eric 似乎“取消”您的建议感到有些困惑。将尝试两者并回来并选择适合我的情况的答案:)
【解决方案2】:

如果您使用的是故事板,您只需将导航控制器拖入那里并将其连接到您的应用代理。只要它是主故事板,并且您已确定要首先加载的视图控制器,您就不需要在应用程序委托中加载任何视图。

为了以编程方式推送故事板中的视图,您需要执行以下操作:

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

您使用在情节提要编辑器中添加的标识符来标识视图控制器。下面是一些屏幕截图,以帮助说明我的意思。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多