gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:39:20 24 4
gpt4 key购买 nike

我面临一些严重的问题。所以我正在非常具体地解释。

  1. 两个 View Controller A) UITabbar Controller B) 包含按钮 ScrollView 的 View Controller 。

  2. 我在 A Controller [图像下方图像的黑色按钮] 的导航栏上有 subview B Controller [图像中的蓝色按钮]。

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

现在我的问题是,当我单击 Controller B 的按钮时,tabbarbar 选定的索引将更改并且该 View Controller 将显示在屏幕上。这意味着当我单击滚动中的 order 按钮时屏幕顶部的按钮然后它将显示顺序 Controller 和标签栏项目索引将更改并且与标签栏 Controller 相同 [A Controller ]

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

另见:

如果不可能,请告诉我如何添加按钮的滚动菜单栏添加标签栏 Controller 的每个 Controller ,滚动按钮将像标签栏 Controller 项目一样重定向同一个 Controller ?现在,我希望我能得到我的解决方案。

最佳答案

哈哈哈......当我解决它时非常有趣。无论我以不同的方式解决这个问题,我都没有将 ScrollView 按钮 Controller 用于 Controller ,在每个 Controller 中我都在其中发挥了作用 ScrollView 中的按钮创建和按钮的 Action 我刚刚更改了标签栏 Controller 的选定索引。

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

}

关于ios - 我如何通过单击按钮来控制标签栏项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8776615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com