Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-27-2011, 11:40 AM   #1 (permalink)
Software Engineer
 
Join Date: Jun 2010
Location: London, United Kingdom.
Posts: 5
rhystowey is on a distinguished road
Send a message via Skype™ to rhystowey
Default Custom annotation/callout button no action

Hey, I am new to iPhone development and am having trouble assigning a action to a custom annotation. I want to have multiple annotations/callouts with buttons to show different information in a Detail View. The pins show with the buttons but don't do anything. Here is the code I have:

mapkitDemoAppDelegate.h
Code:
@interface mapkitDemoAppDelegate : NSObject <UIApplicationDelegate> {
    
    UIWindow *window;
    UITabBarController *navigationController;	
	IBOutlet UITabBarController*  myTabBarController;
	
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *navigationController;

@end
mapkitDemoAppDelegate.m
Code:
#import "mapkitDemoAppDelegate.h"



@implementation mapkitDemoAppDelegate

@synthesize window;
@synthesize navigationController;


#pragma mark -
#pragma mark Application lifecycle

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    
    // Override point for customization after app launch    
	
	[window addSubview:[myTabBarController view]];
    [window makeKeyAndVisible];
	
}


- (void)applicationWillTerminate:(UIApplication *)application {
	// Save data if appropriate
}


#pragma mark -
#pragma mark Memory management

- (void)dealloc {
	[navigationController release];
	[window release];
	[super dealloc];
}

@end
mapViewController.h
Code:
#import "mapkitDemoAppDelegate.h"


@implementation mapkitDemoAppDelegate

@synthesize window;
@synthesize navigationController;


#pragma mark -
#pragma mark Application lifecycle

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    
    // Override point for customization after app launch    
	
	[window addSubview:[myTabBarController view]];
    [window makeKeyAndVisible];
	
}


- (void)applicationWillTerminate:(UIApplication *)application {
	// Save data if appropriate
}


#pragma mark -
#pragma mark Memory management

- (void)dealloc {
	[navigationController release];
	[window release];
	[super dealloc];
}


@end
mapViewController.m
Code:
#import "mapViewController.h"
#import "mapkitDemoAppDelegate.h"
#import <CoreLocation/CoreLocation.h>


@implementation GPSAnnotation

@synthesize coordinate;
@synthesize mTitle;
@synthesize mSubTitle;
@synthesize currentPoint;
- (NSString *)subtitle{
	return mSubTitle;
}
- (NSString *)title{
	return mTitle;
}


-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
	coordinate=c;
	NSLog(@"%f,%f",c.latitude,c.longitude);
	return self;
}

@end


@implementation mapViewController
@synthesize showDetailView;


/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 // Custom initialization
 }
 return self;
 }
 */

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {
 }
 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];
	mapView=[[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
	mapView.showsUserLocation=TRUE;
	mapView.mapType=MKMapTypeSatellite;
	mapView.delegate=self;
	
	MKCoordinateRegion region;
	MKCoordinateSpan span;
	span.latitudeDelta=0.00399;
	span.longitudeDelta=0.00399;
	
	CLLocationManager *locationManager=[[CLLocationManager alloc] init];
	locationManager.delegate=self;
	locationManager.desiredAccuracy=kCLLocationAccuracyBest; //kCLLocationAccuracyNearestTenMeters;
	[locationManager startUpdatingLocation]; 
	
	CLLocationCoordinate2D location2 = mapView.userLocation.coordinate;
	
	location2.latitude= 53.7905;
	location2.longitude= -3.0557;
	region.span=span;
	region.center=location2;
	[mapView setRegion:region animated:TRUE];
	[mapView regionThatFits:region];
	
	
	[self.view insertSubview:mapView atIndex:0];
	
	CLLocationCoordinate2D c;
	NSString *latitude =  @"<latitude>";
	NSString *longitude = @"<longitude>";
	c.latitude = [latitude doubleValue];
	c.longitude = [longitude doubleValue];
	GPSAnnotation *annotation = [[GPSAnnotation alloc] initWithCoordinate:c];
	annotation.currentPoint = [NSNumber numberWithInt:1];
	annotation.mTitle=@"Pin 1";
	annotation.mSubTitle = @"...";
	[mapView addAnnotation:annotation];
	[annotation release];
	
	latitude =  @"<latitude>";
	longitude = @"<longitude>";
	c.latitude = [latitude doubleValue];
	c.longitude = [longitude doubleValue];
	annotation = [[GPSAnnotation alloc] initWithCoordinate:c];
	annotation.currentPoint = [NSNumber numberWithInt:2];
	annotation.mTitle=@"Pin 2";
	annotation.mSubTitle = @"...";
	[mapView addAnnotation:annotation];
	[annotation release];
	
	latitude =  @"<latitude>";
	longitude = @"<longitude>";
	c.latitude = [latitude doubleValue];
	c.longitude = [longitude doubleValue];
	annotation = [[GPSAnnotation alloc] initWithCoordinate:c];
	annotation.currentPoint = [NSNumber numberWithInt:2];
	annotation.mTitle=@"Pin 3";
	annotation.mSubTitle = @"...";
	[mapView addAnnotation:annotation];
	[annotation release];
	
	
}

# pragma mark Personalized annotation

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(GPSAnnotation *) annotation{
	int postag = 0;
	
	MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
	annView.pinColor = MKPinAnnotationColorRed;
	UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
	myDetailButton.frame = CGRectMake(0, 0, 23, 23);
	myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
	myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
	
	// Set the image for the button
	[myDetailButton setImage:[UIImage imageNamed:@"button_right.png"] forState:UIControlStateNormal];
	[myDetailButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside]; 
	
	if ([[annotation title] isEqualToString:@"Current Location"]) {
		postag = 99999;
	} else {
		postag = [annotation.currentPoint intValue];
	} 
	myDetailButton.tag  = postag;
	
	
	// Set the button as the callout view
	annView.rightCalloutAccessoryView = myDetailButton;
	
	annView.animatesDrop=TRUE;
	annView.canShowCallout = YES;
	annView.calloutOffset = CGPointMake(-5, 5);
	return annView;
}

- (IBAction)showLinks:(id)sender
{
	int nrButtonPressed = ((UIButton *)sender).tag;
	
	if (nrButtonPressed < 99999) {
		if (self.showDetailView == nil) {
			detailView *tmpViewController = [[detailView alloc] initWithNibName:@"detailView" bundle:nil];
			self.showDetailView = tmpViewController;
			[tmpViewController release];
		}
		[self.navigationController pushViewController:self.showDetailView animated:YES];
		
		if (nrButtonPressed == 1) {
			[self.showDetailView.detailDescription  setText:@"Pin 1 - description"];
			self.showDetailView.title = @"Pin 1";
			[self.showDetailView.detailHeight  setText:@"44 Inches/112cm"];
			[self.showDetailView.detailWristband  setText:@"Unlimited & Junior"];
			[self.showDetailView.detailImage setImage:[UIImage imageNamed:@"imagename1.png"]];		
		}
		if (nrButtonPressed == 2) {
			[self.showDetailView.detailDescription  setText:@"Pin 2 - description"];
			self.showDetailView.title = @"Pin 2";	
			[self.showDetailView.detailHeight  setText:@"52 Inches/132cm"];
			[self.showDetailView.detailWristband  setText:@"Unlimited & Junior"];
			[self.showDetailView.detailImage setImage:[UIImage imageNamed:@"imagename2.png"]];		
	    }
		if (nrButtonPressed == 3) {
			[self.showDetailView.detailDescription  setText:@"Pin 3 - description"];
			self.showDetailView.title = @"Pin 3";	
			[self.showDetailView.detailHeight  setText:@"46 Inches/117cm"];
			[self.showDetailView.detailWristband  setText:@"Unlimited & Junior"];
			[self.showDetailView.detailImage setImage:[UIImage imageNamed:@"imagename3.png"]];		
	    }
		
	}	
}
#pragma mark Navigation With GPS (must activate [locationManager startUpdatingLocation]; in viewDidLoad)

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
	
	location=newLocation.coordinate;
	//One location is obtained.. just zoom to that location
	
	MKCoordinateRegion region;
	region.center=location;
	//Set Zoom level using Span
	MKCoordinateSpan span;
	span.latitudeDelta=.005;
	span.longitudeDelta=.005;
	region.span=span;
	
	[mapView setRegion:region animated:TRUE];
}

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */
- (void)viewDidUnload {
    // Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	
	[super viewDidUnload];
}

#pragma mark -
#pragma mark CLLocationManagerDelegate Methods

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

/*- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
 }
 
 - (void)dealloc {
 [super dealloc];
 }
 */
@end
Please help fix my problem and just to remind you, I am new at iPhone developing so I'm a little slow. Thank you.
rhystowey is offline   Reply With Quote
Reply

Bookmarks

Tags
annotation callouts, button, mapkit, pin

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 370
18 members and 352 guests
Absentia, akphyo, apatsufas, BinHex, cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, MarkC, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:17 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0