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

标签 ios objective-c iphone uitabbarcontroller xib

我有一个要求,我必须在单击按钮时将一个字符串值从一个 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/

相关文章:

iphone - 如何在 iPhone 应用程序从保存状态恢复时立即调用函数?

iphone - Iphone应用程序的GData示例

iphone - Xcode - 多个 URL 方案

ios - 观看连接 : didReceiveMessage not called

ios icloud 在设备中以编程方式设置开/关

ios - 仍然没有收到来自 fabric for twitter 的电子邮件

ios - 使用 NSLayoutConstraints 均匀间隔多行任意数量的元素

ios - 尝试添加和保存对象时 Realm 抛出错误

ios - 使用 Codable 和 CodableFirebase 快速解析 Firebase 值时获取 nil

iphone - UIViewController 似乎没有完全随 ARC 一起发布