I want to navigate to next class through tap on MKAnnotationView on MKMapView. How can I do this plz give some sample code for this.
I had done this code..explained below..
- (void)mapView:(MKMapView *)mapView1 didSelectAnnotationView:(MKAnnotationView *)customAnnotationView
{
//Here Push to another view controller
}
One problem is that when clicking on one item, this method calls and It will navigate to other class but when I come back and again tap on the same item this method not called.
Please help me to solve me this problem...
Thanks in advance...
you need to implement this method:-
edited:-
you need to implement following method.
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
NSLog(#"touch");
[mapView deselectAnnotation:view animated:NO];
firstVC *aa=[[[firstVC alloc]initWithNibName:#"firstVC" bundle:nil] autorelease];
[[self navigationController] pushViewController:aa animated:YES];
}
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
NSLog(#"notouch");
firstVC *aa=[[[firstVC alloc]initWithNibName:#"firstVC" bundle:nil] autorelease];
[[self navigationController] pushViewController:aa animated:YES];
}
previous:-
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
CompanyDetail *detail = [[CompanyDetail alloc] initWithNibName:#"CompanyDetail" bundle:nil];
[self.navigationController pushViewController:detail animated:YES];
}
Make sure you do
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
in your
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation method
Related
I would like to have another view loaded when I tap on the disclosure button in my map callout.
I added the button to the callout using the following in the initWithAnnotation method of my AnnotationView implementation file (I am only showing the relevant code):
- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
self.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
I also added the following controllTapped method to my MapViewController implementation file for detecting when the disclosure button has been tapped:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
DetailViewController *dvc = [[DetailViewController alloc] init];
[self.navigationController pushViewController:dvc animated:YES];
}
I also created a new detailedDisclosure class with an .XIB for a UI. This is the view I want to load when the user taps the disclosure button.
Why, then, does nothing happen when the user taps the disclosure button?
UIButton *btnGo = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[btnGo addTarget:self action:#selector(goToLocation:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView=btnGo;
-(void) goToLocation:(UIButton *) sender
{
}
You have to use MKMapView delegate method for this purpose like following, for identifying your CustomAnnotationClass:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
id <MKAnnotation> annotation = [view annotation];
if ([annotation isKindOfClass:[CustomAnnotationClass class]])
{
DetailViewController *dvc = [[DetailViewController alloc] init];
[self.navigationController pushViewController:dvc animated:YES];
}
}
Let me kno, If you get it to work.
i have added a detail button on my pin annotation. i want to go to another view. when i click on it, following code doesn't work. i don't know where is the problem.
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
if(annotation != mapview.userLocation)
{
static NSString *defaultPinID = #"myPin";
pinAnnotation = (MKPinAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinAnnotation == nil )
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinAnnotation.canShowCallout = YES;
//instatiate a detail-disclosure button and set it to appear on right side of annotation
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinAnnotation.rightCalloutAccessoryView = infoButton;
}
return pinAnnotation;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
if ([(UIButton*)control buttonType] == UIButtonTypeDetailDisclosure){
annotationViewController *detailView=[[annotationViewController alloc] initWithNibName:#"annotationViewController" bundle:nil];
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
}
The method - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control is a delegate method. That's why you need to set your mapview's delegate to the class that implements this method (here self), in order for it to be called.
You also need to tell the class that you're implementing the MKMapViewDelegate methods. To do so, in you .h, change this line :
#interface MyViewController : UIViewController
To :
#interface MyViewController : UIViewController <MKMapViewDelegate>
Your method should get called now.
I have a map with only one annotation.I created a simple class which I want it to show when the user clicks on the annotation.The problem is that when I click on the annotation nothing happens.
Here is my code:
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
NSLog(#"Reverse Geocoder completed");
mPlacemark=placemark;
[mapView addAnnotation:placemark];
}
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentloc"];
annView.animatesDrop=TRUE;
//create UIButton for annotation
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//NSInteger annotationValue = [self.annotations indexOfObject:annotation];
[detailButton addTarget:self action:#selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView=detailButton;
return annView;
}
-(void)showDetailView:(id)sender{
NSLog("inside the stupid method");
MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:#"MyDetailViewController" bundle:nil];
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
My showDetailView function never gets called.Please help me.I'm new to iphone and I might forget a simple thing.Thanks
Still not working!!!!
First, check that the map view's delegate is set otherwise your viewForAnnotation method will not get called.
Next, the accessory button appears on the annotation's callout which will only appear if you set canShowCallout:
annView.canShowCallout = YES;
Next, instead of using your own method to handle the button action, it's much better to use the map view's own calloutAccessoryControlTapped delegate method which gets called when tapping an accessory:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"inside the stupid method");
//Here, the annotation tapped can be accessed using view.annotation
}
Remove the [detailButton addTarget... line from viewForAnnotation.
Also note your NSLog in the showDetailView method is missing the leading # which will result in a crash when the method does get called.
Another thing is you should use dequeueReusableAnnotationViewWithIdentifier in viewForAnnotation to enable annotation view re-use.
Example as requested:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
static NSString *annReuseId = #"currentloc";
MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annReuseId];
if (annView == nil)
{
annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annReuseId];
annView.animatesDrop = YES;
annView.canShowCallout = YES;
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.rightCalloutAccessoryView=detailButton;
}
else {
annView.annotation = annotation;
}
return annView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"calloutAccessoryControlTapped: annotation = %#", view.annotation);
MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:#"MyDetailViewController" bundle:nil];
//here, can set annotation info in some property of detailView
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
Ok. If you are using storyboards, you need to create a segue between the map view controller and then where the below code exists:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"calloutAccessoryControlTapped: annotation = %#", view.annotation);
MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:#"MyDetailViewController" bundle:nil];
//here, can set annotation info in some property of detailView
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
Use the following code instead:
NSLog(#"calloutAccessoryControlTapped: annotation = %#", view.annotation);
[self performSegueWithIdentifier:#"DetailViewController" sender:self];
This will give you the movement to the detailed view.
write your function like this:
-(IBAction)showDetailView:(id)sender{
NSLog("inside the stupid method");
MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:#"MyDetailViewController" bundle:nil];
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
you have to set the map delegate to self like this,
mapview.delegate=self; (if your calling the method in same page)
if you don't assign the delegate in your code the respective methods won't be called, so check if you have the above mentioned line.
Replace -(IBAction)showDetailView:(UIView*)sender with following...
-(void)showDetailView:(id)sender
In my application I am using mapkit. All the time I have more than one annoatations on the map. In the callout i have placed the detailDisclosureButton. On the click of this button I want to load new viewController. How to do this?
Thanks in advance.
In - (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id )annotation delegate add a disclosure button as -
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:#selector(infoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = infoButton;
and in its action method -
-(IBAction) infoButtonPressed:(id) sender
{
DetailViewController *dvc = [[DetailViewController alloc] init];
[self.navigationController pushViewController:dvc animated:YES];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation
{
//Some code here
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return pinView;
}
And make use of the following delegate method to get the button action
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
// code to show details view
}
I would check here: Modal View Controller Reference to see how to move from one view to another. When the detailDisclosureButton is pressed, set up the viewController you want to move to, and use the methods described in the link. Hope that helps!
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped: (UIControl *)control{
NSLog(#"Disclosure button pressed");
DetailView *tmpView = [[DetailView alloc]initWithNibName:#"DetailView" bundle:nil];
self.detailViewController = tmpView;
[self.navigationController pushViewController:self.detailViewController animated:YES];
//[self.view addSubview:detailViewController.view];
[tmpView release];
}
Are you sure the navigation controller exists in current view controller from where you click on the map annotation? Make sure self.navigationController is not nil.