【问题标题】:uinavigation bar in all view所有视图中的导航栏
【发布时间】:2012-09-12 09:27:21
【问题描述】:

在我的项目中,导航栏只出现在 rootview(homeview) 中,我想在所有视图中启用导航栏?这是我的代码?我应该为此做些什么改变

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.window.rootViewController =navigationController;
[self.window addSubview:[navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}

请帮我写代码?

【问题讨论】:

  • 你是如何从 viewController2 转到另一个 viewControllers 的?
  • 来自 viewController2 我有按钮,只使用按钮。不仅我在其他视图中使用导航栏作为 IB,我希望在每个视图中以编程方式导航栏。我怎么能? @Padavan
  • 你用这样的东西去另一个viewControllers吗? [self.navigationController pushViewController:newViewController Animation:NO];在你的 viewController2 中?
  • 不,我正在使用类似dis about *scraping=[[about alloc]initWithNibName:@"about" bundle:nil]; [self presentModalViewController:scraping animated:YES];@Padavan

标签: iphone ios uiview uinavigationcontroller


【解决方案1】:

看到这个link

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    navigationController =[[UINavigationController alloc] init];

    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [navigationController pushViewController:viewController2 animated:NO];  

    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

当您从 viewController2 导航到另一个视图时,执行 pushViewController 而不是像 presentView 这样的其他视图

[self.navigationController pushViewController:anotherViewController animated:YES];

【讨论】:

  • 你是如何从 viewController2 转到下一个视图的?你应该使用 pushViewController
  • 它与 pushViewController 一起使用,我使用了 presentViewController。非常感谢队友
  • 不客气...如果有帮助,您介意接受答案吗? :)
  • 从导航视图其他视图它工作正常但如果我去其他视图使用 uibutton 它不工作,这里我该怎么办@Neo
  • 您在使用情节提要吗?您是否使用界面生成器设置了按钮?
【解决方案2】:

将此代码放在委托类中。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Set the navigation controller as the window's root view controller and display.
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

【讨论】:

    【解决方案3】:

    如果你想在所有视图中启用导航,那么你必须在 AppDelegate.m 中声明导航,它可以在所有视图中查看。我现在没有我的 mac rite,但这是我现在能提供的最好的建议 :)

    【讨论】:

      【解决方案4】:
      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
      navigationController = [[UINavigationController alloc] initWithRootViewController:viewController2];
      self.window.rootViewController =navigationController;
      [self.window addSubview:[navigationController view]];
      [self.window makeKeyAndVisible];
      return YES;
      }
      

      将 uiviewcontroller 更改为 SecondViewController

      【讨论】:

      • 现在它也是第一次出现@AppleDelegate
      【解决方案5】:

      试试这个方法可能对你有帮助

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
      
      
          self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
      
          // [NSThread sleepForTimeInterval:0.1]; // simulate waiting for server response
      
          // Override point for customization after application launch.
      
          self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
      
          // Initialise the navigation controller with the first view controller as its root view controller
      
          UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
      
          // This is where we hide the navigation bar! :)
      
          [navigationController setNavigationBarHidden:NO];
      
          // Navigation controller has copy of view controller, so release our copy
      
      
      
          //[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
      
      
          [self.viewController release];
          // Add the navigation controller as a subview of our window
      
          [_window addSubview:[navigationController view]];
          [_window makeKeyAndVisible];
          return YES;
      
      }
      

      【讨论】:

      • 不,它不起作用,它只是第一次出现@Vikas Rajput
      • 这是我当前工作的代码,你能说一下你现在工作的 xcode。
      • 这里相同,但我的代码运行正确,请检查根视图和其他视图,其中隐藏了导航控制器。
      猜你喜欢
      • 2022-09-30
      • 1970-01-01
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      相关资源
      最近更新 更多