简体   繁体   English

自定义iOS UITabBarController委托触发器viewDidLoad

[英]Custom iOS UITabBarController Delegate Triggers viewDidLoad

Issue: 问题:

An UITabBarController delegate method calls viewDidLoad and negates/ignores the screen selection. UITabBarController委托方法调用viewDidLoad并取反/忽略屏幕选择。

Background: 背景:

I am making a split-view controller for small iOS screens. 我正在为小型iOS屏幕制作一个拆分视图控制器。 So, far the screen looks something like this: 因此,到目前为止,屏幕看起来像这样:

Custom Split-View Controller 自定义拆分视图控制器

For the Detail View (RS), I'm using a UITabBarController, with the TabBar removed. 对于明细视图(RS),我使用的是UITabBarController,并且已删除TabBar。 You can find the code for this here . 您可以在此处找到此代码。

The user selects the buttons on left, and a delegate sends the button's tag id to the UITabBarController delegate method. 用户选择左侧的按钮,然后委托将按钮的标记ID发送到UITabBarController委托方法。

The delegate method looks like this: 委托方法如下所示:

// delegate method in subclass of UITabBarController
-(void) screenSelected:(int)screenNum
{
    NSLog(@"delegate arrived: %d", screenNum);
    self.selectedIndex = screenNum;
    // code goes to viewDidLoad
}

With breakpoints in place, I determined that the screen does not actually change. 有了断点,我确定屏幕实际上没有改变。

If this screenSelected method is instead called from the UITabBarController viewDidLoad method (when the app first opens), the method works fine and the selectedIndex is changed as expected. 如果改为从UITabBarController viewDidLoad方法(当应用程序首次打开时)调用此screenSelected方法,则该方法可以正常工作,并且selectedIndex会按预期更改。

I'm trying to figure out why the delegate is triggering viewDidLoad. 我试图弄清楚委托为什么触发viewDidLoad。 Does the delegate reset the view? 委托是否重置视图?

Thanks 谢谢

Yes, viewdidload will call every time because you are initializing tabs again. 是的,因为您将再次初始化选项卡,因此每次都会调用viewdidload setSelectedIndex intialize tabbarcontroller evrytime from UITabbarcontroller class. setSelectedIndex初始化UITabbarcontroller类中的tabbarcontroller evrytime。 You should not do that from uitabbarcontroller class. 您不应该在uitabbarcontroller类中这样做。 just initialize tabbarcontroller onece. 只需初始化tabbarcontroller onece。

You should not use tabbarcontroller like this way. 您不应该像这样使用tabbarcontroller according to your requirement you not need to use tabbar just shows viewController on button click. 根据您的要求,您无需使用选项卡,只需在单击按钮时显示viewController

hope this will work. 希望这会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM