MKAnnotation: title in iOS 5.0 - iphone

I have a warning in my custom class for the MKAnnotation.
In iOS 5.0, apple add a new readonly property, the title in the MKAnnotation class, but I already have this property in my custom MKAnnotation.
Then, how can I set the title in the MKAnnotation?
Interest link: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotation_Protocol/Reference/Reference.html
And here is my code for iOS less than 5.0:
// in MyMKAnnotation.h
#interface MyMKAnnotation : NSObject <MKAnnotation>
{
CLLocationCoordinate2D coordinate;
}
#property (nonatomic, retain) NSString *title;
// in MyMKAnnotation.m
- (id) initWithTitle:(NSString *)_title:(NSString *)_title localizacion:(CLLocationCoordinate2D)_localizacion
{
coordinate = _localizacion;
title = _title; //-----------> here is taking the warning
return self;
}
Than you!! :)

With #Lefteris help, I write this code:
// if the iOS version is lesser than 5.0, its retain, otherwise its copy
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_5_0
#property (nonatomic, retain) NSString *title;
#else
#property (nonatomic, copy) NSString *title;
#endif
Thank you very much!! :)
BASED IN: Why after upgrading to Xcode 4.2 does MKAnnotation display a warning

Related

Property not found on ViewController

I am beginning in iOS development. I've been searching a solution for days and I can't get it. I got the following code:
#interface SAProfileViewController : UITableViewController {
#public NSNumber *userId;
}
#property (weak, nonatomic) IBOutlet UIView *awesomeProfileHeader;
#property (nonatomic, assign) NSNumber *userId;
#end
As you can see I am declaring my property, and after that I do this :
#implementation SAProfileViewController
#synthesize userId = _userId;
...
On an other ViewController I import SAProfileViewController.
#import "SAProfileViewController.h"
...
-(void)goToUserProfile :(id) sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
SAProfileViewController *controller = [[SAProfileViewController alloc] init];
controller.userId = gesture.view.tag; // << HERE THE ERROR APPEARS
[self.navigationController pushViewController:controller animated:YES];
}
That's it, this is my code and I get the following error:
"Property 'userId' not found on object of type 'SAProfileViewController *'
Thanks in advance.
You have done a mistake with the property type.
Change
#property (nonatomic, assign) NSNumber *userId;
to
#property (nonatomic, retain) NSNumber *userId;
Than you have to create a NSNumber instance of gesture.view.tag like this
controller.userId = [NSNumber numberWithInteger:gesture.view.tag];
OR you can change your userId to NSInteger instead of NSNumber*
#property (nonatomic, assign) NSInteger userId;
(you have to do this for the object attribute userId, too)

Get image through instance method

I am trying to get image in return from the instance method, i am declaring the method in following way, but giving error. I am using NSObject class.
-(UIImage *)getAdImg;
This is giving error that "Expected a type". What does it mean. I normal view controller it is working fine, but in NSObject class it is giving this error. Please guide for the above.
Thanks in advance.
This is my .h file
#import <Foundation/Foundation.h>
#import "JSON.h"
#interface adClass : NSObject
{
NSString *mStrPid;
NSString *mStrAppUrl;
}
#property (nonatomic, strong) NSString *mStrPid;
#property (nonatomic, strong) NSString *mStrAppUrl;
#property (nonatomic, strong) NSMutableArray *mArrAdsDesc;
#property (nonatomic, strong) NSMutableArray *mArrDeviceType;
#property (nonatomic, strong) NSString *mDevice;
#property (nonatomic, strong) NSString *mImgStr;
-(void)iphoneDevice;
-(UIImage *)getAdImg; //(error line)
#end
You have to #import <UIKit/UIKit.h> (where UIImage is declared)
Use this code
-(UIImage *)getAdImg
{
UIImage *image=[[UIImage alloc]init];
image.images=/**An Array of images**/
return ima;
}
Hope it helps!!!

Custom Annotation with a button. didSelectAnnotaionView does not seem to get called

I am having a problem with my didSelectAnnotationView getting called when I use a custom annotation view. I was hoping someone might be able to take a look and help me with what I am missing.
I actually want a method to get called when the user touches the button show in the screenshot of my custom annotation which can be seen below. I was trying to make that the calloutAccessoryView, but I am doing something wrong. Please if anyone has done custom annotation views that a user can click on please see if you can help!! This is driving me crazy. I think I have posted the relevant info here, if not ask and I will edit my post.
I am adding a custom annotation like this:
LocationObject * obj = [m_MyObject.marLocations objectAtIndex:page];
obj.title =#"A";
[mvMapView addAnnotation:obj];
My Location object used above is defined like this:
// Location Object
#interface LocationObject : NSObject <MKAnnotation>
#property (nonatomic, retain) NSString * practicename;
#property (nonatomic, retain) NSString * address1;
#property (nonatomic, retain) NSString * mapaddress1;
#property (nonatomic, retain) NSString * address2;
#property (nonatomic, retain) NSString * city;
#property (nonatomic, retain) NSString * state;
#property (nonatomic, retain) NSString * zip;
#property (nonatomic, retain) NSString * phone;
#property (nonatomic, retain) NSString * fax;
#property (nonatomic, retain) NSString * email;
#property (nonatomic, retain) NSString * longitude;
#property (nonatomic, retain) NSString * latitude;
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#end // End Location Object
My viewForAnnotation does this:
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
[map.userLocation setTitle:#"I am here"];
return nil;
}
static NSString* AnnotationIdentifier = #"annotationViewID";
ViewDirectionsAnnotationView * annotationView = [[ViewDirectionsAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
int page = floor((svOfficesScrollView.contentOffset.x - svOfficesScrollView.frame.size.width / 2) / svOfficesScrollView.frame.size.width) + 1;
LocationObject * obj = [m_DentistObject.marLocations objectAtIndex:page];
double dLatitude = [obj.latitude doubleValue];
double dLongitude = [obj.longitude doubleValue];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(dLatitude, dLongitude);
((LocationObject*)annotation).coordinate = coordinate;
annotationView.lbPracticeName.text = obj.practicename;
annotationView.lbStreet.text = obj.address1;
NSString * sCityStateZip = [NSString stringWithFormat:#"%#, %# %#", obj.city, obj.state, obj.zip];
annotationView.lbCityStateZip.text = sCityStateZip;
annotationView.lbPhoneNumber.text = obj.phone;
annotationView.canShowCallout = YES;
annotationView.annotation = annotation;
annotationView.rightCalloutAccessoryView = (UIButton *)[annotationView viewWithTag:1]; //[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
}
And my Annotation View is like this:
#interface ViewDirectionsAnnotationView : MKAnnotationView
{
CLLocationCoordinate2D coordinate;
NSString * title;
NSString * subtitle;
}
#property (nonatomic, retain) IBOutlet UIView * loadedView;
#property (weak, nonatomic) IBOutlet UILabel * lbPracticeName;
#property (weak, nonatomic) IBOutlet UILabel * lbStreet;
#property (weak, nonatomic) IBOutlet UILabel * lbCityStateZip;
#property (weak, nonatomic) IBOutlet UILabel * lbPhoneNumber;
#end
And the xib for the above view looks like this:
But, my didSelectAnnotationView never gets called. Can anybody explain what I am missing?
Please, any help is appreciated this has been driving me nuts. I could have it completely wrong, but the custom view does appear so I think I am close.
Thanks for the help!!
UPDATE:
I forgot to mention about the delegate. I have checked the delegate many times and believe I have it right. In the ViewController .h file where the mapView is, I have the and I set [mapView setDelegate:self] In that same .m file is where I have implemented the viewForAnnotation method and didSelectAnnotationView. The viewForAnnotation gets called but not the other.
One more interesting piece of information is that if I click (touch) just below my custom annotation then didSelectAnnotationView does get called. I guess now I am more confused!!
UPDATE #2:
My xib is loaded this way:
In my viewForAnnotation I call initWithAnnotation. That method is as follows:
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString*)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self != nil)
{
[[NSBundle mainBundle] loadNibNamed:#"DirectionsAnnotation" owner:self options:nil];
if (loadedView)
{
[loadedView setFrame:CGRectMake(-(loadedView.frame.size.width/2), -(loadedView.frame.size.height), loadedView.frame.size.width, loadedView.frame.size.height)];
[self addSubview:loadedView];
}
}
return self;
}
The view which is displaying your MapView needs to be set as your delegate.
Where is your didSelectAnnotationView? Make sure that it implements the delegate protocol. E.g.
#interface MyView : UIViewController <MKMapViewDelegate>
...
#end
Then somewhere else...
[mapView setDelegate:myView];

Use SKProduct in normal class

I have StoreKit set up in my project and I can get product information from the app store fine, that's all working.
What I'm now trying to do is add an SKProduct as an instance variable in a class of mine, as below:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface VideoCell : UITableViewCell
{
IBOutlet UILabel *title;
IBOutlet UILabel *description;
SKProduct *product;
}
#property (retain) IBOutlet UILabel *title;
#property (retain) IBOutlet UILabel *description;
#property (retain) SKProduct *product;
It works fine with just the IBOutlet variables but the SKProduct lines have errors, as follows:
Unknown type name 'SKProduct'
I'm confused because the class name auto-completes but doesn't actually compile...
Any ideas?
It looks like you're missing the import:
#import <StoreKit/StoreKit.h>

userLocationVisble returns error

I have a mapview where i update my currentlocation with CoreLocation, but I also have checks which uses userLocation.
I still haven't found an alternative in fixing my problem
But for some reason I can't use userLocationVisible to hide the blue dot.
When I enter my MapView I start the locationManager, but before I have updated my location, the blue dot appears and my pin doesn't show up.
I've tried to use a custom MKAnnotation and init the coordinates with the newLocation from DidUpdateToLocation. But when I run this I get:
-[CustomPlacemark setCoordinate:]: unrecognized selector sent to instance 0x1de6c0
this is my CustomPlacemark:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface CustomPlacemark : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, retain) NSString *title;
#property (nonatomic, retain) NSString *subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;
- (NSString *)subtitle;
- (NSString *)title;
#end
#import "CustomPlacemark.h"
#implementation CustomPlacemark
#synthesize coordinate;
#synthesize title, subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
self=[super init];
if (self!=nil) {
coordinate=c;
}
return self;
}
-(NSString*)title{
return title;
}
-(NSString*)subtitle{
return subtitle;
}
-(void) dealloc
{
[title release]; title = nil;
[subtitle release]; subtitle = nil;
[super dealloc];
}
#end
Can someone also tell me why I can't use UserLocationVisible??
cordinate is read only property for customplacemark class . so u can not set cordinate property. to set cordinate property make it read and write.
change line#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
To #property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
The right way to do it will be initWithCoordinate rather than making the property readwrite
The blue dot appears when you set showsUserLocation=YES; If you want to hide the blue dot, set this to NO. Alternatively, you can determine if the user's location is visible on the screen using CoreLocation and enable showsUserLocation to YES.
Also, the blue dot is a special annotation class MKUserLocation that conforms to MKAnnotation protocol. If you are sending any messages to annotation objects that are your own class, you may want to exclude MKUserLocation annotation object.
If you want to know how to specifically deal with your custom annotation objects without sending incorrect messages to MKUserLocation class, let me know, I have the code that I can send.