1

我面临一些严重的问题。所以我非常具体地解释。如何通过按钮点击控制tabbar项目?

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

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

enter image description here

这是我如何能做到了

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 co ntroller]

需要注意的是: A和B两种控制器将含有相同的观点按钮,同样controller.I不知道该怎么做呢?我想要详细的答案。

也可参见:

如果是不可能的,那么告诉我,我怎样才能添加按钮的滚动菜单栏添加的TabBar控制器的每个控制器和滚动按钮将重定向喜欢的TabBar控制器相同的控制器项目? 现在,我希望我会得到我的解决方案。

+1

你不应该重新发布的问题,你可能想拿下其中的一个,因为这只是分裂解决问题的人,最终可能会产生一团糟。您可能还想重申您的问题,以包含ios或更广泛传播的标签...... – bardiir 2012-01-11 13:12:43

回答

0

哈哈哈.....当我解决这个问题时,这真是太好玩了。无论我用不同的方式解决这个问题,我没有在控制器中使用scrollview按钮控制器,到滚动视图内的按钮创建和按钮的动作我只是改变选定的控制器的索引。

-(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]; 

}