【问题标题】:Programmatically creating UINavigationController in iOS在 iOS 中以编程方式创建 UINavigationController
【发布时间】:2014-04-10 07:37:46
【问题描述】:

我是 iOS 新手。我想在我的应用程序中使用导航控制器,但我不知道该怎么做。那么任何人都可以指导我一步一步地在我的应用程序中创建导航。

【问题讨论】:

标签: ios uinavigationcontroller


【解决方案1】:

appDelegate.h

@property (strong, nonatomic) UINavigationController *navController;

并在appDelegate.m 中设置委托UINavigationControllerDelegate 和综合对象 现在,

appDelegate.m

你可以在didFinishLaunchingWithOptions方法中设置导航控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    frstVwCntlr = [[firstViewController alloc] initWithNibName:@"firstViewController" bundle:nil];
    self.navController = [[UINavigationController alloc] initWithRootViewController:self.frstVwCntlr];
    self.window.rootViewController = self.navController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

在上面的代码中,你的 firstViewController 设置为 UINavigationControllerUINavigationController 添加到 UIWindow 喜欢

self.window.rootViewController = self.navController

希望对你有帮助

【讨论】:

  • 如果对你有价值请点赞回答@user3418619
【解决方案2】:

如果您想以编程方式创建所有内容,则必须在 AppDelegate 中进行。

但如果您不想以编程方式执行此操作,则只需在 Storyboard 中选择 ViewController,然后选择菜单选项:

Editor > Embed In > Navigation Controller

【讨论】:

    【解决方案3】:

    您可以在 Appdelegate 中创建 UINavigationController 并在其上设置您的第一个视图控制器。

    【讨论】:

      【解决方案4】:

      因此,要在不使用情节提要的情况下以编程方式创建 UINavigationController,请转到您的应用委托并执行以下操作。创建两个属性,window和viewController

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
      
          self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
          self.window.backgroundColor=[UIColor clearColor];
      
          self.viewController = [[YourFirstViewController alloc] initWithNibName:@"YourFirstViewController" bundle:nil];
          UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
          self.window.rootViewController = navController;
          [self.window makeKeyAndVisible];
      
          // Override point for customization after application launch.
          return YES;
      }
      

      【讨论】:

        【解决方案5】:
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ImageViewController2 *dealVC = (ImageViewController2 *)[storyboard instantiateViewControllerWithIdentifier:@"ImageViewController2"];
        [self.navigationController pushViewController:dealVC animated:YES];
        

        ImageViewController2 是一个类名

        【讨论】:

          【解决方案6】:

          这是您应该在应用委托中编写的代码。

          UIViewController *vc=[[UIViewController alloc]initWithNibName:@"vc1" bundle:nil];
          UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
          view.backgroundColor=[UIColor redColor];
          [vc setView:view];
          
          self.navme=[[UINavigationController alloc]initWithRootViewController:vc];
          self.window.rootViewController = self.navme;
          

          【讨论】:

            【解决方案7】:

            对于 Swift 3.0,使用过滤器:

            let desiredController = self.navigationController!.viewControllers.filter { $0 is YourController }.first!
            self.navigationController!.popToViewController(desiredController, animated: true)
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-09-21
              • 2016-12-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多