ViewController层次结构“丢失" [英] ViewController Hierarchy getting 'lost'

查看:73
本文介绍了ViewController层次结构“丢失"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图层次结构,该层次结构通过编程方式设置如下:

I've got a view hierarchy which is setup (programmatically) as follows:

Window.root = TabBarController-> UINavigationControllers-> UIViewControllers

Window.root = TabBarController-->UINavigationControllers-->UIViewControllers

我认为这很标准.这是我的问题:

I presume that's rather standard. Here's my problem:

我在选项卡A上.我想导航到选项卡B,然后在选项卡B上的visibleViewController上调用一个方法.

I'm on Tab A. I want to navigate to Tab B, and call a method on the visibleViewController on Tab B.

// View Changes OK
[AppDelegate.tabBarController setSelectedIndex:tabB];

// nav  = 0x387ABF i.e. Valid Address
UINavigationController *nav = (UINavigationController*)[AppDelegate.tabBarController selectedViewController];

// The problem:
nav.viewControllers; // this is nil
nav.topViewController; // as is this
nav.visibleViewContorller; // this too.

即使将对nav.viewControllers的调用放在从主线程调用的单独方法中,我仍然会得到0x0/nil.

Even if I put the calls to nav.viewControllers in a separate method which is called from the Main Thread, I still get 0x0/nil.

我在做什么错了?

一个后续问题是: 更改标签时如何将信息从一个ViewController传递给另一个ViewController? (如果我不能在VC上从tabA到tabB调用方法)

A follow-up question is: How can I pass information from one ViewController to another when changing tabs? (If I can't call methods on VC's from tabA to tabB)

我感觉这与这里的问题有关.

推荐答案

您应该将信息存储在一个普通的地方,无论是单身人士还是初学者,只需创建一个课程并将其传递给下一个人即可.

You should store the information in a common place, either a singleton or as you are a beginner just make a class and pass it down.

sharedDataObject = [[MySharedDataObject alloc] init];

firstViewController.myDataObject = sharedDataObject;
secondViewController.myDataObject = sharedDataObject;

这篇关于ViewController层次结构“丢失"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆