Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从UITableView委托方法访问实例变量_Iphone_Objective C_Cocoa Touch_Uitableview_Delegates - Fatal编程技术网

Iphone 从UITableView委托方法访问实例变量

Iphone 从UITableView委托方法访问实例变量,iphone,objective-c,cocoa-touch,uitableview,delegates,Iphone,Objective C,Cocoa Touch,Uitableview,Delegates,问题: 我正在尝试从UITableViewtableView:DidSelectRowatineXpath:delegate方法访问变量。该变量可以从数据源方法访问,但当我尝试从委托方法(如此方法)访问它时,应用程序崩溃 我在.h文件中声明变量,并在applicationdFinishLaunching方法的.m文件中初始化它。我还没有声明任何访问器/变体 奇怪的是,如果我这样声明变量,问题就不会出现: helloWorld = @"Hello World!"; helloWorld = [N

问题:

我正在尝试从UITableView
tableView:DidSelectRowatineXpath:
delegate方法访问变量。该变量可以从数据源方法访问,但当我尝试从委托方法(如此方法)访问它时,应用程序崩溃

我在.h文件中声明变量,并在
applicationdFinishLaunching
方法的.m文件中初始化它。我还没有声明任何访问器/变体

奇怪的是,如果我这样声明变量,问题就不会出现:

helloWorld = @"Hello World!";
helloWorld = [NSString stringWithFormat: @"Hello World!"];
…但如果我像这样声明变量,它会:

helloWorld = @"Hello World!";
helloWorld = [NSString stringWithFormat: @"Hello World!"];
对这里可能发生的事情有什么想法吗?我错过了什么

完整代码:

UntitledAppDelegate.h:

#import <UIKit/UIKit.h>

@interface UntitledAppDelegate : NSObject <UIApplicationDelegate, UITableViewDelegate, UITableViewDataSource>  {
    UIWindow *window;
    NSString *helloWorld;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

您需要保留
helloWorld
实例变量。试试这个:

helloWorld = [[NSString stringWithFormat: @"Hello World!"] retain];
它在第一个实例中起作用,因为静态字符串被“无限保留”,因此永远不会被释放。在第二种情况下,一旦事件循环运行,就会释放实例变量。保留它将防止这种情况