TabBarController中NavController下ViewController的初始化

发布于 2024-09-29 03:40:56 字数 505 浏览 0 评论 0原文

我有一个相对常见的 TabBarController 设置,其选项卡包含以 TableViewControllers 作为根的导航控制器。我正在尝试对这些 TableViewController 之一的初始化执行一些逻辑,但似乎无法找到调用的 init 函数。

我的目标是在 TableViewController (我已对其进行子类化)中添加一个侦听器,该侦听器可以通过更新 navigationController.tabBarItem.badgeVluew 属性来响应事件。

我尝试将代码放入 initWithStyle: 以及 init 中,但最终都没有被调用。我也尝试将其放入 viewDidLoad 中,但只有在控制器实际出现时才会被调用(我需要在控制器加载后/标签栏项目显示后立即发生)。

有谁知道我应该把这段代码放在哪里,以便在控制器初始化时发生?

此外,这都是通过界面构建​​器/NIB 设置的。我没有手动添加导航控制器或 tableviewcontroller,这就是为什么不清楚我需要重写哪个 init 函数。

I have the relatively common setup of a TabBarController whose tabs contain NavigationControllers which have TableViewControllers as their roots. I'm trying to perform some logic on initialization of one of these TableViewControllers but can't seem to find what init function gets called.

My goal is to add a listener in the TableViewController (that I have subclassed) which can respond to events by updating the navigationController.tabBarItem.badgeVluew property.

I've tried putting code into initWithStyle: as well as init but neither of them end up getting called. I've also tried putting it in viewDidLoad, but that only gets called once the controller actually appears (I need to have it happen as soon as the controller is loaded / as soon as the tab bar item shows up).

Does anyone know where I would put this code for it to happen on initialization of the controller?

Also, this is all set up through interface builder / NIBs. I'm not adding the nav controller or tableviewcontroller manually, which is why it's not clear what init function I need to override.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来分手还会想你 2024-10-06 03:40:57

如果您在 IB 中选择 UITabBarItems 之一,您将看到“从“YourView”加载的视图”。单击进入此“灰色”视图。在检查器窗口中,您将在“属性”选项卡(左侧的选项卡)中看到标题和将加载的 NIB 名称(我们将其称为“YourNibName”)。

现在选择检查器的右侧选项卡(身份)并将类名(类旁边的组合)更改为您必须在 xcode 中创建的“YourViewController”类。不要使用已选择的标准 ViewController。 InterfaceBuilder 加载您的 nib 并将其附加到您的 ViewController。

打开 YourNibName 并将 FilesOwner 的类(检查器,右侧选项卡)也更改为“YourViewController”。

TabBar 的 NIB 也包含一个 FilesOwner。为此 FilesOwner 创建一个 ViewController 并将其类设置为此控制器(即 TabBarController)

在“TabBarController”中,您可以使用以下代码找出选择了哪个选项卡:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

if ([viewController.nibName isEqualToString:@"NIBName1"]){

    // Do something here, if you like. (i.e. Save the state in a string or int)
}

if ([viewController.nibName isEqualToString:@"NIBNAme2"]){

    // Do something here, if you like. (i.e. Save the state in a string or int)
}

...
}

在这里您可以执行“全局”操作或预初始化某些操作。这是你能做的一件事。

初始化您的视图:

如果您选择一个选项卡并且视图(由 YourViewController 处理)将首次显示,“viewDidLoad”将在“YourViewController”中调用,

- (void)viewDidLoad {

// Here you can add views programatically
[self.view addSubview:myNavigationController.view];
[self.view bringSubviewToFront:myNavigationController.view];

    // And if you like, do some INIT here


[super viewDidLoad];

}

我希望这就是您的问题。

现在介绍一下徽章。这是一个 hack,但对我来说效果很好。

头文件:

向控制器添加一个插座,它代表您的 TabBarController:

@interface yourController : UIViewController <UITabBarControllerDelegate> {

    UITabBarController *tabBarController;   
}

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

将 IB 中的此插座与您的 TabBar 连接。

实现:

在 TabBarControllerClass 中,您可以覆盖“initWithNibName”:

@synthesize tabBarController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

        // Do some init here

        // select your desired item (it will be loaded)
        // then you can assign the badge
        tabBarController.selectedIndex = 1;
    tabBarController.selectedViewController.tabBarItem.badgeValue = @"222";

            // and select the item you will start with
    tabBarController.selectedIndex = 0;

// if you like you can add a notification, which you can activate from anywhere else
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(itemBadgeChanged:)
                                             name:@"itemBadgeChangedNotification"
                                           object:nil];
}
    return self;
}

如果您不使用 nib,请改用“- (void)loadView { ... }”。
您正在使用 TabBar 控制器的子类,也许您可​​以使用 'self.selectedIndex = 1;'而不是“tabBarController.selectedIndex = 1;”,等等。试试这个

希望这有帮助!

If you select one of your UITabBarItems in IB, you will see 'View loaded from "YourView"'. Click into this "gray" View. In the Inspector window you will see in the Attributes Tab (the tab on the left) the title and the NIB name which will be loaded (lets call it "YourNibName").

Now select the right tab of the inspector (Identity) and change the Classname (Combo next to Class) to your "YourViewController" class, which you must create in xcode. Don't use the standard ViewController, which is already selected. The InterfaceBuilder loads your nib and attaches it to your ViewController.

Open YourNibName and change FilesOwner's Class (Inspector, right Tab) to "YourViewController", too.

Your TabBar's NIB contains a FilesOwner, too. Create a ViewController for this FilesOwner and set its Class to this Controller (i.e. TabBarController)

In "TabBarController" you can find out which Tab was selected by using this code:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

if ([viewController.nibName isEqualToString:@"NIBName1"]){

    // Do something here, if you like. (i.e. Save the state in a string or int)
}

if ([viewController.nibName isEqualToString:@"NIBNAme2"]){

    // Do something here, if you like. (i.e. Save the state in a string or int)
}

...
}

Here you can do something "global" or preinitialize something. This is ONE thing you can do.

INIT OF YOUR VIEWS:

If you select a Tab and the view (which is handled by YourViewController) will be shown for the first time, "viewDidLoad" will be called in "YourViewController"

- (void)viewDidLoad {

// Here you can add views programatically
[self.view addSubview:myNavigationController.view];
[self.view bringSubviewToFront:myNavigationController.view];

    // And if you like, do some INIT here


[super viewDidLoad];

}

I hope this is what your question was about.

Now something about the badge. It's a hack, but works fine for me.

Header file:

Add an outlet to your controller, which is representing your TabBarController:

@interface yourController : UIViewController <UITabBarControllerDelegate> {

    UITabBarController *tabBarController;   
}

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

Connect this outlet in IB with your TabBar.

Implementation:

In your TabBarControllerClass you can overwrite 'initWithNibName':

@synthesize tabBarController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

        // Do some init here

        // select your desired item (it will be loaded)
        // then you can assign the badge
        tabBarController.selectedIndex = 1;
    tabBarController.selectedViewController.tabBarItem.badgeValue = @"222";

            // and select the item you will start with
    tabBarController.selectedIndex = 0;

// if you like you can add a notification, which you can activate from anywhere else
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(itemBadgeChanged:)
                                             name:@"itemBadgeChangedNotification"
                                           object:nil];
}
    return self;
}

if you don't use nib, use '- (void)loadView { ... }' instead.
You are using a subclass of the TabBar controller, maybe you can use 'self.selectedIndex = 1;' instead of 'tabBarController.selectedIndex = 1;', and so on. Just try this out

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文