两个带有 2 个 uibutton 的选项卡

发布于 2024-12-10 00:46:39 字数 2346 浏览 0 评论 0原文

我有以下按钮:

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮光之海 2024-12-17 00:46:39

您应该只使用 UIButton 的 2 个实例,并将它们声明为 .h 文件中的属性/ivars。然后只需在您的toggleActiveTab 方法中切换使按钮看起来处于活动/非活动状态的属性即可。

像这样:

在 .h 中

@interface...
{
  UIButton *_firstButton;
  UIButton *_secondButton;
}

在 .m 中(loadView 或 viewDidLoad,取决于您是否使用 NIB):

_firstButton = <code to set up the first button>;
_secondButton = <code to set up the second button>;

最后,您的toggleActiveTab 方法可能如下所示:

- (void)toggleActiveTab
{
  BOOL activateSecond = _firstButton.enabled;
  _firstButton.enabled = !activateSecond;
  _secondButton.enabled = activateSecond;

  // whatever other setup
}

不要忘记释放 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

@interface...
{
  UIButton *_firstButton;
  UIButton *_secondButton;
}

In .m (loadView or viewDidLoad, depending on whether you use a NIB or not):

_firstButton = <code to set up the first button>;
_secondButton = <code to set up the second button>;

Finally, your toggleActiveTab method might look something like this:

- (void)toggleActiveTab
{
  BOOL activateSecond = _firstButton.enabled;
  _firstButton.enabled = !activateSecond;
  _secondButton.enabled = activateSecond;

  // whatever other setup
}

Don't forget to release the 2 buttons in your dealloc method.

Also: Have you considered using a UITabBarController instead?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文