问题:
我正在尝试从UITableView访问变量
tableView:didSelectRowAtIndexPath:
applicationDidFinishLaunching
奇怪的是,如果我这样声明变量,问题就不会出现:
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
#import "UntitledAppDelegate.h"
@implementation UntitledAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
helloWorld = [NSString stringWithFormat: @"Hello World!"];
NSLog(@"helloWorld: %@", helloWorld); // As expected, logs "Hello World!" to console.
[window makeKeyAndVisible];
}
- (void)dealloc {
[window release];
[super dealloc];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
cell.textLabel.text = @"Row";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"helloWorld: %@", helloWorld); // App crashes
}
@end