简体   繁体   中英

How can i control the tabbar item via button click?

My facing some serious problem.So I am explaining very specifically.

  1. Two view Controller A) UITabbar Controller B) View controller containing scrollview of buttons.

  2. I have subview B controller [blue button in the image] on the navigation bar of A controller [black button of the image in below of image].

在此处输入图片说明

Here is the code how can i did it

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

Now My problem is when i will click button of the controller B then tabbarbar selected index will change and that view controller will show in the screen.thats mean when I will click order button in the scrolled button at the top of the screen then it will show the order controller and tabbar item index will change and same as for the tabbar controller [A controller]

NOTE THAT: Both A and B controller will contain same view button and same controller.I do not know how can do it? I want a answer with detailed manner.

SEE ALSO:

If it is not possible then tell me how can i add a scrolling menu bar of buttons add every controller of the tabbar controller and scrolling button will redirect the same controller like tabbar controller items? Now, i hope i will get my solution.

Ha ha ha .....it was so much fun when i solved it.whatever i solved this problem in different way, i did not use scrollview button controller for controller just, in every controller i have made function to where buttons within a scrollview create and on action of button i have just change the selected index of the tabbar controller.

in -(void)viewDidload i wrote this code

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

here is the button create and action

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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