将字符串从表视图控制器传递到新的视图控制器

时间:2014-06-11 15:54:00

标签: ios iphone objective-c uitableview viewcontroller

objective-c和ios新手在这里,但我正在学习!

name是descs中的字符串。我试图通过detailViewController.note将它传递给新的视图控制器。我可以在调试中看到字符串正在detailViewController.note中保存,但当我转到我的其他视图控制器时,它不在那里。

这是我的表视图控制器中的代码:

  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

        [tableView deselectRowAtIndexPath:indexPath animated:NO];

        DetailViewController *detailViewController = [[DetailViewController alloc]init];
        detailViewController.view.frame=self.view.frame;
        detailViewController.title=@"detail";
        detailViewController.note=[self.descs[indexPath.row]name];
        [[self navigationController] pushViewController:detailViewController animated:YES];


    }

DetailViewController的标头:

@interface DetailViewController : UIViewController
@property (nonatomic) NSString * note;
@end

DetailViewController的实现:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITextView *description =[[UITextView alloc]initWithFrame:CGRectMake(12,260,300,180)];
    description.font=[UIFont fontWithName:@"Helvetica" size:15];
    description.editable=NO;
    description.text=self.note;
    [self.view addSubview:description];
}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

您应该在viewWillAppear:中设置子视图。它在您当前的实现中不起作用,因为在设置viewDidLoad之前调用了detailViewController.note