更改视图控制器时单例会导致应用程序崩溃
我正在尝试使用单例类来根据用户所做的选择来选择要显示的自定义内容。它是这样的:显示一个列表,用户选择列表中的一行,应用程序进入另一个 ViewController 视图。所有列表选项使用的 ViewController 都是相同的,但内容不同。目前,我设法仅针对 1 个选项执行此操作,并尝试使用单例类来告诉应用程序要选择哪些内容。
这就是选择“Raffles Landing Site”选项时发生的情况:
if(landmarkSelected== @"Raffles Landing Site") {
RafflesLandmarkInfo *rafflesLandmarkInfo = [[RafflesLandmarkInfo alloc] initWithNibName:@"RafflesLandmarkInfo" bundle:nil];
[self.navigationController pushViewController:rafflesLandmarkInfo animated:YES];
[rafflesLandmarkInfo release];
这将打开一个 UIWebView,其实现如下:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"raffles" ofType:@"html"]isDirectory:NO]]];
我创建了一个单例类,如下所述: http://www.galloway.me.uk/tutorials/singleton-classes/
我向其中添加了一个 NSMutableString 属性并更改了之前的代码如下:
if(landmarkSelected== @"Raffles Landing Site") {
LandmarkController* instance = [LandmarkController sharedInstance];
[instance.activeLandmark setString:@"raffles"];
RafflesLandmarkInfo *rafflesLandmarkInfo = [[RafflesLandmarkInfo alloc] initWithNibName:@"RafflesLandmarkInfo" bundle:nil];
[self.navigationController pushViewController:rafflesLandmarkInfo animated:YES];
[rafflesLandmarkInfo release];
但是
if (instance.activeLandmark ==@"raffles"){
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:instance.activeLandmark ofType:@"html"]isDirectory:NO]]];
}
当我从选项列表中选择 Raffles Landing Site 时,应用程序崩溃了。罪魁祸首似乎是
[instance.activeLandmark setString:@"raffles"];
如何在第一个 ViewController 中设置 activeLandmark 字符串,以便在加载第二个 ViewController 时它根据第一个 ViewController 中设置的值显示内容?
I am trying to use a singleton class to choose custom content to display based on the selection made by the user. It goes like this: a list is displayed, users select one of the rows in the list, and the app goes into another ViewController view. The ViewController used is the same for all the list options, however the content is different. Currently I managed to do this for only 1 option, and am trying to use a singleton class to tell the app which content to choose from.
This is what happens when the option "Raffles Landing Site" is chosen:
if(landmarkSelected== @"Raffles Landing Site") {
RafflesLandmarkInfo *rafflesLandmarkInfo = [[RafflesLandmarkInfo alloc] initWithNibName:@"RafflesLandmarkInfo" bundle:nil];
[self.navigationController pushViewController:rafflesLandmarkInfo animated:YES];
[rafflesLandmarkInfo release];
This opens a UIWebView implemented as follows:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"raffles" ofType:@"html"]isDirectory:NO]]];
I created a singleton class as described here: http://www.galloway.me.uk/tutorials/singleton-classes/
I added an NSMutableString property to it and changed the previous codes to the following:
if(landmarkSelected== @"Raffles Landing Site") {
LandmarkController* instance = [LandmarkController sharedInstance];
[instance.activeLandmark setString:@"raffles"];
RafflesLandmarkInfo *rafflesLandmarkInfo = [[RafflesLandmarkInfo alloc] initWithNibName:@"RafflesLandmarkInfo" bundle:nil];
[self.navigationController pushViewController:rafflesLandmarkInfo animated:YES];
[rafflesLandmarkInfo release];
and
if (instance.activeLandmark ==@"raffles"){
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:instance.activeLandmark ofType:@"html"]isDirectory:NO]]];
}
but the app crashes when I select Raffles Landing Site from the list of options. The culprit seems to be
[instance.activeLandmark setString:@"raffles"];
How do I set the activeLandmark string in the first ViewController so when it loads the second ViewController it displays content based on the value set in the first ViewController?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的单例中,在尝试分配给它之前,是否已分配/初始化 activeLandmark 字符串?
In your singleton, is the activeLandmark string being alloced/initialized before you try and assign to it?