Question:Accès aux variables d'instance de méthodes de délégué UITableView
J'essaie d'accéder à une variable de la méthode délégué UITableView tableView:didSelectRowAtIndexPath:
. La variable est accessible à partir des méthodes de source de données, mais lorsque j'essaie d'y accéder à partir de méthodes de délégation comme celle-ci, l'application se bloque.
Je déclare la variable dans mon fichier .h et l'initialise dans le fichier .m dans la méthode applicationDidFinishLaunching
. Je n'ai pas déclaré d'accesseurs/mutateurs.
La chose étrange est que le problème ne se produit pas si je déclare la variable comme ceci:
helloWorld = @"Hello World!";
... mais si je ne déclare la variable comme ceci:
helloWorld = [NSString stringWithFormat: @"Hello World!"];
Des idées sur ce qui se passe ici? Qu'est-ce que je rate?
code complet:
UntitledAppDelegate.h:
#import <UIKit/UIKit.h>
@interface UntitledAppDelegate : NSObject <UIApplicationDelegate, UITableViewDelegate, UITableViewDataSource> {
UIWindow *window;
NSString *helloWorld;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
UntitledAppDelegate.m:
#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
Super, ça a marché. Merci! –