0

didSelectRowAtIndexPathデリゲートメソッドに次のコードがあります。

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

ここでは、選択したセルのテキストを取得し、IBOutletUILabelのexerciseNameをその文字列に設定しようとしています。

私のメソッドはコンパイルされますが、strに設定した後にUILabelのテキスト値を出力するNSLogを実行すると、nullが返されます。これはポインタの問題のように感じますが、理解できないようです。誰かが物事を明確にすることができますか?

4

1 に答える 1

1

問題は、半分初期化されたViewControllerです。サブビューのコンテンツを初期化する前に、ビルドさせる必要があります。

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

didSelectメソッドからその新しい初期化子を呼び出します。

Exercise *exerciseView = [[Exercise alloc] initWithExerciseName:str]; 

ただし、呼び出しではなく、cellForRowAtIndexPath内のロジックを使用してそのstrを取得してください。

于 2012-04-23T03:14:51.420 に答える