我有一个名为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
答案 0 :(得分:0)
问题很可能是self.map被立即释放,假设视图正在从笔尖或故事板正确加载。
确保将self.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
将保持对它的强引用,因此您可以在推送它之后使用弱引用而没有任何问题
答案 1 :(得分:-1)
您的问题似乎出现在mapIsPressed方法中。您正在覆盖刚刚创建的视图控制器:
self.map=[[MapViewController alloc]initWithNibName:@"map" bundle:nil];
self.map=[self.storyboard instantiateViewControllerWithIdentifier:@"mapp"];