0

プロトタイプセルの UILabel から単純な文字列を次のビューコントローラーのラベルに転送しようとしています。View Controller の viewDidLoad の label.text の値が (null) を返しています。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    mainCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (mainCell == nil) {
        mainCell = [[dictionaryTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSString* date = [dateArray objectAtIndex:indexPath.row];
    mainCell.viewLabel.text = date;

    return mainCell;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"View Segue"]) {
        NSLog(@"View Load Segue Success");

        ViewController *one = segue.destinationViewController;
        one.label.text = mainCell.viewLabel.text;
    }
}

ここで何が間違っていますか?

4

3 に答える 3

2
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"View Segue"]) {
        NSLog(@"View Load Segue Success");
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        ViewController *one = segue.destinationViewController;
        one.label.text = [dateArray objectAtIndex:indexPath.row];
    }
}

実際には、テキストをテキストラベルに割り当てるには、viewControlleroneのメソッド(viewDidLoadまたはviewWillAppear)で行う必要があります。したがって、viewControllerのプロパティを転送用のプロパティにする必要がありますNSString

于 2013-01-13T18:38:15.653 に答える
1

使用できますindexPathForSelectedRow

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"View Segue"]) {

        ViewController *one = segue.destinationViewController;

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        one.textProperty = [dateArray objectAtIndex:indexPath.row];
    }
}

senderまたは、セグエがセルから次のシーンへの場合にも使用できます。たとえば、次のようになります。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"View Segue"])
    {
        ViewController *one = segue.destinationViewController;

        NSAssert([sender isKindOfClass:[UITableViewCell class]], @"Not cell");

        UITableViewCell *cell = sender;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

        one.textProperty = [dateArray objectAtIndex:indexPath.row];
    }
}

2 つの注意事項:

  1. 良いプログラミング スタイルの問題として、セルからテキスト値を取得していません。モデルからテキスト値を取得しています。情報を渡すためにビューに依存するべきではありません。元の情報源であるモデルに戻ります。

  2. textのプロパティをlabel宛先コントローラーに直接設定しないでください。のコントロールはdestinationControllerまだ作成されていません。destinationControllerまでコントロールの設定を延期する必要がありますviewDidLoad。そのため、代わりにNSString、宛先コントローラーでプロパティを作成します。

    @property (nonatomic, strong) NSString *textProperty;
    

    明らかに、 よりもわかりやすい名前を使用する必要がありtextPropertyますが、アイデアが得られることを願っています。とにかく、prepareForSegueこの新しいプロパティを設定することができviewDidLoad、宛先コントローラの はそのプロパティを使用してのプロパティNSStringを入力する必要があります。次に例を示します。textUILabel

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.label.text = self.textProperty;
    }
    
于 2013-01-13T18:54:16.667 に答える
1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath     {
    YourViewController *controller =[[YourViewController alloc] init];
    [self presentModalViewController:controller animated:YES];
    one.label.text = [dateArray objectAtIndex:indexPath.row];
}

label.textアフターを変更しpresentModalViewControllerます。さてどうなる?

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion 

をすでに使用されていることは承知していますSegue。他の答えに従う必要があります。

于 2013-01-13T18:19:48.817 に答える