简体   繁体   English

两个带有2个uibutton的选项卡

[英]Two tabs with 2 uibuttons

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? tabToggler方法将是什么样? How do I proceed? 我该如何进行?

You should just use 2 instances of UIButton and have them declared as properties/ivars in your .h file. 您应该只使用UIButton的2个实例,并在.h文件中将它们声明为properties / ivars。 Then just switch the properties that make a button look active/inactive in your toggleActiveTab method. 然后,只需切换属性即可使按钮在toggleActiveTab方法中看起来处于活动状态/非活动状态。

Like so: 像这样:

In .h 在.h中

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

In .m (loadView or viewDidLoad, depending on whether you use a NIB or not): 在.m中(loadView或viewDidLoad,取决于您是否使用NIB):

_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: 最后,您的toggleActiveTab方法可能看起来像这样:

- (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. 不要忘记释放dealloc方法中的2个按钮。

Also: Have you considered using a UITabBarController instead? 另外:您是否考虑过改用UITabBarController?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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