帮助我了解如何使用此实现访问 UINaviagtionController
所以我正在构建一个应用程序,并且正在运行一些不需要彼此了解的 ViewController,因此我开始像这样切换视图......
// remove the previous view in order to load in the new view
NSArray *sViews = [self.view subviews];
[sViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
// create the new view, in this case the user wishes to
BaseViewController *baseVC = [[BaseViewController alloc] initWithNibName:@"BaseViewController" bundle:[NSBundle mainBundle]];
self.baseViewController = baseVC;
[baseVC release];
// add the newly created view to the screen
[self.view insertSubview:baseViewController.view atIndex:0];
上面是我想要的导航控制器的视图控制器因此,在这个视图控制器的 .m 中,我创建了一个 UINavigationController 作为成员变量,并将其命名为 navController。然后我尝试使用下面的代码实现 UINavigationController。
UIViewController *control = [[BusinessDisplayViewController alloc] initWithNibName:@"BusinessDisplayViewController" bundle: nil];
navController = [[UINavigationController alloc] initWithRootViewController:control];
[self presentModalViewController:navController animated:YES];
我遇到的问题有两个。首先,当加载 BusinessDisplayViewController(如下)时,我的 mapView 和 tableView 之间有一个 20 像素左右的间隙,当我使用 insertSubview:
加载它时,它不存在,不知道为什么会这样。其次,一旦我进入 BusinessDisplayViewController.m,我不确定如何访问在 BaseViewController 中创建的 navigationController。有人可以解释为什么我的视图会受到影响,我如何访问 navigationController 或者我是否以正确的方式处理这个问题。
So I'm building an app and I am running through a few ViewControllers that don't need to know about each other, so I start off switching through views like so...
// remove the previous view in order to load in the new view
NSArray *sViews = [self.view subviews];
[sViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
// create the new view, in this case the user wishes to
BaseViewController *baseVC = [[BaseViewController alloc] initWithNibName:@"BaseViewController" bundle:[NSBundle mainBundle]];
self.baseViewController = baseVC;
[baseVC release];
// add the newly created view to the screen
[self.view insertSubview:baseViewController.view atIndex:0];
The above is the view controller that I want the navigation controller to reside in. So within the .m of this view controller I created a UINavigationController as a member variable and named it navController. I then tried implementing a UINavigationController using the code below.
UIViewController *control = [[BusinessDisplayViewController alloc] initWithNibName:@"BusinessDisplayViewController" bundle: nil];
navController = [[UINavigationController alloc] initWithRootViewController:control];
[self presentModalViewController:navController animated:YES];
The problem I'm running into is two fold. First, when the BusinessDisplayViewController (below) is loaded there is a 20 pixel or so gap between my mapView and tableView that isn't there when I was loading it using insertSubview:
not sure why that would be. Second, once I'm in BusinessDisplayViewController.m I'm not sure how to access the navigationController created in BaseViewController. Could someone explain why my view would be effected, how I could access the navigationController or if I'm even going about this the right way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UINavigationController
设计用于 iPhone 上三种可能的上下文之一:UITabBarController
的 viewController 之一。在您的情况下,
UINavigationController
已将自身配置为呈现为窗口的子视图。这就是您在顶部看到 20 像素间隙的原因。由于窗口对象与状态栏重叠,UINavigationController
会将其导航栏的位置偏移 20 像素(如果您正在打电话,则偏移更多)。使用 UINavigationController 作为根视图控制器的标准方法是将其构造为 application:didFinishLaunchingWithOptions: 中应用程序委托的属性,并将其视图添加为窗户。然后,在您推送到导航堆栈的任何视图控制器中,您可以使用 self.navigationController 访问导航控制器对象。
UINavigationController
is designed for use in one of three possible contexts on iPhone:UITabBarController
.presentModalViewController:animated:
.In your case, the
UINavigationController
has configured itself for presentation as a subview of the window. This is why you see the 20 pixel gap at the top. Since the window object underlaps the status bar,UINavigationController
offsets the position of its navigation bar by 20 pixels (or more, if you're on a phone call).The standard way to use
UINavigationController
as your root view controller is to construct it as a property of your app delegate inapplication:didFinishLaunchingWithOptions:
, and add its view as a subview of the window. Then within any view controller you push onto your navigation stack, you can access the navigation controller object usingself.navigationController
.通常,您希望 UINavigationController 位于根级别,以这种方式设置您的应用程序是否有特定原因?不过,要回答您的问题,您可以通过为其设置 属性 来访问该变量,然后使用点符号:
baseVC.navController
。对于20像素空间问题,发布你的BaseViewController视图相关代码。这可能是边界与框架问题。
Usually, you want your UINavigationController to be at the root level, Is there a specific reason for having your app setup this way? To answer your question though, you can access the variable by setting a property for it, then using the dot notation:
baseVC.navController
.For the 20 pixel space problem, post your BaseViewController view related code. It is probably a bounds vs frame issue.