如何通过使用instantiateViewControllerWithIdentifier创建的ViewController传递数据?

3

我创建了一个使用大量容器视图的应用程序。今天我发现在iOS 5中不支持嵌套segue(感谢Xcode让我知道)。 所以现在我有很多嵌套segue,它们之间传递数据。

我正在使用此代码将ViewControllers加载到UIView中,而不是从IB加载容器:

根视图控制器:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
    news.view.frame = self.newsSection.bounds;
    [self.newsSection addSubview:news.view];
    [self addChildViewController:news];
    [news didMoveToParentViewController:self];
    // Do any additional setup after loading the view.
}

我该如何使用上述方法并仍然将数据传递给NewsViewController(类似于prepare for segue)?
我尝试在上述内容中插入以下内容:
        NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
        news.view.frame = self.newsSection.bounds;
        news.myLabel.text = @"Test";
        [self.newsSection addSubview:news.view];

但标签不会改变。
我更喜欢仅传递数据而不使用单例或委托。
谢谢。
3个回答

1
我建议创建一个公共方法,从中您可以传递所需的数据到您的NewsViewController控制器。但是,目前的问题是在初始化之前更新了UI。此外,我建议您查看如何创建内容UIViewControllers,因为现在您只添加了其UIView。请参阅苹果公司的this文档,查看实现自定义容器视图控制器部分。还有这段代码:
- (void) displayContentController: (UIViewController*) content;
{
   [self addChildViewController:content];                 // 1
   content.view.frame = [self frameForContentController]; // 2
   [self.view addSubview:self.currentClientView];
   [content didMoveToParentViewController:self];          // 3
}

在您的情况下,在完成第三步之后,您可以传递要在NewsViewController中使用的值。

感谢您提供详细的答案。我在我的应用程序中使用容器。您是怎么知道我只添加了UIView?在上面的代码中,我将UIViewController添加到我的UIView中。这就是容器的作用,对吧? - Segev
我看到你使用了addChildViewController,但没有使用必要的didMoveToParentViewController。根据文档:如果您正在实现自己的容器视图控制器,则在完成到新控制器的转换后或(如果没有转换)在立即调用addChildViewController:方法后,它必须调用子视图控制器的didMoveToParentViewController:方法。 - Rui Peres
你可能会遇到意想不到的问题,例如旋转问题。 - Rui Peres
意料之外的问题 - 不要!我已经在代码中添加了那行。感谢您的帮助! - Segev

1
问题可能在于当您设置标签的文本时,标签仍然为nil
尝试在NewsViewController中添加一个NSString属性(例如labelText),并在viewDidLoad中执行self.myLabel.text = self.labelText;

我不明白。我想将数据传递给self.labelText,而不是相反的方式。 - Segev

0

你需要将字符串传递给下一个视图控制器,并在viewDidLoad中设置文本。由于视图尚未创建,如果检查UIlabel实例,则为nil。因此,您需要在加载到内存时设置下一个视图控制器的文本。

编辑 您需要在NewsViewController中创建一个NSString类型的属性变量(textString),并将字符串作为news.textString = @"Test";传递

然后,在NewsViewController的ViewDidLoad中将该字符串设置为标签的文本,如myLabel.text = textString;

更好的解释

-在NewsViewController.h中声明NSString属性

@property (strong, nonatomic) NSString *assignText;

正常实例化,但是分配字符串而不是标签。

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
    news.assignText = @"Test";
    [self addChildViewController:news];

-然后在你的NewsViewController的viewDidLoad方法中,将标签赋值为字符串。

-(void)viewDidLoad {
  [super viewDidLoad];
  self.myLabel.text = self.assignText;
}

我知道我必须传递它。我该如何传递它? - Segev
我可以知道负投票的原因吗? - Manish Agrawal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接