-12

我正在尝试创建一个在视图顶部和导航栏下方具有导航栏的应用程序看法。我想在顶部使用它(就在导航栏的下方)。所以我使用了 4 个自定义按钮。我正在使用导航控制器来推送视图,这些视图是根据按下的按钮创建的。你能解释一下如何像标签栏一样实现吗?如何按钮将像标签栏项目一样工作。

4

4 回答 4

0

你可以做如下几点。

  • 将 4 个圆形矩形按钮放在 Xib 的底部。
  • 为每个按钮创建 4 个类,并使用按钮适当地连接 IBAction。现在您可以在没有动画的情况下推送和弹出:-

[self.navigationController pushViewController:ObjYourViewcontroler animated:NO];

并像这样弹出它:-

[self.navigationController popViewControllerAnimated:NO];

于 2013-02-26T05:59:33.570 回答
0

只需检查完整的代码。它就像标签栏控制器。

localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:0];

AllRumsController *allRumsScreen=[[AllRumsController alloc]init];
RumRecipesController *rumRecipesScreen =[[RumRecipesController alloc] init];
PartyPlannerClass *aPartyPlannerScreen =[[PartyPlannerClass alloc] init];
BarTendingClass *aBarTendingScreen =[[BarTendingClass alloc] init];
MoreControllersClass *moreControllerScreen =[[MoreControllersClass alloc] init];

controllersArray = [[NSArray alloc]initWithObjects:allRumsScreen,rumRecipesScreen,aPartyPlannerScreen,aBarTendingScreen,moreControllerScreen,nil];

for(int i=0;i<[controllersArray count];i++)
{
UINavigationController *localNavigationController=[[UINavigationController alloc]initWithRootViewController:[controllersArray objectAtIndex:i]];
localNavigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    if(!i)
        [localNavigationController setTitle:@"All Rums"];
    else if(i ==1)
        [localNavigationController setTitle:@"Recipes"];
    else if(i ==2)
        [localNavigationController setTitle:@"PartyPlanner"];
    else if(i ==3)
        [localNavigationController setTitle:@"BarTending"];
    else 
        [localNavigationController setTitle:@"More"];
       localNavigationController.navigationBar.hidden = YES;
       [localViewControllersArray addObject:localNavigationController];

     [localNavigationController release];
 }
[allRumsScreen release];
[rumRecipesScreen release];
[aPartyPlannerScreen release];
[aBarTendingScreen release];
[moreControllerScreen release];

if(contentView == nil)
{
    contentView = [[UIView alloc] initWithFrame:CGRectMake(0,0,MAX_WIDTH, 431)];
    contentView.backgroundColor = [UIColor clearColor];
}
if(bottomView == nil)
{   
    bottomView = [[UIView alloc] initWithFrame:CGRectMake(0,431,MAX_WIDTH, 49)];
    bottomView.backgroundColor = [UIColor clearColor];
}

allButtonsArray = [[NSMutableArray alloc]initWithCapacity:0];

for(int i=0;i<[localViewControllersArray count];i++)
{
    UIButton *aButton=[UIButton buttonWithType:UIButtonTypeCustom];
    aButton.frame=CGRectMake(i*64,0,64,49);
    [aButton setTag:i];
    if(!i)
    {
        [aButton setImage:[UIImage imageNamed:@"ourrums_n.png"] forState:UIControlStateNormal];
        [aButton setImage:[UIImage imageNamed:@"ourrums_a.png"] forState:UIControlStateHighlighted];
        [aButton setImage:[UIImage imageNamed:@"ourrums_n.png"] forState:UIControlStateDisabled];
    }
    else if(i ==1)
    {
        [aButton setImage:[UIImage imageNamed:@"Recipies_n.png"] forState:UIControlStateNormal];
        [aButton setImage:[UIImage imageNamed:@"Recipies_a.png"] forState:UIControlStateHighlighted];
        [aButton setImage:[UIImage imageNamed:@"Recipies_n.png"] forState:UIControlStateDisabled];

    }else if(i ==2)
    {
        [aButton setImage:[UIImage imageNamed:@"myevents_n.png"] forState:UIControlStateNormal];
        [aButton setImage:[UIImage imageNamed:@"myevents_a.png"] forState:UIControlStateHighlighted];
        [aButton setImage:[UIImage imageNamed:@"myevents_n.png"] forState:UIControlStateDisabled];

    }else if(i ==3)
    {
        [aButton setImage:[UIImage imageNamed:@"bartending_n.png"] forState:UIControlStateNormal];
        [aButton setImage:[UIImage imageNamed:@"bartending_a.png"] forState:UIControlStateHighlighted];
        [aButton setImage:[UIImage imageNamed:@"bartending_n.png"] forState:UIControlStateDisabled];

    }else 
    {
        [aButton setImage:[UIImage imageNamed:@"more_n.png"] forState:UIControlStateNormal];
        [aButton setImage:[UIImage imageNamed:@"more_a.png"] forState:UIControlStateHighlighted];
        [aButton setImage:[UIImage imageNamed:@"more_n.png"] forState:UIControlStateDisabled];
    }
    [aButton addTarget:self action:@selector(showViewOfButton:) forControlEvents:UIControlEventTouchUpInside];
    [allButtonsArray addObject:aButton];
    [bottomView addSubview:aButton];
}

[[[[UIApplication sharedApplication] delegate] window] addSubview:contentView];
[[[[UIApplication sharedApplication] delegate] window] addSubview:bottomView];

[bottomView setHidden:YES];
[contentView setHidden:YES];


[self showViewOfButton:[allButtonsArray objectAtIndex:0]];





-(void)showViewOfButton:(id)sender
    {
if(start == YES)
{
    start = NO;
    [repeat setTag:[sender tag]];
}
else if([repeat tag] == [sender tag])
{
    return;
}
else
{
    [repeat setTag:[sender tag]];
}

if(previous!=nil)
{
    [previous setImage:[previous imageForState:UIControlStateDisabled] forState:UIControlStateNormal];
}


if( [[contentView subviews] count] )
{
    if([[RumsDataHolder sharedHolder] mailController])
    {
        [[[[RumsDataHolder sharedHolder] mailController] retain] dismissModalViewControllerAnimated:NO];
    }
    [[[contentView subviews] objectAtIndex:0] removeFromSuperview];
}

if( ((UIView *)sender).tag == 0)
{
    [sender setImage:[UIImage imageNamed:@"ourrums_a.png"] forState:UIControlStateNormal];
    [contentView addSubview:[[localViewControllersArray objectAtIndex:0] view]];
}
else if( ((UIView *)sender).tag == 1)
{
    [sender setImage:[UIImage imageNamed:@"Recipies_a.png"] forState:UIControlStateNormal];
    [contentView addSubview:[[localViewControllersArray objectAtIndex:1] view]];
}
else if(((UIView *)sender).tag == 2)
{
    [sender setImage:[UIImage imageNamed:@"myevents_a.png"] forState:UIControlStateNormal];
    [contentView addSubview:[[localViewControllersArray objectAtIndex:2] view]];
}
else if( ((UIView *)sender).tag == 3)
{
    [sender setImage:[UIImage imageNamed:@"bartending_a.png"] forState:UIControlStateNormal];
    [contentView addSubview:[[localViewControllersArray objectAtIndex:3] view]];
}
else if(((UIView *)sender).tag == 4)
{
    [sender setImage:[UIImage imageNamed:@"more_a.png"] forState:UIControlStateNormal];
    [contentView addSubview:[[localViewControllersArray objectAtIndex:4] view]];
}
else
    ;
previous=sender;
}
于 2013-02-26T06:09:46.920 回答
0

好吧,而不是使用UIbuttons或调用整个UITabbarController为什么不使用UITabbartabbar items这里的代码..这应该让你去:)

 UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(your frame)];   
  NSMutableArray  *tabBarItemsArray= [[NSMutableArray alloc] init];
    UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"artist" image:[UIImage imageNamed:@"artist-tab.png"] tag:1];//assigning the title name and image
[tabBarItem1 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:48.0/255.0  green:60.0/255.0 blue:109.0/255.0 alpha:1.0],UITextAttributeTextColor,nil] forState:UIControlStateNormal];//set the color when the tabbar appears

[tabBarItem1 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil] forState:UIControlStateSelected];//sets the color when the tabbar is selected

 UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTitle:@"Facebook" image:[UIImage imageNamed:@"music-tab.png"] tag:2];

[tabBarItem2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:48.0/255.0  green:60.0/255.0 blue:109.0/255.0 alpha:1.0],UITextAttributeTextColor,nil] forState:UIControlStateNormal];

[tabBarItem2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil] forState:UIControlStateSelected];

        [tabBarItemsArray addObject:tabBarItem1];
        [tabBarItemsArray addObject:tabBarItem2];
        tabBar.items = tabBarItemsArray;
        tabBar.delegate = self;

     [self.view addSubview:tabBar];

确保在头文件中添加委托 UITabBarDelegate 以实现此方法

  - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
        NSLog(@"Tabbar selected itm %d",item.tag); 
   // here you can call your view based on tab click you can try using switch statements here based on item.tag
       }

希望这能回答您的问题并有所帮助:)

此外,我们可以使用图像自定义 UITabbar...请让我知道...如果您愿意,我会在此处发布代码。

于 2013-02-26T06:10:42.893 回答
0

要表现得像 tabbar 控制器一样,请按照相同的步骤以编程方式创建 tabbar 控制器,

创建标签栏

为要在单击 UIButton 时推送的每个 UIViewController 创建实例,将其添加为 rootviewcontrollerUINavigationController及其所有对象NSArray并将该数组设置为_tabBarController.viewControllers,然后将 _tabBarController.view 添加到self.window

创建自定义视图

接下来现在创建一个UIView与视图中 _tabBarController 位置相同的框架,添加UIButton要在 _tabBarController 上显示的 s,可以为任何背景图像自定义按钮或匹配您需要的 UI 外观,将所有这些按钮作为子视图添加到UIView最后是视图对象到self.window子视图。至此,您将看到带有您想要的所有按钮的自定义标签栏,现在让它像标签栏一样。

不要忘记为您添加的每个按钮添加一个动作,动作实现类似于,

- (void)select_tab:(UIButton *)sender
{
   _tabBarController.selectedIndex = sender.tag;
   //_tabBarController is object of UITabBarController
}

请记住,还需要每个按钮都应该带有一些标签集,这些标签将作为当前标签栏索引的索引,因此按照将视图控制器添加到数组中的顺序给出标签,按钮标签应该以 0 开头(零)基本索引到您拥有的最大视图控制器。

就是这样,希望这有帮助!

于 2013-02-26T06:19:03.030 回答