Iphone 将视图控制器推到导航堆栈上会导致SIGABRT
作为前言,我对iOS开发还不熟悉。我已经四处寻找了一段时间,试图找到一个答案,无论是在这里还是通过谷歌 我的应用程序加载到带有注释的mapview中。如果用户点击其中一个注释,将显示带有附件按钮的详图索引视图。我遇到的问题是当点击附件按钮时调用的方法。我想显示特定注释的详细视图,但当我点击附件按钮时,应用程序会与SIGABRT崩溃Iphone 将视图控制器推到导航堆栈上会导致SIGABRT,iphone,mapkit,mkannotationview,sigabrt,accessoryview,Iphone,Mapkit,Mkannotationview,Sigabrt,Accessoryview,作为前言,我对iOS开发还不熟悉。我已经四处寻找了一段时间,试图找到一个答案,无论是在这里还是通过谷歌 我的应用程序加载到带有注释的mapview中。如果用户点击其中一个注释,将显示带有附件按钮的详图索引视图。我遇到的问题是当点击附件按钮时调用的方法。我想显示特定注释的详细视图,但当我点击附件按钮时,应用程序会与SIGABRT崩溃 // method that provides the view for when the callout accessory button is tapped -
// 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类代码
标题:
@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");
}
实施:
@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 MKEBAT[1687:ef03]标注附件
2011-07-12 19:05:06.679 MKEBAT[1687:ef03]美国银行大厦
2011-07-12 19:05:06.680 mkeBOAT[1687:ef03](空)
2011-07-12 19:05:06.680 mkeBOAT[1687:ef03](空)(空)
2011-07-12 19:05:06.680 mkeBOAT[1687:ef03]视图已初始化
2011-07-12 19:05:06.711 mkeBOAT[1687:ef03]*由于未捕获异常而终止应用程序
“NSUnknownKeyException”,原因:“[setValue:forUndefinedKey:]:此类不符合密钥描述的键值编码。”
我不认为字典工作正常,因为它为实际应该有内容的值输出null
谢谢你的帮助 我认为这是因为您的
@属性设置为copy
应该是retain
确保您的XIB中的文件所有者类别设置为DetailViewController。您不应该这样做
[detailViewController autorelease];
将detailViewController推到堆栈之前
您应该首先将其推入导航堆栈,然后释放它,因为您不再需要它,而且导航堆栈现在是detailView控制器对象的所有者
因此,与其这样做
[detailViewController autorelease];
// this is the line that breaks with SIGABRT
[self.navigationController pushViewController:detailViewController animated:YES];
这样做
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
更多信息:
SIGABRT通常发生在IB中有剩余的引用出口,但ur控制器中的对象不再存在时
您应该检查所有引用出口。您的实现中存在一些错误:
1) 不使用固定值调用父方法:
self=[super initWithNibName:@“DetailViewController”捆绑包:nil]
而是使用参数:
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]
2) 标题和子标题属性是IBUILabel,但您使用的是“复制”而不是“保留”,请按“保留”更改“复制”
3) 在init方法中初始化UILabel(title和subtitle)文本,但在此方法中,尚未定义属性(title和subtitle=nil)
您应该改为使用NSString属性,并使用NSString属性设置viewDidLoad方法中UILabel的文本
并对UITextField的Description属性执行相同的操作
别忘了释放dealoc中复制或保留的所有属性,并在viewDidUnload方法中重置IBOutlet属性
关于第3点:
将NSString属性添加到您的DetailViewController(不要忘记在dealloc中合成并发布它):
@属性(非原子,保留)NSString*标题文本
接下来,在pushViewController之前,添加:
detailViewController.TheTitletText=标题
最后,在DetailViewController的viewDidLoad方法中,添加:
[标题设置文本:self.thetitleText]
并对其他标签执行相同操作。重新启动并创建新的详细视图控制器后,我非常确定我的视图链接到了图像,而不是实际视图。更复杂的是,我还认为我的滚动视图真的把事情搞砸了。如果有人想看到真正有效的代码,请告诉我。谢谢大家的贡献!!SOverflow是一个多么伟大的社区啊 仔细阅读代码
您使用以下方法初始化了对象:
PointOfInterest*object=[[PointOfInterest alloc]init]代码>
为什么要这样重新分配对象
:
object=[dictionary objectForKey:viewTitle]代码>
所以,不管在这个问题上,但这里有一个漏洞。另外,我很确定崩溃是由于使用viewttitle
对应的键将object
重新指定给值,稍后尝试向其发送消息,以及您刚刚想到操作类型为PointOfInterest
的对象
希望这有帮助。确保您的视图控制器具有视图。正如问题中所述,我的视图正在轰炸(SIGABRT),因为我在IB中忘记了将视图出口(通常在xib文件中为您创建)连接到控制器的视图。更改后,仍然与SIGABRT崩溃。请尝试检查所有引用出口。通常是sigbrtalso的原因,请尝试执行此操作NSString*viewTitle=view.annotation.title;NSLog(@“%@”,视图标题);可能是尼利,它可以毫无问题地通过这一关。问题似乎是在尝试初始化细节视图时出现的。2011-07-12 18:07:10.954 MKEBAT[433:ef03]调用附件点击2011-07-12 18:07:10.957 MKEBAT[433:ef03]美国银行大厦2011-07-12 18:07:10.957 MKEBAT[433:ef03]进入初始化2011-07-12 18:07:10.957 MKEBAT[433:ef03]设置标题2011-07-12 18:07:07:10.957 MKEBAT[433:ef03]集合字幕2011-07-12 18:07:10.957 mkeBOAT[433:ef03]集合描述2011-07-12 18:07:11.682 mkeBOAT[433:ef03]***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey::此类不符合密钥描述的键值编码。在inspector窗口中,我在哪里