两个带有 2 个 uibutton 的选项卡
我有以下按钮:
// First Tab selected
UIButton *firstTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
firstTabButton.frame = CGRectMake(75, 42, 153, 42);
[firstTabButton setTitle:@"FirstTab selected" forState:UIControlStateNormal];
[firstTabButton addTarget:self action:@selector(tabToggler:) forControlEvents:UIControlEventTouchUpInside];
[firstTabButton setBackgroundImage:[UIImage imageNamed:@"FirstTabActive.png"] forState:UIControlStateNormal];
firstTabButton.adjustsImageWhenHighlighted = NO;
[self.view addSubview:firstTabButton];
// First Tab
UIButton *firstTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
firstTabButton.frame = CGRectMake(75, 42, 153, 42);
[firstTabButton setTitle:@"FirstTab selected" forState:UIControlStateNormal];
[firstTabButton addTarget:self action:@selector(tabToggler:) forControlEvents:UIControlEventTouchUpInside];
[firstTabButton setBackgroundImage:[UIImage imageNamed:@"FirstTab.png"] forState:UIControlStateNormal];
firstTabButton.adjustsImageWhenHighlighted = NO;
[self.view addSubview:firstTabButton];
//Second Tab Active
UIButton *secondTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
secondTabButton.frame = CGRectMake(75, 42, 153, 42);
[secondTabButton setTitle:@"Second Tab Active" forState:UIControlStateNormal];
[secondTabButton addTarget:self action:@selector(tabToggler:) forControlEvents:UIControlEventTouchUpInside];
[secondTabButton setBackgroundImage:[UIImage imageNamed:@"SecondTabActive.png"] forState:UIControlStateNormal];
secondTabButton.adjustsImageWhenHighlighted = NO;
[self.view addSubview:secondTabButton];
appDelegate = [[UIApplication sharedApplication] delegate];
//Second Tab
UIButton *secondTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
secondTabButton.frame = CGRectMake(75, 42, 153, 42);
[secondTabButton setTitle:@"Second Tab" forState:UIControlStateNormal];
[secondTabButton addTarget:self action:@selector(tabToggler:) forControlEvents:UIControlEventTouchUpInside];
[secondTabButton setBackgroundImage:[UIImage imageNamed:@"SecondTabActive.png"] forState:UIControlStateNormal];
secondTabButton.adjustsImageWhenHighlighted = NO;
[self.view addSubview:secondTabButton];
appDelegate = [[UIApplication sharedApplication] delegate];
第一次我想显示第一个选项卡“活动”和第二个选项卡。
tabToggler 方法会是什么样子?我该如何继续?
I have the following buttons:
// First Tab selected
UIButton *firstTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
firstTabButton.frame = CGRectMake(75, 42, 153, 42);
[firstTabButton setTitle:@"FirstTab selected" forState:UIControlStateNormal];
[firstTabButton addTarget:self action:@selector(tabToggler:) forControlEvents:UIControlEventTouchUpInside];
[firstTabButton setBackgroundImage:[UIImage imageNamed:@"FirstTabActive.png"] forState:UIControlStateNormal];
firstTabButton.adjustsImageWhenHighlighted = NO;
[self.view addSubview:firstTabButton];
// First Tab
UIButton *firstTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
firstTabButton.frame = CGRectMake(75, 42, 153, 42);
[firstTabButton setTitle:@"FirstTab selected" forState:UIControlStateNormal];
[firstTabButton addTarget:self action:@selector(tabToggler:) forControlEvents:UIControlEventTouchUpInside];
[firstTabButton setBackgroundImage:[UIImage imageNamed:@"FirstTab.png"] forState:UIControlStateNormal];
firstTabButton.adjustsImageWhenHighlighted = NO;
[self.view addSubview:firstTabButton];
//Second Tab Active
UIButton *secondTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
secondTabButton.frame = CGRectMake(75, 42, 153, 42);
[secondTabButton setTitle:@"Second Tab Active" forState:UIControlStateNormal];
[secondTabButton addTarget:self action:@selector(tabToggler:) forControlEvents:UIControlEventTouchUpInside];
[secondTabButton setBackgroundImage:[UIImage imageNamed:@"SecondTabActive.png"] forState:UIControlStateNormal];
secondTabButton.adjustsImageWhenHighlighted = NO;
[self.view addSubview:secondTabButton];
appDelegate = [[UIApplication sharedApplication] delegate];
//Second Tab
UIButton *secondTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
secondTabButton.frame = CGRectMake(75, 42, 153, 42);
[secondTabButton setTitle:@"Second Tab" forState:UIControlStateNormal];
[secondTabButton addTarget:self action:@selector(tabToggler:) forControlEvents:UIControlEventTouchUpInside];
[secondTabButton setBackgroundImage:[UIImage imageNamed:@"SecondTabActive.png"] forState:UIControlStateNormal];
secondTabButton.adjustsImageWhenHighlighted = NO;
[self.view addSubview:secondTabButton];
appDelegate = [[UIApplication sharedApplication] delegate];
For the first time I'd like to display First tab Active and the 2nd tab.
What the tabToggler method will look like? How do I proceed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该只使用 UIButton 的 2 个实例,并将它们声明为 .h 文件中的属性/ivars。然后只需在您的toggleActiveTab 方法中切换使按钮看起来处于活动/非活动状态的属性即可。
像这样:
在 .h 中
在 .m 中(loadView 或 viewDidLoad,取决于您是否使用 NIB):
最后,您的toggleActiveTab 方法可能如下所示:
不要忘记释放 dealloc 方法中的 2 个按钮。
另外:您是否考虑过使用 UITabBarController 来代替?
You should just use 2 instances of UIButton and have them declared as properties/ivars in your .h file. Then just switch the properties that make a button look active/inactive in your toggleActiveTab method.
Like so:
In .h
In .m (loadView or viewDidLoad, depending on whether you use a NIB or not):
Finally, your toggleActiveTab method might look something like this:
Don't forget to release the 2 buttons in your dealloc method.
Also: Have you considered using a UITabBarController instead?