I am trying to use the CalloutAccessory feature, but the delegate method never gets called. I have the delegate properly set up as other mapview delegate methods in my code are firing fine, but for some reason for this one, the delegate method never gets called when the button is tapped.
I have tried extending RouteViewAnnotation from both the MKAnnotationView and the MKPinAnnotationView, and it makes no difference.. the delegate method never gets called.
What am I missing? Do I need something else that isn't here for this to work? The RouteAnnotationView just overrides the drawrect, and has no other code in it.
Relevant Code:
In ViewForAnnotation
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
if([annotation isMemberOfClass:[RouteAnnotation class]])
{
RouteAnnotationView *routeAnnotationView = [[RouteAnnotationView alloc] initWithFrame:(CGRectMake(0,0,100,50))] ;
[routeAnnotationView setBackgroundColor:[UIColor clearColor]];
routeAnnotationView.centerOffset = CGPointMake(0,-25);
routeAnnotationView.canShowCallout = TRUE;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100.0, 40.0)];
[button setTitle:#"Select" forState:UIControlStateNormal];
button.backgroundColor = [UIColor blueColor];
[routeAnnotationView setRightCalloutAccessoryView:button];
return routeAnnotationView;
}
.
.
.
}
In calloutAccssoryControlTapped
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"CALLOUT BUTTON TOUCHED");
}
Okay, I figured it out... I had a tapGestureRecognizer assigned to the MapView (A WildcardGestureRecognizer (https://github.com/OrbJapan/ResizableMKCircleOverlay/blob/master/MapView/WildcardGestureRecognizer.m) to be exact) added to my MapView, and while this had not interfered in any way with annotation touches, or other touches in the MapView or MapView Delegate methods I was using, for some reason this completely interfered with the calloutAccessoryControlTapped Delegate method and it was never called... once I removed this the method called without issue.
Related
I have a UIView that contains a UITextView. The UIView is initiated inside a UIViewController.
But when I touch the UITextView box, nothing happens. Neither the keyboard appears nor delegate methods respond to interaction.
Code:
noteText = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 700, 240)];
noteText.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2].CGColor;
noteText.layer.borderWidth = 2;
[noteText setEditable:YES];
noteText.userInteractionEnabled = YES;
noteText.returnKeyType = UIReturnKeyDone;
noteText.font = [UIFont fontWithName:#"Calibri" size:16];
noteText.textColor = [UIColor blackColor];
[self addSubview:noteText];
Update 1
I removed all other views from the UIViewController, and only put a UITextView in the UIViewController and still not reacting. Neither cursor or keyboard appear.
noteText = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 700, 240)];
noteText.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2].CGColor;
noteText.layer.borderWidth = 2;
[noteText setEditable:YES];
noteText.userInteractionEnabled = YES;
noteText.returnKeyType = UIReturnKeyDone;
noteText.font = [UIFont fontWithName:#"Calibri" size:16];
noteText.textColor = [UIColor blackColor];
[self addSubview:noteText];
// Two suggestions
self.userInteractionEnabled = YES; // superview may blocks touches
self.clipsToBounds = YES; // superview may clips your textfield, you will see it
I found the error.
In one class I categorize the UITextView instead of subclass and set canBecomeFirstResponder to NO.
try this:
in your .h file conform the delegate UITextViewDelegate
:<UITextViewDelegate>
in your .m file:
noteText.delegate = self;
And delegate methods:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
[textView becomeFirstResponder];
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
//resign for exapmple
return YES;
}
Hope this help!
I know it's late, but maybe for someone it'll be helpful. I had the same problem, and it disappeared after I used
[self setSelectable:true];
in my UITextView subclass.
Check this things:
add your viewcontroller on the right UIWindow.
set window makeKeyAndVisible and add rootViewController to window at this method:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
I think here is something wrong in xib file or at appdelegate.
Try overriding - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { inside the UIView to see if it's receiving touches
Just in case, try setting userInteractionEnabled = YES; for your UIView object. If it's somehow set as NO it will trickle down to all of its subviews
Maybe you have some mistake in your overriden -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method? Maybe you do resignFirstResponder there or put some view above all?
Whenever something like this happens check the following items,
Make sure that the Text view is properly retained and released only in dealloc.(If you are not using ARC)
Check the frame of both Text view and it's parent views. Make sure that the subview is fitting exactly inside the parent view. Also make sure that this is the case with all the superviews of text view.
Check whether the delegate is set properly.
If all these are done, try adding a button on top of text view and check if it's target selector is getting called. If yes, the issue is with text view delegate or release statement. Otherwise the issue is with frame setting of Text view or it's superviews.
Ok, so bear with me: as this is an Objective-C related question, there's obviously a lot of code and subclassing. So here's my issue. Right now, I've got an iPad app that programmatically creates a button and two colored UIViews. These colored UIViews are controlled by SubViewControllers, and the entire thing is in a UIView controlled by a MainViewController. (i.e. MainViewController = [UIButton, SubViewController, SubViewController])
Now, all of this happens as it should, and I end up with what I expect (below):
However, when I click the button, and the console shows "flipSubView1", nothing happens. No modal view gets shown, and no errors occur. Just nothing. What I expect is that either subView1 or the entire view will flip horizontally and show subView3. Is there some code that I'm missing that would cause that to happen / is there some bug that I'm overlooking?
viewtoolsAppDelegate.m
#implementation viewtoolsAppDelegate
#synthesize window = _window;
#synthesize mvc;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
mvc = [[MainViewController alloc] initWithFrame:self.window.frame];
[self.window addSubview:mvc.theView];
[self.window makeKeyAndVisible];
return YES;
}
MainViewController.m
#implementation MainViewController
#synthesize theView;
#synthesize subView1, subView2, subView3;
- (id)initWithFrame:(CGRect) frame
{
theView = [[UIView alloc] initWithFrame:frame];
CGRect sV1Rect = CGRectMake(frame.origin.x+44, frame.origin.y, frame.size.width-44, frame.size.height/2);
CGRect sV2Rect = CGRectMake(frame.origin.x+44, frame.origin.y+frame.size.height/2, frame.size.width-44, frame.size.height/2);
subView1 = [[SubViewController alloc] initWithFrame:sV1Rect andColor:[UIColor blueColor]];
subView2 = [[SubViewController alloc] initWithFrame:sV2Rect andColor:[UIColor greenColor]];
subView3 = [[SubViewController alloc] initWithFrame:sV1Rect andColor:[UIColor redColor]];
[theView addSubview:subView1.theView];
[theView addSubview:subView2.theView];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton addTarget:self action:#selector(flipSubView1:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[aButton setFrame:CGRectMake(0, 0, 44, frame.size.height)];
[theView addSubview:aButton];
return self;
}
- (void)flipSubView1:(id) sender
{
NSLog(#"flipSubView1");
[subView3 setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[subView1 presentModalViewController:subView3 animated:YES];
}
SubViewController.m
#implementation SubViewController
#synthesize theView;
- (id)initWithFrame:(CGRect)frame andColor:(UIColor *)color
{
theView = [[UIView alloc] initWithFrame:frame];
theView.backgroundColor = color;
return self;
}
TLDR: modal view not working. should see flip. don't.
It doesn't look like you're setting the 'view' property of the MainViewController anywhere, just 'theView'. The controllers view delegate must be connected to the root view it displays for it to work properly. You'll need to correct that on the Sub View Controller impl as well. If you want all the plumbing that framework classes bring, you have to set things up the way they expect.
Also, you're calling presentModalViewController on one of the sub view controllers; change that to call [self presentModalViewController:...], since the MainViewController is the one which will 'own' the modal view.
I think if you fix those points, you'll find -presentModalViewController will work.
I am creating a subclass of UIButton in order to create my own customized buttons. My code as follows:
//interface file (subclass of uIButton
#interface UICustomButton : UIButton
{
Answer *answer;
NSString *btnType;
}
#property (nonatomic, retain) Answer *answer;
#property (nonatomic, assign) NSString *btnType;
- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame;
- (void)buttonPressed;
#end
//Implementation file (.m)
#implementation UICustomButton
#synthesize answer,btnType;
- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame;
{
self = [super initWithFrame:frame];
if (self)
{
self = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
self.backgroundColor = [UIColor colorWithHexString:#"#E2E4E7"];
}
[self addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlStateNormal];
self.answer = ans;
self.btnType = type;
return self;
}
I am facing some issues in getting the above code to work. I have 2 problems
1) The buttons are not responding to the selector method "buttonPressed"
2) I am hitting a runtime error for the lines 'self.answer = ans' and 'self.btnType = type' Stack trace as follows:
-[UIButton setAnswer:]: unrecognized selector sent to instance 0x614ebc0
2011-06-23 00:55:27.038 onethingaday[97355:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setAnswer:]: unrecognized selector sent to instance 0x614ebc0'
What am I doing wrong here?
This is happening because you are creating a UIButton type object and not a UICustomButton type inside the init method when you do
self = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
Try replacing your init method for
- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame;
{
self = [self initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
if (self)
{
self.backgroundColor = [UIColor colorWithHexString:#"#E2E4E7"];
[self addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
self.answer = ans;
self.btnType = type;
}
return self;
}
This will cause self to be a UICustomButton type object.
Also, you are using a wrong type for the UIControlState parameter when you add the target to your button using the addTarget:action:forControlEvents: method
You should use value among the ones bellow:
UIControlEventTouchDown
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
UIControlEventTouchCancel
EDIT:
Notes on UIButton subclassing
Many references on the web say you should NOT subclass the UIButton class, but not only anybody said why but what also deeply annoyed me was that the UIButton Class Reference does not say anything about it at all.
If you take UIWebView Class Reference for example, it explicitly states that you should not subclass UIWebView
Subclassing Notes The UIWebView class
should not be subclassed.
the big deal with UIButton is that it inherits from UIControl and a good and simple explanation is on the UIControl Class Reference itself
Subclassing Notes You may want to
extend a UIControl subclass for either
of two reasons:
To observe or modify the dispatch of
action messages to targets for
particular events
To provide custom
tracking behavior (for example, to
change the highlight appearance)
So, this means that you CAN subclass a UIButton, but you should be careful on what you are doing. Just subclass it to change its behavior and not its appearance. To modify a UIButton appearance you should use the interface methods provided for that, such as:
setTitle:forState:
setBackgroundImage:forState:
setImage:forState:
References worth reading
The UIView Programming Guide: View and Window Architecture -> Tips for Using Views Effectively -> Do Not Customize Controls by Embedding Subviews
Source: my post here
Not sure this was in the docs before, but anyway these are the current notes on + (id)buttonWithType:(UIButtonType)buttonType...
To me it looks like subclassing is OK as long as you use init instead of buttonWithType. I have yet to try it myself however.
Discussion This method is a convenience constructor for creating
button objects with specific configurations. It you subclass UIButton,
this method does not return an instance of your subclass. If you want
to create an instance of a specific subclass, you must alloc/init the
button directly.
When creating a custom button—that is a button with the type
UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0)
initially. Before adding the button to your interface, you should
update the frame to a more appropriate value.
If you want to get notifications when the user is interacting with your buttons, just sublcass UIButton and implement these methods:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesBegan");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesEnded");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesCancelled");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesMoved");
}
No init method required.
Edit
This answer reaches back several years, and things have changed - as Apple docs now explicitly mention subclassing and gives some hints.
So the following answer might be irrelevant or wrong for current development and might be ignored if you're interested in the current state of the art.
UIButton is not meant to be subclassed.
You are better off making a category and defining a factory method that delivers your needed button (with proper call to buttonWithType:). initWithFrame: is not the correct way to initialize a button anyway.
//
// BtnClass.m
#import "BtnClass.h"
#implementation BtnClass
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
//added custum properities to button
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(#"initWithCoder");
self = [super initWithCoder: aDecoder];
if (self) {
// Initialization code
_numberOfItems=[[UILabel alloc]initWithFrame:CGRectMake(40, 8, 160, 30)];
_numberOfItems.textAlignment=NSTextAlignmentLeft;
_numberOfItems.font = [UIFont boldSystemFontOfSize:18.0];
_numberOfItems.textColor = [UIColor darkGrayColor];
[self addSubview:_numberOfItems];
_leftImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 25, 25)];
[self addSubview:_leftImage];
_rightImage=[[UIImageView alloc]initWithFrame:CGRectMake(280, 10, 15, 15)];
[self addSubview:_rightImage];
[self setImage:[UIImage imageNamed:#"list-bg2-1.png"] forState:UIControlStateNormal];
[_rightImage setImage:[UIImage imageNamed:#"carat.png"]];
self.backgroundColor=[UIColor blackColor];
if(self.tag==1)
{
[_leftImage setImage:[UIImage imageNamed:#"notes-icon.png"]];
}
if(self.tag==2)
{
[_leftImage setImage:[UIImage imageNamed:#"photos-icon.png"]];
}
if(self.tag==3)
{
[_leftImage setImage:[UIImage imageNamed:#"videos-icon.png"]];
}
}
return self;
}
//selected method of uibutton
-(void)setSelected:(BOOL)selected
{
[super setSelected:selected];
if(selected)
{
[self setImage:nil forState:UIControlStateNormal];
_numberOfItems.textColor = [UIColor whiteColor];
[_rightImage setImage:[UIImage imageNamed:#"carat-open.png"]];
if(self.tag==1)
{
[_leftImage setImage:[UIImage imageNamed:#"white-notes-icon.png"]];
}
else if(self.tag==2)
{
[_leftImage setImage:[UIImage imageNamed:#"white-photo-icon.png"]];
}
else
{
[_leftImage setImage:[UIImage imageNamed:#"white-video-icon.png"]];
}
}
else{
_numberOfItems.textColor = [UIColor darkGrayColor];
if(self.tag==1)
{
[_leftImage setImage:[UIImage imageNamed:#"notes-icon.png"]];
}
if(self.tag==2)
{
[_leftImage setImage:[UIImage imageNamed:#"photos-icon.png"]];
}
if(self.tag==3)
{
[_leftImage setImage:[UIImage imageNamed:#"videos-icon.png"]];
}
[self setImage:[UIImage imageNamed:#"list-bg2-1.png"] forState:UIControlStateNormal];
[_rightImage setImage:[UIImage imageNamed:#"carat.png"]];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
I've been playing around with the MKMapView and trying get my head around how the MKMapViewDelegate system works. So far I have no luck in getting the didAddAnnotationViews to get called when the current location marker is added.
I have set my app delegate to implement MKMapViewDelegate, I have an Outlet to the MapView in my xib and have set the delegate property of the MapView to be self, as in the app delegate instance. I have implemented didAddAnnotationViews in the app delegate which I simply NSLog any calls to it as shown below. The map is set to show current location which it does and adds the blue pin annotation on startup, but for some reason didAddAnnotationViews is not being hit.
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
NSLog(#"Annotation added!");
}
Any ideas what I might have missed?
I came across the same issue in BNR. Here is what I ended up using:
// Tell MKMapView to zoom to current location when found
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(#"didUpdateUserLocation just got called!");
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([userLocation coordinate], 250, 250);
[mapView setRegion:region animated:YES];
}
mapView:didAddAnnotations: is only called in response to addAnnotation: or addAnnotations:. The users location pin will not trigger this delegate method.
Just wanted to confirm that I was able to get this working using
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 250,250);
[mv setRegion:region animated:YES];
}
Make sure you are using
mapView.delegate = self;
or
[mapView setDelegate:self];
I am trying to make a map, where I can see my current location, and see what the street is called.
so far, I am able to put a pin on my map, but for some reason, I am not getting the callout.
and I have put a NSLog in my viewForAnnotation method, but it is not being called, so i wasn't able to test it.
can someone help me?
-(void)lat:(float)lat lon:(float)lon
{
CLLocationCoordinate2D location;
location.latitude = lat;
location.longitude = lon;
NSLog(#"Latitude: %f, Longitude: %f",location.latitude, location.longitude);
//One location is obtained.. just zoom to that location
MKCoordinateRegion region;
region.center=location;
//Set Zoom level using Span
MKCoordinateSpan span;
span.latitudeDelta=.005f;
span.longitudeDelta=.005f;
region.span=span;
[map setRegion:region animated:TRUE];
//MKReverseGeocoder *geocoder=[[MKReverseGeocoder alloc] initWithCoordinate:location];
//geocoder.delegate=self;
//[geocoder start];
if (cPlacemark != nil) {
[map removeAnnotation:cPlacemark];
}
cPlacemark=[[CustomPlacemark alloc] initWithCoordinate:location];
cPlacemark.title = mPlacemark.thoroughfare;
cPlacemark.subtitle = mPlacemark.locality;
[map addAnnotation:cPlacemark];
[cPlacemark release];
[mLocationManager stopUpdatingLocation];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// try to dequeue an existing pin view first
if ([annotation isKindOfClass:[CustomPlacemark class]]){
MKPinAnnotationView *pinView=(MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:#"customIdentifier"];
if (!pinView)
{
// if an existing pin view was not available, create one
MKPinAnnotationView* cPinAnnoView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:#"customIdentifier"] autorelease];
cPinAnnoView.pinColor = MKPinAnnotationColorPurple;
cPinAnnoView.animatesDrop = YES;
cPinAnnoView.canShowCallout = YES;
// Add button
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[leftButton addTarget:self action:#selector(annotationViewClick:) forControlEvents:UIControlEventTouchUpInside];
cPinAnnoView.leftCalloutAccessoryView = leftButton;
} else
{
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
Right now I have customized my viewForAnnotation to be like this.
But I still can't get a callout from my pin and the pin remains red.
But it should be purple of nothing at all
I had the same problem which was not setting the MapView delegate to the File Owner.
Open your nib
Right click on the MapView
Drag the delegate to the File's Owner
I had the same problem, as you mentioned. The delegate had been set to ViewController, but the viewForAnnotation selector was not being called. After some checks, I realized if you do not call addAnotation in the main thread, mapView would not call viewForAnnotation, so following update resolved my problem:
Before:
[_mMapView addAnnotation:marker];
After:
dispatch_async(dispatch_get_main_queue(), ^{
[_mMapView addAnnotation:marker];
});
In order to get the viewForAnnotation to be called, add mapView.delegate=self; to e.g. the viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
mapView.delegate=self;
}
Could it be that your annotation has been added outside the current view area of the MKMapView?
For storyboard, Ctl drag the MKMapView to the orange circle on the bottom bar of ViewController, and select delegate.
This will solve the problem.
As vatrif mentioned in the comments, you must set your delegate BEFORE adding annotations to your MKMapView object.
Others have already explained, odds are high you have not connected your mapview delegate to your controller. Its the first thing to check
i have been working in ios 9 Mapview related app and I experienced the same problem.
somehow I solved my problem, in my case im resizing the mapview.
I added delegate after i resize the mapview. it works now perfectly.!
After having set the delegate for the mapview if still the viewforannotation not getting called then this is something which you have missed - set the self.mapView.showsUserLocation to YES, in interface builder you can tick the shows userLocation option in attributes inspector.