gpt4 book ai didi

ios - 将数据从 View Controller 传递到 iOS 中的选项卡栏 Controller

转载 作者:可可西里 更新时间:2023-11-01 03:06:00 26 4
gpt4 key购买 nike

我正在开发一个 iOS 应用程序,但现在我不知所措。我正在尝试将数据从第一个 View Controller 传递到 TabBarViewController 的第一个选项卡(使用 Storyboard)。我找到了很多解释如何在 View Controller 之间传递数据的教程,但对我的选项卡栏没有任何作用。我知道选项卡栏 Controller 包含一种 View 数组。 View Controller 和 Tab Bar Controller 之间的关系是使用 segue (push) 实现的。所以,我认为使用 prepareForSegue() - 方法很容易。像那样:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"tab"]) {

// ...

}
}

不幸的是,Tab Bar Controller 和每个 tab bar view 之间的关系并不是真正的 segue。这只是一种“关系”。这意味着,我无法将 segue 标识符用于上述方法。在这种情况下是否可以使用 prepareForSegue?如果没有,有什么想法可以解决这个问题吗?我知道有一个类似的问题,但答案并没有那么有用。我是否必须为选项卡栏 Controller 中的每个选项卡( View )创建一个新文件?或者是否可以为整个选项卡栏 Controller 设置一个类(m. & h.),使用 objectAtIndex() 访问多个 View ?

提前致谢!

最佳答案

这是我的有效设置:

  1. 设置继续:
    1. 在 Storyboard 中使用带有 2 个 subview Controller 的选项卡栏 Controller 设置 View Controller
    2. 指定 segue 标识符 ( tab )
  2. 在 Storyboard 中设置类:
    1. View Controller 类 = ViewController
    2. 标签栏 Controller 类 = TabBarController
    3. 标签栏 Controller subview Controller 类 = TabsViewController (两者共享)
  3. 设置 labelString选项卡栏 Controller 中的属性:

    1. TabBarController.h :

      @property (nonatomic, strong) NSString *labelString;
    2. TabBarController.m :

      @synthesize labelString;
  4. 设置 prepareForSegue ViewController.m 中的方法:

    #import "TabBarController.h"

    ...

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([[segue identifier] isEqualToString:@"tab"]){
    TabBarController *tabBar = [segue destinationViewController];
    [tabBar setLabelString:[NSString stringWithFormat:@"This has been set"]];
    }
    }
  5. 设置 UILabel s 用于子选项卡栏 View Controller 。

    1. 拖默认UILabel控制到两个 subview Controller
    2. TabsViewController.h 中创建属性:

      @property (nonatomic, strong) IBOutlet UILabel *label;
    3. Hook 5.15.2在 Storyboard 中

  6. 设置 ViewDidLoad TabsViewController.m 中的方法:

    #import "TabBarController.h"

    ...

    @synthesize label;

    ...

    - (void)viewDidLoad
    {
    [super viewDidLoad];

    TabBarController *tabBar = (TabBarController *)self.tabBarController;
    label.text = [NSString stringWithFormat:@"Tab %i: %@",[tabBar.viewControllers indexOfObject:self],tabBar.labelString];
    }

现在单击第一个和第二个选项卡将显示标签 Tab 0: This has been setTab 1: This has been set , 分别。

关于ios - 将数据从 View Controller 传递到 iOS 中的选项卡栏 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17743107/

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