[英]Displaying a string from MasterViewController to DetailViewController using prepareForSegue
I have declared a string as a property in MasterViewController.h 我在MasterViewController.h中声明了一个字符串作为属性
@property (nonatomic , readwrite) NSString *theString;
Here is my prepareForSegue method 这是我的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.hi have created an outlet for the textView DetailViewController.hi为textView创建了一个插座
@interface APPDetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextView *textView;
Now, What to code in implementation so i can display "theString" in MasterViewController to the textField in DetailViewController ? 现在,在实现中编码什么,以便我可以在MasterViewController中将“theString”显示到DetailViewController中的textField?
DetailViewController.h DetailViewController.h
@property (nonatomic , strong) NSString *myString; //Do this
MasterViewController.m 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;
}
}
Try this way. 试试这种方式。
1 - there should be theString
property inside DetailViewController
. 1 -
DetailViewController
应该有theString
属性。 (Optional, see step 3 below) (可选,请参阅下面的步骤3)
2 - For the above code to compile and run, you must also cast destinationViewController
to DetailViewController
. 2 - 要编译和运行上面的代码,还必须将
destinationViewController
为DetailViewController
。
Like this: 像这样:
DetailViewController * destVC = (DetailViewController *) [segue destinationViewController];
3 - 3 -
destVC.theString = string;
Optionally, without theString
inside DetailViewController
, you could do it like: 可选的,没有
theString
内DetailViewController
,你能做到这一点,如:
destVC.textView.text = string;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.