Ios 将UIButton添加到mapkit中的注释详图索引

Ios 将UIButton添加到mapkit中的注释详图索引,ios,objective-c,uibutton,annotations,mapkit,Ios,Objective C,Uibutton,Annotations,Mapkit,我想在特定位置的注释标注中添加一个自定义图像按钮。例如,搜索星巴克会显示星巴克位置的注释标记,当按下标记时,标注将显示一个按钮,然后将引导您到另一个包含星巴克信息的viewcontroller。现在,当按下注释时会显示位置的地址,我如何将其更改为在我选择的自定义位置显示按钮?我对xcode非常陌生,似乎找不到关于我如何设计我的应用程序的有用信息。除了我不知道从哪里开始添加按钮外,所有功能都符合要求 这是我的视图控制器 #import "ViewController.h" @interface

我想在特定位置的注释标注中添加一个自定义图像按钮。例如,搜索星巴克会显示星巴克位置的注释标记,当按下标记时,标注将显示一个按钮,然后将引导您到另一个包含星巴克信息的viewcontroller。现在,当按下注释时会显示位置的地址,我如何将其更改为在我选择的自定义位置显示按钮?我对xcode非常陌生,似乎找不到关于我如何设计我的应用程序的有用信息。除了我不知道从哪里开始添加按钮外,所有功能都符合要求

这是我的视图控制器

#import "ViewController.h"

@interface ViewController () <UISearchDisplayDelegate, UISearchBarDelegate>

@end

@implementation ViewController {
MKLocalSearch *localSearch;
MKLocalSearchResponse *results;
}

#pragma mark - View Lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
[self.searchDisplayController setDelegate:self];
[self.ibSearchBar setDelegate:self];

// Zoom the map to current location.
[self.ibMapView setShowsUserLocation:YES];
[self.ibMapView setUserInteractionEnabled:YES];
[self.ibMapView setUserTrackingMode:MKUserTrackingModeFollow];

}

#pragma mark - Search Methods

- (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.ibMapView.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.ibMapView addAnnotation:item.placemark];
[self.ibMapView selectAnnotation:item.placemark animated:NO];


[self.ibMapView setCenterCoordinate:item.placemark.location.coordinate animated:YES];

[self.ibMapView setUserTrackingMode:MKUserTrackingModeNone];

}

@end


#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UISearchBar *ibSearchBar;
@property (strong, nonatomic) IBOutlet MKMapView *ibMapView;
@end
#导入“ViewController.h”
@界面视图控制器()
@结束
@实现视图控制器{
MKLocalSearch*localSearch;
MKLocalSearchResponse*结果;
}
#pragma标记-视图生命周期
-(无效)viewDidLoad
{
[超级视图下载];
[self.searchDisplayController setDelegate:self];
[self.ibSearchBar setDelegate:self];
//将地图缩放到当前位置。
[self.ibMapView设置显示位置:是];
[self.ibMapView setUserInteractionEnabled:是];
[self.ibMapView setUserTrackingMode:MKUserTrackingModeFollow];
}
#pragma标记-搜索方法
-(无效)搜索栏搜索按钮选中:(UISearchBar*)搜索栏{
//取消以前的任何搜索。
[本地搜索取消];
//执行新的搜索。
MKLocalSearchRequest*request=[[MKLocalSearchRequest alloc]init];
request.naturalLanguageQuery=searchBar.text;
request.region=self.ibMapView.region;
[UIApplication sharedApplication].networkActivityIndicatorVisible=是;
localSearch=[[MKLocalSearch alloc]initWithRequest:request];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse*响应,NSError*错误){
[UIApplication sharedApplication].networkActivityIndicatorVisible=否;
如果(错误!=nil){
[[[UIAlertView alloc]initWithTitle:NSLocalizedString(@“映射错误”,无)
消息:[错误本地化描述]
代表:无
取消按钮提示:NSLocalizedString(@“OK”,无)其他按钮提示:无]显示];
返回;
}
如果([response.mapItems count]==0){
[[[UIAlertView alloc]initWithTitle:NSLocalizedString(@“无结果”,无)
信息:无
代表:无
取消按钮提示:NSLocalizedString(@“OK”,无)其他按钮提示:无]显示];
返回;
}
结果=响应;
[self.searchDisplayController.searchResultsTableView重新加载数据];
}];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回[results.mapItems count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*标识符=@“SearchResultCell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:标识符];
}
MKMapItem*item=results.mapItems[indexPath.row];
cell.textlab.text=item.name;
cell.detailTextLabel.text=item.placemark.addressDictionary[@“Street”];
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
[self.searchDisplayController设置活动:否动画:是];
MKMapItem*item=results.mapItems[indexPath.row];
[self.ibMapView addAnnotation:item.placemark];
[self.ibMapView selectAnnotation:item.placemark动画:否];
[self.ibMapView setCenterCoordinate:item.placemark.location.coordinate:YES];
[self.ibMapView setUserTrackingMode:MKUserTrackingModeNone];
}
@结束
#进口
#进口
@界面ViewController:UIViewController
@属性(强,非原子)IBUISearchBar*ibSearchBar;
@属性(强,非原子)IBMAppView*IBMAppView;
@结束

您可以在注释的
视图中设置按钮作为详图索引附件视图
方法:

 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
 {

      static NSString *AnnotationIdentifier = @"Annotation";

      if ([annotation isKindOfClass:MKUserLocation.class]) {
         return nil;
      }

      MKPinAnnotationView* pinAnnotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];

      if (!pinAnnotationView)
      {
           pinAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] ;
           pinAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
           pinAnnotationView.canShowCallout = YES;
      }

      return pinAnnotationView;
 }

在本例中,我使用
UINavigationController
来管理通过视图控制器的导航。

关于如何自定义详图索引视图,有很多资源,如和。您还可以查看苹果的文档,特别是子类化注释及其示例。

感谢您的回复,我只是在理解第一段代码应该放在哪里时遇到了一点困难。我的viewForAnnotation方法将位于何处?我似乎找不到它。用
mapView:viewForAnnotation
方法代码编辑了我的答案。您应该在
ViewController.m
中找到它……我很难让这段代码正常工作。我将这两种方法都放入ViewController.m文件中,但我收到错误消息,说detalview是一个未声明的标识符。我对xcode完全陌生,我为我的知识不足道歉。无需道歉。您有一个名为DetailViewController的类吗?您在类中正确导入了它吗?没有,我没有名为detail view controller的类。该按钮应将我指向InfoController,它是我故事板中的下一个viewController。InfoController将填充特定于在调用中使用按钮选择的位置的信息。
 -(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
 {
     InfoController *infoController = [[InfoController alloc] initWithNibName:@"InfoController" bundle:[NSBundle mainBundle]];

     /*
     here you can pass the necessary information to your InfoController
     */

     [self.navigationController pushViewController:infoController  animated:YES];
     [infoController  release];
 }