0

我正在尝试将NSString文本传递给UIViewControlleriOS 中的按钮“点击”事件。我写了以下代码。

self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

self.detailViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:self.detailViewController animated:YES];

self.detailViewController.urlAddress = @"http://www.google.com";

我已经urlAddressdetailViewController. 在我用NSLogin测试之后viewWillAppear,它只显示空值,而不是urlAddress字符串。我该如何解决?

4

2 回答 2

2

您在视图控制器已经呈现后设置字符串。切换它,你应该很好。

于 2012-07-02T13:56:42.590 回答
1

注意调用方法的顺序:

  1. initWithNibName
  2. viewDidLoad
  3. viewWillAppear

只有在三个人都完成工作后,您的字符串才会更改。

将您的代码更改为:

self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
self.detailViewController.urlAddress = @"http://www.google.com";
self.detailViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:self.detailViewController animated:YES]; //Triggers viewWillAppear eventually
于 2012-07-02T14:00:27.587 回答