iOS - UIViewController的属性navigationController即使它被设置为UINavigationController的rootViewController

时间:2014-06-20 09:42:04

标签: ios objective-c uinavigationcontroller uinavigationbar uinavigationitem

我正在使用故事板来构建我的应用程序的UI。基本上,我打开UINavigationController作为模态视图,在此导航控制器中,我将rootViewController嵌入另一个UIViewController的实例(位置选择视图)。这都是在故事板中设置的,看起来基本上是这样的:

enter image description here

现在,我想访问viewDidLoad LocationSelectionViewController中的导航控制器,以便在导航栏中添加UISearchBarself.searchDisplayController.displaysSearchBarInNavigationBar = YES;,但这不起作用,因为我的UINavigationController此时为零,我知道因为我设置了断点并记录了它:

(lldb) po self.navigationController

nil

是否有人知道为什么或我必须这样做,以便在UINavigationController上实际可以访问LocationSelectionViewController的实例?

更新:这里有更多代码,标题实际上只包含声明

LocationSelectionViewController.h

@protocol LocationSelectionViewControllerDelegate <NSObject>
- (void)setLocation:(Location *)location;
@end

 @interface LocationSelectionViewController : UIViewController <GMSGeocodingServiceDelegate, UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate, GMSMapViewDelegate>
 @end

LocationSelectionViewController.m

的部分内容
- (void)viewDidLoad
{
  [super viewDidLoad];

  self.searchBar.text = DUMMY_ADDRESS;

  self.previouslySearchedLocations = [[CoreDataManager coreDataManagerSharedInstance] previouslySearchedLocations];
  self.searchResults = [[NSMutableArray alloc] initWithArray:self.previouslySearchedLocations];

  self.mapView.delegate = self;
  self.gmsGeocodingService = [[GMSGeocodingService alloc] initWithDelegate:self];

  self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
  }


- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  [self addMapView];
}

1 个答案:

答案 0 :(得分:0)

好的,我刚刚解决了我的问题。我坚信这是界面构建器中的一个错误。我删除了旧的导航控制器,只是将新的导航控制器拖放到故事板上,现在在po self.navigationController中调用viewDidLoad实际上返回UINavigationController的实例。感谢所有的帮助,我非常感激!