应用程序尝试推送到目标<UINavigationController>上的 nil 视图控制器



我有一个名为maps的导航按钮,我推到一个名为mapviewcontroller的视图控制器,我不断地让应用程序试图推到一个nil视图控制器。

我尝试改变故事板的名称,尝试用nibname初始化我的地图对象,并有一个标识符设置在故事板和编程的mapvc ,以及,但似乎没有工作。

导航按钮代码:

UIBarButtonItem *maps = [[UIBarButtonItem alloc]
                           initWithTitle:@"Map"
                           style:UIBarButtonItemStyleBordered
                           target:self
                           action:@selector(mapIsPressed:)];
self.navigationItem.rightBarButtonItem = maps;
}
-(void)mapIsPressed: (UIBarButtonItem*) paramsender
{
self.map=[[MapViewController alloc]initWithNibName:@"map" bundle:nil];
self.map=[self.storyboard instantiateViewControllerWithIdentifier:@"mapp"];
[self.navigationController pushViewController:self.map animated:YES];
}

在我的mapvc中,我有一个搜索栏按钮项,当按下时加载一个表视图和一个地图视图。映射的代码vc:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.searchDisplayController setDelegate:self];
[self.mySearchBar setDelegate:self];
self.myMapView.delegate=self;
// Zoom the map to current location.
[self.myMapView setShowsUserLocation:YES];
[self.myMapView setUserInteractionEnabled:YES];
[self.myMapView setUserTrackingMode:MKUserTrackingModeFollow];

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
[locationManager startUpdatingLocation];
[self.myMapView setRegion:MKCoordinateRegionMake(locationManager.location.coordinate,     MKCoordinateSpanMake(0.2, 0.2))];
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.region = self.myMapView.region;
request.naturalLanguageQuery = @"restaurant";
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError      *error){
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    results = response;
    if (response.mapItems.count == 0)
        NSLog(@"No Matches");
    else
        for (MKMapItem *item in response.mapItems)
        {
            NSLog(@"name = %@", item.name);
            NSLog(@"Phone = %@", item.phoneNumber);
            [_matchingItems addObject:item];
            MKPointAnnotation *annotation =
            [[MKPointAnnotation alloc]init];
            annotation.coordinate = item.placemark.coordinate;
            annotation.title = item.name;
            [self.myMapView addAnnotation:annotation];
        }
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} 
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// Cancel any previous searches.
[localSearch cancel];
// Perform a new search.
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = searchBar.text;
request.region = self.myMapView.region;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError     *error){
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    if (error != nil) {
        [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Map Error",nil)
                                    message:[error localizedDescription]
                                   delegate:nil
                          cancelButtonTitle:NSLocalizedString(@"OK",nil)     otherButtonTitles:nil] show];
        return;
    }
    if ([response.mapItems count] == 0) {
        [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No Results",nil)
                                    message:nil
                                   delegate:nil
                          cancelButtonTitle:NSLocalizedString(@"OK",nil)     otherButtonTitles:nil] show];
        return;
    }
    results = response;
    [self.searchDisplayController.searchResultsTableView reloadData];
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [results.mapItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:     (NSIndexPath *)indexPath {
static NSString *IDENTIFIER = @"SearchResultsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle     reuseIdentifier:IDENTIFIER];
}
MKMapItem *item = results.mapItems[indexPath.row];
cell.textLabel.text = item.name;
cell.detailTextLabel.text = item.placemark.addressDictionary[@"Street"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath     *)indexPath {
[self.searchDisplayController setActive:NO animated:YES];
MKMapItem *item = results.mapItems[indexPath.row];
[self.myMapView addAnnotation:item.placemark];
[self.myMapView selectAnnotation:item.placemark animated:YES];
[self.myMapView setCenterCoordinate:item.placemark.location.coordinate animated:YES];
[self.myMapView setUserTrackingMode:MKUserTrackingModeNone];
}
@end

问题很可能是self。假设视图正确地从nib或storyboard加载,Map会立即被释放。

确保自我。map声明为@property (strong, nonatomic) MapViewController *map,或者修改代码,在设置self.map:

之前将新创建的视图控制器保存为局部作用域变量。
-(void)mapIsPressed: (UIBarButtonItem*) paramsender
{
    MapViewController *mapView = [[MapViewController alloc]initWithNibName:@"map" bundle:nil]; // or load from storyboard
    [self.navigationController pushViewController:mapView animated:YES];
    self.map = mapView;
}

在ARC下,任何没有引用或只有弱引用的对象都将被自动释放。对该对象的任何弱引用都将被设置为nil,这解释了你得到的错误。

现在,一旦视图控制器被推送到视图层次,UINavigationController将保持对它的强引用,所以你可以在推送后使用弱引用,没有问题

您的问题似乎是在您的mapIsPressed方法。你重写了刚刚创建的视图控制器:

self.map=[[MapViewController alloc]initWithNibName:@"map" bundle:nil];
self.map=[self.storyboard instantiateViewControllerWithIdentifier:@"mapp"];

最新更新