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];
Related
I'm trying to make a custom MKAnnotation class so it can hold additional information but i keep getting this error :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [AnnotationForId setCoordinate:]: unrecognized selector sent to instance 0x16f63340'
I've tried looking up how to create one and i've followed what i've found online and i'm confused at why i keep getting this error.
Here is my custom annotation class.
#import <MapKit/MapKit.h>
#import <UIKit/UIKit.h>
#interface AnnotationForId : NSObject <MKAnnotation >{
NSString *title;
NSString *subtitle;
CLLocationCoordinate2D coordinate;
NSInteger shopId;
}
#property(nonatomic)NSInteger shopId;
#property (nonatomic, copy) NSString * title;
#property (nonatomic, copy) NSString * subtitle;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#end
I've synthesized the property variables in .m class.
I try to call the custom class in my main controller:
for(Shop *shops in feed.shops){
NSLog(#"reached");
AnnotationForId *shop = [[AnnotationForId alloc] init];
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(shops.latitude, shops.longitude);
shop.coordinate = coords;
//[[CLLocationCoordinate2DMake(shops.latitude, shops.longtitude)]];
shop.title = shops.name;
shop.subtitle = #"Coffee Shop";
[map addAnnotation:shop];
}
Any help to why this isnt working would be awesome.
Thanks.
It seems like you're trying to set your readonly property.
You declared it as readonly:
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
But then you're trying to use setter:
shop.coordinate = coords;
readonly means that no setter will be defined for other classes to leverage.
Edit:
I think you should add convenience initializer to your AnnotationForId class like:
-(id)initWithCoordinate:(CLLocationCoordinate2D)coord
{
self = [super init]; // assuming init is the designated initialiser of the super class
if (self)
{
coordinate = coord;
}
return self;
}
So your code will look like this:
for(Shop *shops in feed.shops){
NSLog(#"reached");
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(shops.latitude, shops.longitude);
AnnotationForId *shop = [[AnnotationForId alloc] initWithCoordinate:coords];
//[[CLLocationCoordinate2DMake(shops.latitude, shops.longtitude)]];
shop.title = shops.name;
shop.subtitle = #"Coffee Shop";
[map addAnnotation:shop];
}
This is Simple Example For how to Create Custom AnnotationView.
Create custom AnnotationView:
#import <MapKit/MapKit.h>
#interface AnnotationView : MKPlacemark
#property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, strong) NSString *title;
#property (nonatomic, strong) NSString *subtitle;
// you can put here any controllers that you want. (such like UIImage, UIView,...etc)
#end
And in .m file
#import "AnnotationView.h"
#implementation AnnotationView
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary
{
if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary]))
{
self.coordinate = coordinate;
}
return self;
}
#end
// Use Annotation Add #import "AnnotationView.h" in your relevant .m file:
CLLocationCoordinate2D pCoordinate ;
pCoordinate.latitude = LatValue;
pCoordinate.longitude = LanValue;
// Create Obj Of AnnotationView class
AnnotationView *annotation = [[AnnotationView alloc] initWithCoordinate:pCoordinate addressDictionary:nil] ;
annotation.title = #"I m Here";
annotation.subtitle = #"This is Sub Tiitle";
[self.mapView addAnnotation:annotation];
Above is simple Example of how to create AnnotationView.
You have given the property as readonly, which sets only a getter method for the property. So please make the property retain and try it once again.
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)
I'm trying to use a UITextField as an output for a simple encryption. The field starts out with a nil string (its default behavior), and I try to update it in the encryptButtonPressed: action. I know I'm getting a good NSString into ciphertext, and I'm getting no errors or warnings or anything that the method doesn't exist, but the field remains blank. Any ideas?
Here's the header:
#interface FirstViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UITextField *affineKeyField;
IBOutlet UITextField *shiftKeyField;
IBOutlet UITextField *messageField;
IBOutlet UITextField *ciphertextField;
MAAffineMachine* machine;
}
#property (nonatomic, retain) UITextField *affineKeyField;
#property (nonatomic, retain) UITextField *shiftKeyField;
#property (nonatomic, retain) UITextField *messageField;
#property (nonatomic, retain) UITextField *ciphertextField;
#property (nonatomic, retain) UIButton *encryptButton;
#property (nonatomic, retain) UIButton *clipboardButton;
- (IBAction)encryptButtonPressed:(id)sender;
- (IBAction)clipboardButtonPressed:(id)sender;
#end
And the action, defined in the controller's implementation:
- (IBAction)encryptButtonPressed:(id)sender {
NSLog(#"Encrypt Button pushed.\n");
int affine = [[affineKeyField text] intValue];
int shift = [[shiftKeyField text] intValue];
NSString* message = [messageField text];
NSLog(#"%d, %d, %#", affine, shift, message);
NSString* ciphertext = [machine encryptedStringFromString:message
ForAffine:affine
Shift:shift];
NSLog(#"%#\n", ciphertext);
[[self ciphertextField] setText: ciphertext];
}
If ciphertext has a proper value, you should check that ciphertextField is linked properly in the interface builder.
Try [[self ciphertextField] setText: ciphertext] -> [ciphertextField setText: ciphertext] or cipherTextField.text = ciphertext;
I have a problem that I have been searching for a solution for but can't seem to find one that works. UserInput.xib , Calculations.h & .m ,DataOutput.xib ... there is no .xib for that calculations.
UserInput.h
DataOutput *dataOutput;
UITextField *tf1;
UITextField *tf2;
UITextField *tf3;
#property (nonatomic, retain) DataOutput *dataOutput;
#property (nonatomic, retain) IBOutlet UITextField *tf1;
#property (nonatomic, retain) IBOutlet UITextField *tf2;
#property (nonatomic, retain) IBOutlet UITextField *tf3;
UserInput.m
#synthesize dataOutput;
#synthesize tf1;
#synthesize tf2;
#synthesize tf3;
- (IBAction)calculate:(id)sender {
DataOutput *dataOut = [[DataOutput alloc] initWithNibName:#"DataOutput" bundle:nil];
Calculations *calc = [[Calculations alloc] init];
dataOut.dataCalc = calc;
dataOut.dataCalc.tf1 = tf1.text;
dataOut.dataCalc.tf2 = tf2.text;
dataOut.dataCalc.tf3 = tf3.text;
dataOut.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:dataOut animated:YES];
[dataOut release];
DataOutput.h
Calculations *dataCalc;
UILabel *results1;
UILabel *results2;
UILabel *results3;
#property (nonatomic, retain) Calculations *dataCalc;
#property (nonatomic, retain) IBOutlet UILabel *results1;
#property (nonatomic, retain) IBOutlet UILabel *results2;
#property (nonatomic, retain) IBOutlet UILabel *results3;
DataOutput.m
#synthesize results1;
#synthesize results2;
#synthesize results3;
- (void)viewDidLoad {
self.results1.text = dataCalc.tf1;
self.results2.text = dataCalc.tf2;
self.results3.text = dataCalc.tf3;
Calculations.h
NSString *tf1;
NSString *tf2;
NSString *tf3;
NSString *results1;
NSString *results2;
NSString *results3;
float *tempResults1;
float *tempResults2;
float *tempResults3;
#property (nonatomic, retain) NSString *tf1;
#property (nonatomic, retain) NSString *tf2;
#property (nonatomic, retain) NSString *tf3;
#property (nonatomic, retain) NSString *results1;
#property (nonatomic, retain) NSString *results2;
#property (nonatomic, retain) NSString *results3;
- (float)getResults1;
- (float)getResults2;
Calculations.m
#synthesize tf1;
#synthesize tf2;
#synthesize tf3;
#synthesize results1;
#synthesize results2;
#synthesize results3;
- (float) getResults1 {
float temp1 = [tf1 floatValue];
float temp2 = [tf2 floatValue];
if (temp1 <= 1 && temp2 >= 3) {
if (temp2 ==10) {tempResults1 = 50;}
else if (temp2 == 11) {tempResults1 = 52;}
etc etc etc ok here is my problem..with the way I have things setup I can carry data from the userInput, threw Calculations and display them in a label on the DataOutput. BUT, when
using the floats in those if statements on calculations.m that I declared in calculations.h... I can't carry the floats (the actual data calculations) over to the DataOutput screen. on DataOutput when I try to set it a float from calculations it doesn't recognize it, it will recognize the NSString but not the float. I have tried converting float tempResults1 to NSString results1 and i keep getting errors. I have tried several different ways to go about doing this from different questions and answers on here but can't figure out why it wont work. can anyone help me with this?
What I want to do is to be able to display results from the calculations on the dataoutput screen.
I know it has to be something simple, maybe I'm doing something wrong or overlooking looking something I didn't do ... I don't know, I could use a little guidance though I know that much.
if (temp1 <= 1 && temp2 >= 3) // this is ok
{
if (temp2 ==10) { // exact floating point comparisons are dangerous,
// and should be avoided. you must rewrite this.
// turn up your compiler warnings
tempResults1 = 50; // this is not what you think it is.
// turn up your compiler warnings. you
// are assigning the address for the
// pointer's value. this will lead to a
// crash after you use it. what exactly
// are you trying to accomplish with this
// statement? this must be rewritten.
}
else if (temp2 == 11) {tempResults1 = 52;} // as above
I created a subclass from UIView
#import <UIKit/UIKit.h>
#interface MeeSelectDropDownView : UIView {
UILabel *mainText;
UIImage *bgImg;
UIImageView *bgView;
UIImageView *originView;
NSMutableArray *labelArray;
int selectedItem;
BOOL inSelectTag;
float _defaultHeight;
}
#property (nonatomic , retain) UIImage *bgImg;
#property (nonatomic , retain) UIImageView *bgView;
#property (nonatomic , retain) NSMutableArray *labelArray;
#property (nonatomic , retain) UIImageView *originView;
#property (nonatomic , retain) UILabel *mainText;
#property (nonatomic , readonly) int selectedItem;
- (void) setViewHeight:(float)aheight;
-(void) showDropList;
-(void) hiddenDropList;
-(void) setStringByArray:(NSArray*)array;
-(void)hiddenLabels
{
for(UILabel *aLabel in labelArray){
[aLabel removeFromSuperview];
}
}
Is it possible to generate an Event from function 'hiddenLabels' to inform and do somethings
Thanks
interdev
It is totally unclear what you are trying to do.
Suggestion: you can consider either posting an NSNotification, or registering observers using KVO (Key Value Observing).