使用prepareForSegue显示从MasterViewController到DetailViewController的字符串

时间:2013-07-27 06:06:37

标签: ios uitextview segue

我已将字符串声明为属性  MasterViewController.h

    @property (nonatomic , readwrite) NSString *theString;

这是我的prepareForSegue方法

     - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
      if ([[segue identifier] isEqualToString:@"showDetail"]) {

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
   [[segue destinationViewController] setTheString:string];

  //UITextField *dest = [segue destinationViewController];
  //dest.theString = string;
}

}

DetailViewController.h 我已经为textView

创建了一个插座
    @interface APPDetailViewController : UIViewController
    @property (weak, nonatomic) IBOutlet UITextView *textView;

现在,在实现中代码是什么,所以我可以在MasterViewController中将“theString”显示到DetailViewController中的textField?

2 个答案:

答案 0 :(得分:1)

<强> DetailViewController.h

@property (nonatomic , strong) NSString *myString; //Do this

<强> MasterViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSString *string = [feeds[indexPath.row] objectForKey: @"link"];

        //Do this
        APPDetailViewController *myController = (APPDetailViewController*)segue.destinationViewController;
        myController.myString = string;
    }
}

试试这种方式。

答案 1 :(得分:1)

1 - theString内应该有DetailViewController个属性。 (可选,请参阅下面的步骤3)

2 - 要编译和运行上述代码,您还必须将destinationViewController强制转换为DetailViewController

像这样:

DetailViewController * destVC = (DetailViewController *) [segue destinationViewController];

3 -

destVC.theString = string;

可选地,theString内没有DetailViewController,您可以这样做:

destVC.textView.text = string;