【问题标题】:How to pass Data to a ViewController created using instantiateViewControllerWithIdentifier?如何将数据传递给使用 instantiateViewControllerWithIdentifier 创建的 ViewController?
【发布时间】:2013-07-25 10:23:19
【问题描述】:

我创建了一个使用大量容器视图的应用。今天我发现 iOS 5 不支持嵌入式转场(感谢 Xcode 让我知道。) 所以现在我有很多嵌入的 segues 可以在彼此之间传递数据。

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

RootViewController:

- (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(类似于为 segue 做准备)

我已经尝试在上面插入类似的东西

        NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
        news.view.frame = self.newsSection.bounds;
        news.myLabel.text = @"Test";
        [self.newsSection addSubview:news.view];

但标签不会改变。

我更喜欢只传递数据而不使用单例或委托。

谢谢

【问题讨论】:

    标签: ios objective-c cocoa-touch


    【解决方案1】:

    我建议创建一个公共方法,您可以通过该方法在NewsViewController 控制器上传递您需要的数据。不过,现在的问题是您在初始化 UI 之前正在更新它。另外我建议你看看如何创建内容UIViewControllers,因为现在你只添加它的UIView。检查来自 Apple 的 this 文档,查看实现自定义容器视图控制器部分。而这部分代码:

    - (void) displayContentController: (UIViewController*) content;
    {
       [self addChildViewController:content];                 // 1
       content.view.frame = [self frameForContentController]; // 2
       [self.view addSubview:self.currentClientView];
       [content didMoveToParentViewController:self];          // 3
    }
    

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

    【讨论】:

    • 感谢您的详细解答。我在我的应用程序中使用容器。是什么让你说我只添加 UIView?在上面的代码中,我将 UIViewControllers 添加到我的 UIViews 中。这就是容器的全部意义所在吗?
    • 我确实看到你使用了addChildViewController,但不是didMoveToParentViewController,这是必要的。来自文档:If you are implementing your own container view controller, it must call the didMoveToParentViewController: method of the child view controller after the transition to the new controller is complete or, if there is no transition, immediately after calling the addChildViewController: method.
    • 您可能会遇到意想不到的问题,例如旋转问题。
    • 意外问题 - 不,谢谢!我已经在代码中添加了该行。感谢您的帮助!
    【解决方案2】:

    问题可能是在您设置标签文本的那一刻,标签仍然是nil

    尝试将NSString property 添加到您的NewsViewController(例如labelText),并在viewDidLoad 中添加self.myLabel.text = self.labelText;

    【讨论】:

    • 我没有关注。我想将数据传递给 self.labelText,而不是相反。
    【解决方案3】:

    您已将字符串传递给您的下一个视图控制器,并将 viewDidLoad 传递给该字符串。
    它不起作用,因为您的视图尚未创建,如果检查 UIlabel 的实例,它将为零。因此,当它加载到内存中时,您已经在下一个视图控制器中设置了文本。

    编辑 您已经在 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;
    }
    

    【讨论】:

    • 我知道我必须通过它。如何通过?
    • 我能知道-ve投票的原因吗?
    猜你喜欢
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 2012-11-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多