带有2个uibutton的两个标签

时间:2011-10-17 11:50:42

标签: iphone objective-c uibutton uisegmentedcontrol

我有以下按钮:

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

我第一次想要显示First选项卡Active和第二个选项卡。

tabToggler方法的样子是什么?我该怎么办?

1 个答案:

答案 0 :(得分:1)

您应该只使用2个UIButton实例,并将它们声明为.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?