作为序言,我是 iOS 开发的新手。我环顾四周试图找到答案,无论是在这里还是通过谷歌。
我的应用程序加载到带有注释的地图视图中。如果用户点击其中一个注释,则会显示带有附件按钮的标注视图。我遇到的问题在于点击附件按钮时调用的方法。我想显示特定注释的详细视图,但是当我点击附件按钮时,应用程序会因 SIGABRT 而崩溃。
// method that provides the view for when the callout accessory button is tapped
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
// grabs the annotation's title from the annotation view
NSString *viewTitle = view.annotation.title;
// grabs corresponding POI object for map annotation
PointOfInterest *object = [[PointOfInterest alloc] init];
object = [dictionary objectForKey:(NSString *)viewTitle];
// assigns title and subtitle ivars to variables to be passed into detailviewcontroller init
NSString *title = object._title;
NSString *subtitle = object._subtitle;
UIImage *picture = object._picture;
NSString *description = object._description;
// releases POI object after all the information has been taken from it
[object release];
// inits detailVC
DetailVC *detailVC = [[DetailVC alloc] initWithNibName:@"DetailVC"
bundle:[NSBundle mainBundle]];
// sets the nsstring ivars in the DVC which correspond to the POI information
detailVC.thetitleText = title;
detailVC.thesubtitleText = subtitle;
detailVC.thepictureImage = picture;
detailVC.thedescriptionText = description;
// sets the "back" button on the navigation controller to say "Back to Map"
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back to Map"
style:UIBarButtonItemStyleBordered
target:nil
action: nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
[newBackButton release];
// pushes navcontroller onto the stack and releases the detail viewcontroller
[self.navigationController pushViewController:detailVC animated:YES];
[detailVC release];
}
我还没有添加图片和描述,因为我只是想让视图显示标题和副标题。
这是DetailViewController类代码 Header:
@interface DetailViewController : UIViewController {
IBOutlet UIScrollView *scrollview;
IBOutlet UILabel *thetitle;
NSString *thetitleText;
IBOutlet UILabel *thesubtitle;
IBOutlet UIImage *thepicture;
IBOutlet UITextView *thedescription;
}
@property (nonatomic, retain) UIScrollView *scrollview;
@property (nonatomic, retain) UILabel *thetitle;
@property (nonatomic, retain) NSString *thetitleText;
@property (nonatomic, retain) UILabel *thesubtitle;
@property (nonatomic, retain) UIImage *thepicture;
@property (nonatomic, retain) UITextView *thedescription;
// method that creates the custom detail view with the corresponding information from
// the point of interest objects
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil title:(NSString*)title subtitle:(NSString *)subtitle picture:(UIImage *)picture description:(NSString *)description;
@end
执行:
@implementation DetailViewController
@synthesize scrollview, thetitle, thesubtitle, thepicture, thedescription, thetitleText;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil title:(NSString *)title subtitle:(NSString *)subtitle picture:(UIImage *)picture description:(NSString *)description;
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSLog(@"view initialized");
}
return self;
}
- (void)dealloc
{
[scrollview release];
[thetitle release];
[thesubtitle release];
[thepicture release];
[thedescription release];
[thetitleText release];
[super dealloc];
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"view did load");
[thetitle setText:self.thetitleText];
NSLog(@"thetitle text set");
// Do any additional setup after loading the view from its nib.
// [thetitle setText:title];
// NSLog(@"set title");
// [thesubtitle setText:subtitle];
// NSLog(@"set subtitle");
// thepicture = picture;
// [thedescription setText:description];
// NSLog(@"set description");
}
这是 SIGABRT 输出:
2011-07-12 19:05:06.678 mkeBOAT[1687:ef03] 标注附件已点击
2011-07-12 19:05:06.679 mkeBOAT[1687:ef03] 美国银行大厦
2011-07-12 19:05:06.680 mkeBOAT[1687:ef03](空)
2011-07-12 19:05:06.680 mkeBOAT[1687:ef03] (null), (null)
2011-07-12 19:05:06.680 mkeBOAT[1687:ef03] 视图初始化
2011-07-12 19:05:06.711 mkeBOAT[1687:ef03] *由于未捕获的异常而终止应用程序
'NSUnknownKeyException',原因:'[ setValue:forUndefinedKey:]:此类不符合键描述的键值编码。'
我不认为字典工作正常,因为它为实际上应该有某些东西的值输出 null。
感谢您的任何帮助!