这可能很容易实现,但我是 iOS 编程新手,我似乎被卡住了.
This is probably something really simple to achieve, but I am new to programming for iOS and I seem to be stuck.
所以,基本上,我有一个选项卡式应用程序.我决定除了标签栏之外还需要一个导航栏.为此,我放置了标签栏控制器,然后添加了我的视图控制器并嵌入到每个视图控制器的导航控制器中,然后将其连接到标签栏.
So, basically, I have a tabbed application. I decided that I wanted a navigation bar, in addition to the tab bar. To do this, I put the tab bar controller and then I added my view controllers and embedded in a navigation controller for each view controller, which is then connected to the tab bar.
我在故事板中的层次结构有点像这样:
My hierarchy in the storyboard looks somewhat like this:
我被困在这里的部分是尝试将数据从第一个 View Controller 传递到任何其他 View Controller 时.在添加导航控制器之前,我使用 prepareForSegue 方法来传递数据,如下所示:
The part where I am stuck here, is when attempting to pass data from the first View Controller and to any of the other View Controllers. Before adding in the navigation controllers, I was using the prepareForSegue method to pass the data, like so:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"logged"])
{
UITabBarController *tabar=segue.destinationViewController;
SecondViewController *svc=[tabar.viewControllers objectAtIndex:1];
svc.groupArray = [(NSArray*)sender objectAtIndex:0];
svc.userArray = [(NSArray*)sender objectAtIndex:1];
svc.taskArray = [(NSMutableArray*)sender objectAtIndex:2];
svc.selfArray = [(NSMutableArray*)sender objectAtIndex:3];
[tabar setSelectedIndex:1];
}
}
如您所见,我将数据传递给我的第二个视图控制器并使用 performSegueWithIdentifier 方法将标签栏索引设置为 1,因为我希望打开第二个页面.所有这一切都工作得很好,直到我将导航控制器引入我的代码,因为我想要导航栏.那时一切都崩溃了.如果我尝试按原样运行代码,应用程序会崩溃并在控制台中显示以下输出:
As you can see, I was passing the data to my second view controller and set the tab bar index to 1 using the performSegueWithIdentifier method, since I wanted the second page to open. All of this was working just fine, until I introduced the Navigation Controllers to my code, since I want navigation bars. That's when everything pretty much broke. If I try to run the code as is, the application crashes with the following output in the console:
[UINavigationController setGroupArray:]: 无法识别的选择器发送到实例 0x7ffa6acec620
[UINavigationController setGroupArray:]: unrecognized selector sent to instance 0x7ffa6acec620
*** 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[UINavigationController setGroupArray:]: unrecognized selector sent to instance 0x7ffa6acec620'
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setGroupArray:]: unrecognized selector sent to instance 0x7ffa6acec620'
我尝试过修改代码,但似乎没有任何效果.我只是很困惑,也许正确方向的提示会对我有所帮助.
I've tried messing around with the code a bit, but nothing seems to work really. I am just very confused, and maybe a hint in the right direction would help me out a bit.
由于您将视图控制器嵌入到导航控制器中,它们不再是标签栏的直接子级;但是,您将标签栏控制器的子级转换为导航控制器以外的其他东西.您想首先获取导航控制器,它是标签栏控制器的子级,然后获取该导航控制器的子级.这将是您的视图控制器.然后,您可以为此设置数据.
Since you embedded your view controllers inside navigation controllers they are no longer direct children of the tabbar; however, you are casting the children of the tabbar controller as something other than Navigation controllers. You want to first get the navigation controller, which is a child of the tabbar controller, and then get the child of that navigation controller. This will be your view controller. Then you can set the data for that.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"logged"])
{
UITabBarController *tabar=segue.destinationViewController;
UINavigationController *navController = [tabbar.viewControllers objectAtIndex:1];
SecondViewController *svc=[navController.viewControllers objectAtIndex:0];
svc.groupArray = [(NSArray*)sender objectAtIndex:0];
svc.userArray = [(NSArray*)sender objectAtIndex:1];
svc.taskArray = [(NSMutableArray*)sender objectAtIndex:2];
svc.selfArray = [(NSMutableArray*)sender objectAtIndex:3];
[tabar setSelectedIndex:1];
}
}
这篇关于通过视图控制器、标签栏控制器、导航控制器和视图控制器传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!