如何通过按钮单击控制标签栏项目?

Posted

技术标签:

【中文标题】如何通过按钮单击控制标签栏项目?【英文标题】:How can i control the tabbar item via button click? 【发布时间】:2012-01-08 09:44:26 【问题描述】:

我面临一些严重的问题。所以我解释得很具体。

    两个视图控制器 A) UITabbar 控制器 B) 包含按钮滚动视图的视图控制器。

    我在 A 控制器的导航栏上有子视图 B 控制器 [图像中的蓝色按钮] [图像下方图像的黑色按钮]。

这是我怎么做的代码

scrollButtonView = [[scrollViewButtons alloc] initWithNibName:@"scrollViewButtons" bundle:nil];
CGRect frame = CGRectMake(0, 20, 320, 43);
scrollButtonView.view.frame = frame;
scrollButtonView.view.userInteractionEnabled =YES;
[self.navigationController.view addSubview:scrollButtonView.view];

现在我的问题是,当我单击控制器 B 的按钮时,tabbarbar 选择的索引将发生变化,并且该视图控制器将显示在屏幕中。这意味着当我单击顶部滚动按钮中的 order 按钮时然后屏幕将显示订单控制器和标签栏项目索引将更改,并且与标签栏控制器 [A 控制器] 相同

请注意: A 和 B 控制器都将包含相同的视图按钮和相同的控制器。我不知道该怎么做?我想要一个详细的答案。

另请参阅:

如果不可能,那么告诉我如何添加按钮的滚动菜单栏添加标签栏控制器的每个控制器,并且滚动按钮将重定向相同的控制器,如标签栏控制器项目? 现在,我希望我能得到我的解决方案。

【问题讨论】:

您不应该重新发布问题,您可能想要删除其中一个问题,因为这只会分散解决问题的人,您最终可能会遇到一团糟。您可能还想重新标记您的问题以包含 ios 或一些更广泛传播的标签... 【参考方案1】:

哈哈哈.....当我解决它时真是太有趣了。无论我以不同的方式解决了这个问题,我并没有将滚动视图按钮控制器用于控制器,在每个控制器中我都在哪里实现了功能滚动视图中的按钮创建和按钮的操作我刚刚更改了标签栏控制器的选定索引。

-(void)viewDidload我写了这段代码

     UIView *scrollViewBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
scrollViewBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"topmenu_bg.png"]];

menuScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(5,0,320,40)];
menuScrollView.showsHorizontalScrollIndicator = FALSE;
menuScrollView.showsVerticalScrollIndicator = FALSE;
menuScrollView.bounces = TRUE;
[scrollViewBackgroundView addSubview:menuScrollView];
[self.view addSubview:scrollViewBackgroundView];

[self createMenuWithButtonSize:CGSizeMake(92.0, 30.0) withOffset:5.0f noOfButtons:7];   

这里是按钮的创建和操作

-(void)mybuttons:(id)sender    
NSLog(@"mybuttons called");
UIButton *button=(UIButton *)sender;
NSLog(@"button clicked is : %iBut \n\n",button.tag);
int m = button.tag;
for(int j=0;j<8;j++)
    if(button.tag == m)
        self.tabBarController.selectedIndex = m;
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_hover.png"] forState:UIControlStateHighlighted]; //sets the background Image]            
    
    if(button.tag != m)
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]
    

       

 -(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons

NSLog(@"inserting into the function for menu bar button creation"); 
for (int i = 0; i < totalNoOfButtons; i++) 

    UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    [button addTarget:self action:@selector(mybuttons:) forControlEvents:UIControlEventTouchUpInside];
    (button).titleLabel.font =  [UIFont fontWithName:@"Arial" size:12];
    if(i==0)
        [button setTitle:[NSString stringWithFormat:@"Dashboard"] forState:UIControlStateNormal];//with title
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_hover.png"] forState:UIControlStateNormal]; //sets the background Image]
    
    if(i==1)
        [button setTitle:[NSString stringWithFormat:@"Order"] forState:UIControlStateNormal];//with title
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]

    
    if(i==2)
        [button setTitle:[NSString stringWithFormat:@"Product"] forState:UIControlStateNormal];//with title
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]

    
    if(i==3)
        [button setTitle:[NSString stringWithFormat:@"Customers"] forState:UIControlStateNormal];//with title
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]

    
    if(i==4)
        [button setTitle:[NSString stringWithFormat:@"Content"] forState:UIControlStateNormal];//with title
    
    if(i==5)
        [button setTitle:[NSString stringWithFormat:@"Site Analysis"] forState:UIControlStateNormal];//with title
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]

    
    if(i==6)
        [button setTitle:[NSString stringWithFormat:@"Store Settings"] forState:UIControlStateNormal];//with title
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]

    
    if(i==7)
        [button setTitle:[NSString stringWithFormat:@"CMS Settings"] forState:UIControlStateNormal];//with title
        [button setBackgroundImage:[UIImage imageNamed:@"btn_topmenu_normal.png"] forState:UIControlStateNormal]; //sets the background Image]

    
    button.frame = CGRectMake(i*(offset+buttonSize.width), 6.0, buttonSize.width, buttonSize.height);
    button.clipsToBounds = YES;
    button.showsTouchWhenHighlighted=YES;
    button.layer.cornerRadius = 5;//half of the width
    button.layer.borderColor=[UIColor clearColor].CGColor;
    button.layer.borderWidth=0.0f;
    button.tag=i;
    [menuScrollView addSubview:button];

menuScrollView.contentSize=CGSizeMake((buttonSize.width + offset) * totalNoOfButtons, buttonSize.height);
[self.view addSubview:menuScrollView];


【讨论】:

以上是关于如何通过按钮单击控制标签栏项目?的主要内容,如果未能解决你的问题,请参考以下文章

如何从外部视图控制器转到标签栏项目

如何在标签栏控制器视图上创建后退按钮?

使用导航控制器后标签栏项目图标丢失

如何在标签栏视图控制器中显示后退按钮?

从标签栏控制器模态显示视图

标签栏控制器显示最后推送的控制器 - swift 4