gpt4 book ai didi

iOS - 使用 xib 中的 tabBarController 将数据从一个 viewController 传递到另一个

转载 作者:行者123 更新时间:2023-11-29 02:10:08 26 4
gpt4 key购买 nike

我有一个要求,我必须在单击按钮时将一个字符串值从一个 viewController 传递到另一个 viewController。我还需要使用 tabBarController 从当前 viewController 切换到 secondViewController。我试过:

FirstViewController.h

#import "SecondViewController.h"

@property(nonatomic, strong) SecondViewController *secondController;

FirstViewController.m

-(IBAction)go:(id)sender
{
self.secondController.itemText=@"Demo Text";
self.tabBarController.selectedIndex=1; //say secondViewController index is 1
}

SecondViewController.h

#import "FirstViewController.h"

@property(nonatomic, strong) NSString *itemText;

SecondViewController.m

  -(void) viewWillAppear (BOOL) animated
{
NSLog(@"Comes from FirstViewController: %@",self.itemText);
}

虽然它切换了 viewController 但它打印空白。代码有什么问题?

最佳答案

我的猜测是您的第一个 View Controller 的“secondController”属性与选项卡栏 Controller 中指向的对象不同。

正确的做法是从 view controller array property 访问您的“SecondViewController”在标签栏中。

也就是说,像这样:

-(IBAction)go:(id)sender
{
SecondViewController *secondVC = (SecondViewController *)[self.tabBarController.viewControllers objectAtIndex: 1];
secondVC.itemText=@"Demo Text";
self.tabBarController.selectedIndex=1;
}

关于iOS - 使用 xib 中的 tabBarController 将数据从一个 viewController 传递到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29376358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com