Reputation: 45463
I have the following code in my didSelectRowAtIndexPath delegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Exercise *exerciseView = [[Exercise alloc] initWithNibName:@"Exercise" bundle:nil]; //Makes new exercise object.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text; // Retrieves the string of the selected cell.
exerciseView.exerciseName.text = str;
NSLog(@"%@",exerciseView.exerciseName.text);
[self presentModalViewController:exerciseView animated:YES];
}
In this, I try to take the text of the selected cell, and set the IBOutlet UILabel exerciseName to that string.
My method compiles, but when I run the NSLog, which prints the textvalue of the UILabel after setting it to str, it returns null. I feel like this is a pointer problem, but can't seem to grasp it. Can anyone clarify things?
Upvotes: 0
Views: 45
Reputation: 62686
The problem is the half-initialized view controller. Need to let it get built before you init the contents of a subview.
Exercise.h
@property(strong, nonatomic) NSString *theExerciseName; // assuming ARC
- (id)initWithExerciseName:(NSString *)theExerciseName;
Exercise.m
@synthesize theExerciseName=_theExerciseName;
- (id)initWithExerciseName:(NSString *)theExerciseName {
self = [self initWithNibName:@"Exercise" bundle:nil];
if (self) {
self.theExerciseName = theExerciseName;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
exerciseName.text = self.theExerciseName;
}
Call that new initializer from your didSelect method.
Exercise *exerciseView = [[Exercise alloc] initWithExerciseName:str];
But please get that str by using the logic within cellForRowAtIndexPath, not by calling it.
Upvotes: 1