ios - 选项卡栏、 View Controller 和 Storyboard

标签 ios objective-c uitabbarcontroller uistoryboard

好的,所以我有一个小难题要解决。

我有一个选项卡栏 Controller ,其中两个选项卡(实际上是三个,但这显示了问题所在)具有基本相同的 UI。

它们以相同的方式显示信息,但使用不同的查询来获取该信息。

现在,因为它们是单独的选项卡,我需要在 Storyboard 上有两个实例才能创建这些选项卡。每个选项卡还有一个 UINavigationController 作为选项卡的“根”。但这意味着我在一个中设置的任何 UI 都必须在另一个中复制。我使用运行时属性来更改 Controller 从何处获取信息。

我可以只拥有一个实例并将其连接到两个导航 Controller 。这意味着我可以只在一个地方更改 UI,但我如何配置每个 View Controller 以获取正确的信息?

我确信有办法解决这个问题,但我陷入困境,目前找不到出路。

如果有人能理解我在说什么,请提供您的任何建议。

这是我根据答案尝试过的方法,但它只在第一个选项卡上加载。随后的选项卡只有一个没有 Root View 的 nag Controller 。

enter image description here

最佳答案

您可以再次将导航 Controller (及其顶 View Controller )连接到标签栏 Controller ,方法是再次从标签栏 Controller 中按住 Control 并拖动,然后选择“ View Controller ”。这将为您提供导航 Controller 的两个实例及其顶 View Controller 的两个实例。这样,如果您更改 UI,您只需编辑一个场景。我通过从选项卡式应用程序模板开始测试这个,将第二个 Controller 嵌入导航 Controller ,然后控制拖动到第二个导航 Controller 以第二次连接它。您只会在导航 Controller 的选项卡栏项目中看到一个标题,因此我在 SecondViewController.m 中执行了以下操作来更改标题,

@interface SecondViewController ()
@property (weak,nonatomic) IBOutlet UILabel *label;
@end

@implementation SecondViewController {
    BOOL isThird;
}

-(void)awakeFromNib {
    if ([self.navigationController isEqual:self.tabBarController.viewControllers[2]]) isThird = YES;
    if (isThird) {
        self.navigationController.tabBarItem.title = @"Third";
    }
}


-(void)viewDidLoad {
    [super viewDidLoad];
    if (isThird) {
        self.label.text = @"Third View";
    }else{
        self.label.text = @"Second View";
    }
}

这将为您提供第三个 Controller 的新标题,您可以在代码中使用 BOOL isThird 来区分 SecondViewController 的两个实例,当您想要将不同的信息加载到它们时。

enter image description here

关于ios - 选项卡栏、 View Controller 和 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24122589/

相关文章:

javascript - iOS 上的 WebViewJavascriptBridge

swift - 暗盒而不是自定义 tabBar 项目

iphone - 如何处理大 JSON 响应

ios - 通过更新约束在底部的 MGLMapView scaleBar 位置

iphone - UIImagePickerController 语言未更改

iphone - 用两个 UIImageView 保存一个图像,其中有两个图像 - ios

ios - 呈现一个新的 View Controller 保持标签栏

ios - 从标签栏 Controller 分支出来?

ios - 在 Swift 中获取点处像素的颜色

objective-c - 在 objective-c 中创建自定义动态类