Iphone 两个选项卡和两个按钮

Iphone 两个选项卡和两个按钮,iphone,objective-c,uibutton,uisegmentedcontrol,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

我有以下按钮:

// 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方法是什么样子的?如何继续?

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

像这样:

In.h

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

不要忘记释放dealloc方法中的2个按钮


另外:您是否考虑过改用UITabBarController

你能给我一个代码示例吗?我对objective-c很陌生。。。我还想从表视图中切换一些数据。。。;此ViewController已放置在UITabBarController中。我选择按钮的另一个原因是,我可以对它们进行更多的自定义。所以您想要两个级别的选项卡吗?你确定这是一个好的设计决策吗?如果我点击激活按钮,激活状态会跳到另一个。有什么线索可以解决这个问题吗?
@interface...
{
  UIButton *_firstButton;
  UIButton *_secondButton;
}
_firstButton = <code to set up the first button>;
_secondButton = <code to set up the second button>;
- (void)toggleActiveTab
{
  BOOL activateSecond = _firstButton.enabled;
  _firstButton.enabled = !activateSecond;
  _secondButton.enabled = activateSecond;

  // whatever other setup
}