[英]Singleton causes app to crash when changing View Controllers
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.
就像这样:显示一个列表,用户选择列表中的行之一,然后该应用程序进入另一个ViewController视图。 The ViewController used is the same for all the list options, however the content is different.
所有列表选项使用的ViewController相同,但是内容不同。 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.
目前,我仅通过1个选项就做到了这一点,并且正在尝试使用单例类来告诉应用程序要选择哪些内容。
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: 这将打开一个实现如下的UIWebView:
[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/ 我创建了一个单例课程,如下所述: http : //www.galloway.me.uk/tutorials/singleton-classes/
I added an NSMutableString property to it and changed the previous codes to the following: 我向其添加了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];
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? 如何在第一个ViewController中设置activeLandmark字符串,以便在加载第二个ViewController时根据第一个ViewController中设置的值显示内容?
在您的单例中,是否在尝试分配ActiveLandmark字符串之前对其进行了分配/初始化?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.