菜鸟教程小白 发表于 2022-12-12 23:10:35

ios - 无法在 MKPinAnnotationView 上设置标题


                                            <p><p>我已按照本教程进行操作:
<a href="http://www.shawngrimes.me/2011/04/custom-map-pins-for-mapkit/#comment-193" rel="noreferrer noopener nofollow">http://www.shawngrimes.me/2011/04/custom-map-pins-for-mapkit/#comment-193</a> </p>

<p>但我无法添加标题和描述</p>

<p>(在此处查看我的代码 <a href="http://pastebin.com/03mDLc9q" rel="noreferrer noopener nofollow">http://pastebin.com/03mDLc9q</a>)</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您正在尝试将名称和描述设置为注释 View 的属性,您应该在注释对象上使用 <code>title</code> 和 <code>subtitle</code> - <code>MyAnnotationClass</code>,注解 View 在渲染标注时将使用该对象的标题和副标题。</p>

<p>我更改了您的代码以在此处工作:<a href="http://pastebin.com/YRGYhQev" rel="noreferrer noopener nofollow">http://pastebin.com/YRGYhQev</a> </p>

<pre><code>@interface MyAnnotationClass : NSObject&lt;MKAnnotation&gt;

@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

-(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate;

@end


MyAnnotationClass.m

#import &#34;MyAnnotationClass.h&#34;

@implementation MyAnnotationClass

-(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate{
    self=;
    if(self){
      _coordinate = coordinate;
    }
    return self;
}

-(void) dealloc{
    ;
    ;
    ;
}
@end


ViewController.h

#import &lt;UIKit/UIKit.h&gt;
#import &lt;MapKit/MapKit.h&gt;

@interface ViewController : UIViewController&lt;MKMapViewDelegate&gt; {
    IBOutlet MKMapView *_myMapView;
    NSArray *_myAnnotations;
}

@property (nonatomic, retain) NSArray *myAnnotations;
@property (nonatomic, retain) IBOutlet MKMapView *myMapView;

@end


ViewController.m

#import &#34;ViewController.h&#34;
#import &#34;AppDelegate.h&#34;
#import &lt;MapKit/MapKit.h&gt;
#import &lt;CoreLocation/CoreLocation.h&gt;
#import &#34;PlaceMark.h&#34;
#import &#34;MyAnnotationClass.h&#34;
@interface ViewController ()

@end

@implementation ViewController
@synthesize myMapView = _myMapView;
@synthesize myAnnotations = _myAnnotations;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = ;
    if (self) {
      // Custom initialization
    }
    return self;
}

-(void) viewDidLoad{
    AppDelegate *appDelegate = (AppDelegate *)[delegate];

    //Initialize annotation
    MyAnnotationClass *commuterLotAnnotation=[ initWithCoordinate:CLLocationCoordinate2DMake(appDelegate.latitude , appDelegate.longitude)];
    commuterLotAnnotation.title = @&#34;Hello title&#34;;
    commuterLotAnnotation.subtitle = @&#34;Correct&#34;;

    MyAnnotationClass *overflowLotAnnotation=[ initWithCoordinate:CLLocationCoordinate2DMake(appDelegate.latitude , appDelegate.longitude)];
    overflowLotAnnotation.title = @&#34;Hello title&#34;;
    overflowLotAnnotation.subtitle = @&#34;Correct&#34;;

    //Add them to array
    self.myAnnotations=;

    //Release the annotations now that they&#39;ve been added to the array
    ;
    ;

    //add array of annotations to map
    ;
}


-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{
    static NSString *parkingAnnotationIdentifier=@&#34;ParkingAnnotationIdentifier&#34;;

    if(]){
      //Try to get an unused annotation, similar to uitableviewcells
      MKAnnotationView *annotationView=;

      //If one isn&#39;t available, create a new one
      if(!annotationView){
            annotationView=[ initWithAnnotation:annotation reuseIdentifier:parkingAnnotationIdentifier];
            //Here&#39;s where the magic happens
            annotationView.image=;
            annotationView.canShowCallout = YES;
      }
      return annotationView;
    }
    return nil;
}


- (void)viewDidUnload
{
    ;
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


@end
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 无法在 MKPinAnnotationView 上设置标题,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/12905274/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/12905274/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 无法在 MKPinAnnotationView 上设置标题