【问题标题】:Two tabs with 2 uibuttons两个带有 2 个 uibuttons 的选项卡
【发布时间】:2011-10-17 21:03:12
【问题描述】:

我有以下按钮:

// 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 方法会是什么样子?我该如何进行?

【问题讨论】:

    标签: iphone objective-c uibutton uisegmentedcontrol


    【解决方案1】:

    您应该只使用 2 个 UIButton 实例并将它们声明为 .h 文件中的属性/变量。然后只需在您的 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 代替?

    【讨论】:

    • 你能给我一个代码示例吗?我对objective-c还是很陌生...我还想从表格视图中切换一些数据...;这个 ViewController 已经被放置到一个 UITabBarController 我选择按钮的另一个原因是我能够更多地自定义它们
    • 所以你想要两级标签?你确定这是一个好的设计决定吗?
    • 如果我点击活动按钮,活动状态会跳转到另一个。任何线索如何解决这个问题?
    猜你喜欢
    • 2016-06-07
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多