使用this教程我设置了一个自定义TabBar。不幸的是,本教程不会描述如何在您不想显示它的视图中隐藏自定义TabBar。
在customTabBar.h
我定义的
- (void) hideAlsoCustomTabBar;
- (void) showCustomTabBarAgain;
实现为
- (void) hideAlsoCustomTabBar {
btn1.hidden = YES;
btn2.hidden = YES;
btn3.hidden = YES;
btn4.hidden = YES;
}
- (void) showCustomTabBarAgain {
btn1.hidden = NO;
btn2.hidden = NO;
btn3.hidden = NO;
btn4.hidden = NO;
}
调用CustomTabBar.m
viewDidAppear
内的人工作正常,并完全按照我的期望去做。如果我尝试从ViewController调用那些方法,我需要像这样隐藏TabBar
[customTabs hideAlsoCustomTabBar];
在initWithNibName
OR viewDidLoad
OR viewWillAppear
内,什么都不会发生。我检查了NSLog,该方法被调用,但是当我读出任何按钮.hidden
属性的BOOL时,它返回0,当它应该是1(对于hidden==YES
)。
我不知道我的设置有什么问题。默认情况下,这些按钮的属性是否可能是私有的,因为CustomTabBar
继承自UITabBarController
,我无法设置它们?或者我在通话中犯了错误?
谢谢! 的修改 教程
中描述的TabBar实现import "CustomTabBar.h"
@implementation CustomTabBar
@synthesize btn1,btn2,btn3,btn4;
- (void) viewDidAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self hideExistingTabBar];
[self addCustomElements];
}
- (void) hideExistingTabBar {
for (UIView *view in self.view.subviews) {
if ([view isKindOfClass:[UITabBar class]]) {
view.hidden = YES;
break;
}
}
}
- (void) addCustomElements {
//Initialise the two images for btnEinleitung, not selected and selected
UIImage *btnImage = [UIImage imageNamed:@"btnEinl.png"];
UIImage *btnImageSelected = [UIImage imageNamed:@"btnEinl_s.png"];
self.btnEinleitung = [UIButton buttonWithType:UIButtonTypeCustom];
btnEinleitung.frame = CGRectMake(0, 430, 86, 50);
[btnEinleitung setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnEinleitung setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[btnEinleitung setTag:0];
[btnEinleitung setSelected:true];
//set other buttons
btnImage = [UIImage imageNamed:@"btnUbg.png"];
btnImageSelected = [UIImage imageNamed:@"btnUbg_s.png"];
self.btnUebungen = [UIButton buttonWithType:UIButtonTypeCustom];
btnUebungen.frame = CGRectMake(86, 430, 80, 50);
[btnUebungen setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnUebungen setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[btnUebungen setTag:1];
/* do the same for btn3 and btn4*/
//add custom buttons to view
[self.view addSubview:btn1];
[self.view addSubview:btn2];
[self.view addSubview:btn3];
[self.view addSubview:btn4];
//setup event handlers so the buttonClicked method will respond to the touch up inside event
[btn1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn4 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
//set tab to active according to the passed tag number
- (void) selectTab:(int)tabID {
switch (tabID) {
case 0:
[btnEinleitung setSelected:TRUE];
[btnUebungen setSelected:FALSE];
[btnTipps setSelected:FALSE];
[btnBrauchtum setSelected:FALSE];
btnEinleitung.userInteractionEnabled = NO;
btnUebungen.userInteractionEnabled = YES;
btnTipps.userInteractionEnabled = YES;
btnBrauchtum.userInteractionEnabled = YES;
break;
case 1:
[btnEinleitung setSelected:FALSE];
[btnUebungen setSelected:TRUE];
[btnTipps setSelected:FALSE];
[btnBrauchtum setSelected:FALSE];
btnEinleitung.userInteractionEnabled = YES;
btnUebungen.userInteractionEnabled = NO;
btnTipps.userInteractionEnabled = YES;
btnBrauchtum.userInteractionEnabled = YES;
break;
// and so on for 2 and 3
}
self.selectedIndex = tabID;
}
//get the tag of the sender/pressed button, call the function selectTab
- (void) buttonClicked:(id)sender {
int tagNum = [sender tag];
[self selectTab:tagNum];
}
修改 如下所述,我在Tabbar中有4个Tabs,它是用IB生成的,添加了带有ViewControllers的导航控制器,为那些做了Outlets并在IB中连接它们。 单击第二个选项卡(例如sndMenuVC)将打开一个包含4个按钮的视图。单击其中一个按钮将导致另一个视图(例如detailVC),其中我不希望显示TabBar。 detailVC有它自己的笔尖 打开detailVC会发生按钮的动作,就像这个
一样- (IBAction) openFourth:(id)sender{
detailVC *detailView = [[detailVC alloc] initWithNibName:@"detailVC" bundle:nil andSender:kFourthButtonSender];
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];
}
由于这是一个自定义的initWithNibName,这就是我实现它的方式
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andSender: (NSString *)calledByButton{
self.callingButton = calledByButton;
[super initWithNibName:@"detailVC" bundle:nil];
return self;
}
所以我基本上只是根据传输的发件人设置一个全局变量“callingButton”,然后调用“normal”initWithNibName。
一切正常。
如果我尝试调用hideAlsoCustomTabBar
,并且NSLog的值为btn1.hidden,则从detailVC调用时返回0,但在CustomTabBar中调用时返回1并实际隐藏按钮。
如果需要,我将customTabs作为IBOutlet,但不知道它是否正确连接到CustomTabBar类型的TabBarController。
希望这有助于理解我:)如果您需要任何其他信息,请告诉我。 谢谢!
答案 0 :(得分:2)
我已经写了一篇关于我的RXCustomTabBar教程的跟进来回答你的问题。我没有看到在这里完全复制它的任何一点。
Hiding your New Custom UITabBar (RXCustomTabBar follow up)
如果您有任何疑问,请告诉我。 奥利
答案 1 :(得分:1)
如果你想隐藏tabBar,你可以在视图控制器中调用
[[[self tabBarController] tabBar] setHidden:YES];
答案 2 :(得分:0)
在RXCustomTabBar.m文件中,从viewDidAppear调用addCustomElements函数。
-(void)addCustomElements
{
.
.
.
.
// Add my new buttons to the view
// Following lines are adding buttons to the view. Put your condition here according to requirement so that it will check and add buttons accordingly.
[self.view addSubview:btn1];
[self.view addSubview:btn2];
[self.view addSubview:btn3];
[self.view addSubview:btn4];
.
.
.
.
}
希望这有帮助。
<强>更新强>
//Add following function in RXCustomTabBar.m
-(void)hideButtons
{
btn1.hidden = YES;
btn2.hidden = YES;
btn3.hidden = YES;
btn4.hidden = YES;
}
- (void)selectTab:(int)tabID
{
switch(tabID)
{
case 0:
[btn1 setSelected:true];
[btn2 setSelected:false];
[btn3 setSelected:false];
[btn4 setSelected:false];
break;
case 1:
[self hideButtons]; //Call function for hiding buttons like this.
[btn1 setSelected:false];
[btn2 setSelected:true];
[btn3 setSelected:false];
[btn4 setSelected:false];
break;
}
self.selectedIndex = tabID;
}
假设您要隐藏第二视图控制器上的按钮。所以我在案例1索引中调用[self hideButtons]。
这将隐藏所有标签栏按钮。而且,你必须将差异条件设置为显示那些标签按钮。
这有意义吗?
答案 3 :(得分:0)
由于自定义选项卡已存在,因此调用此方法。您应该在此处将其分配到detailView
的{{1}}媒体资源。
customTabs
原始答案
由于对- (IBAction) openFourth:(id)sender{
detailVC * detailView = [[detailVC alloc] initWithNibName:@"detailVC" bundle:nil andSender:kFourthButtonSender];
detailView.customTabs = **theExistingCustomTabsObject**;
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];
}
的子类的引用是UITabBarController
,您必须正确设置它。如果已使用IB设置视图控制器,请转至NIB文件。右键单击视图控制器,选择nil
插座并将其连接到包含视图控制器的customTabs
对象。
如果您已经以编程方式创建了视图控制器,那么在将视图控制器添加到选项卡栏之后以及在释放视图控制器之前,请执行此操作,
customTabBar