更改视图控制器时,单例会导致应用程序崩溃 [英] Singleton causes app to crash when changing View Controllers

查看:24
本文介绍了更改视图控制器时,单例会导致应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用单例类根据用户所做的选择来选择要显示的自定义内容.它是这样的:显示一个列表,用户选择列表中的一行,然后应用程序进入另一个 ViewController 视图.所有列表选项使用的 ViewController 都相同,但内容不同.目前,我设法仅针对 1 个选项执行此操作,并且正在尝试使用单例类来告诉应用程序要选择哪些内容.

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];

这会打开一个实现如下的 UIWebView:

This opens a UIWebView implemented as follows:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"raffles" ofType:@"html"]isDirectory:NO]]];

我创建了一个单例类,如下所述:http://www.galloway.me.uk/tutorials/singleton-classes/

I created a singleton class as described here: http://www.galloway.me.uk/tutorials/singleton-classes/

我向它添加了一个 NSMutableString 属性并将之前的代码更改为以下内容:

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];

 if (instance.activeLandmark ==@"raffles"){
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:instance.activeLandmark ofType:@"html"]isDirectory:NO]]];
}

但是当我从选项列表中选择 Raffles Landing Site 时应用程序崩溃了.罪魁祸首似乎是

but the app crashes when I select Raffles Landing Site from the list of options. The culprit seems to be

[instance.activeLandmark setString:@"raffles"];

如何在第一个 ViewController 中设置 activeLandmark 字符串,以便在加载第二个 ViewController 时根据第一个 ViewController 中设置的值显示内容?

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?

推荐答案

在你的单例中,activeLandmark 字符串在你尝试分配之前是否被分配/初始化?

In your singleton, is the activeLandmark string being alloced/initialized before you try and assign to it?

这篇关于更改视图控制器时,单例会导致应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆