【问题标题】:Method for creating buttons创建按钮的方法
【发布时间】:2013-12-24 10:15:26
【问题描述】:

我有自定义按钮类“EHCheckBox”。我的应用程序中有很多按钮,所以我尝试优化我的代码并在一行而不是 10 个中创建按钮。在我看来,在 viewDidLoad 我调用方法女巫调用方法来创建我的按钮。

-(void)viewDidLoad
{
  .....
  [self buttons];
  .....
}

-(void)buttons
{
    [self addNewBut:pcswitch Title:@"   Switch" Column:3 Position:5 Dropable:NO Hide:NO]
}
-(void)addNewBut:(EHCheckBox*)checkBoxName Title:(NSString*)checkBoxTitle Column:(int)checkBoxColumn Position:(int)checkBoxPosition Dropable:(BOOL)checkBoxDropable Hide:(BOOL)hide
{
    checkBoxName = [EHCheckBox buttonWithType:UIButtonTypeCustom];
    [checkBoxName setTitle:NSLocalizedString(checkBoxTitle, @"") forState:UIControlStateNormal];
    checkBoxName.hidden = hide;

    if (checkBoxDropable) {
        [checkBoxName setBackgroundImage:[UIImage imageNamed:@"CheckButton1.png"] forState:UIControlStateNormal];
        [checkBoxName setBackgroundImage:[UIImage imageNamed:@"CheckButtonSelect1.png"] forState:UIControlStateSelected];
    }else{
        [checkBoxName setBackgroundImage:[UIImage imageNamed:@"CheckButton.png"] forState:UIControlStateNormal];
        [checkBoxName setBackgroundImage:[UIImage imageNamed:@"CheckButtonSelect.png"] forState:UIControlStateSelected];
    }

    int firstColX = 45;
    int firstColY = 125;
    int width = 140;
    int height = 36;
    int widhtTwo = 18;

    int x = firstColX + (checkBoxColumn - 1)*(width+widhtTwo);
    int y = firstColY + (checkBoxPosition - 1)*(height);

    checkBoxName.frame = CGRectMake(x, y, 140, 50);
    [self.view addSubview:checkBoxName];
}

hide = YES,因为我有其他方法可以取消隐藏和隐藏按钮,但它们不起作用,因为在视图中我的按钮是 nil,我不明白为什么?也许我的方法应该返回这些按钮?谢谢。

【问题讨论】:

  • addNewBut:...方法的开头设置一个断点并从那里取出。
  • @dandan78 "...in view my button is nil..." 这些,意味着我使用了调试。
  • pcswitch 呢?

标签: ios objective-c uibutton subview


【解决方案1】:

我无法理解您的目标和代码是什么,但我真的建议使用类别。在这种情况下,罐子应该看起来更好,更有条理。

因此,例如,您可以采用这种方法。

#import <UIKit/UIKit.h>

@interface UIButton (Factory)

+ (UIButton*)buttonWithTitle:(NSString*)title column:(int)column position:(int)position dropable:(BOOL)dropable hide:(BOOL)hide;

@end

#import "UIButton+Factory.h"

@implementation UIButton (Factory)

+ (UIButton*)buttonWithTitle:(NSString*)title column:(int)column position:(int)position dropable:(BOOL)dropable hide:(BOOL)hide {

    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    //[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitle:NSLocalizedString(title, @"") forState:UIControlStateNormal];
    [button setHidden:hide];

    if (dropable) {
        [button setBackgroundImage:[UIImage imageNamed:@"CheckButton1.png"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"CheckButtonSelect1.png"] forState:UIControlStateSelected];
    } else {
        [button setBackgroundImage:[UIImage imageNamed:@"CheckButton.png"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"CheckButtonSelect.png"] forState:UIControlStateSelected];
    }

    int firstColX = 45;
    int firstColY = 125;
    int width = 140;
    int height = 36;
    int widhtTwo = 18;

    int x = firstColX + (column - 1)*(width+widhtTwo);
    int y = firstColY + (position - 1)*(height);
    button.frame = CGRectMake(x, y, 140, 50);

    return button;
}

@end

您可以像下面这样使用这个类别。您需要导入#import "UIButton+Factory.h"

UIButton* button = [UIButton buttonWithTitle:@"test" column:1 position:1 dropable:YES hide:NO];
[viewWhereYouWantToAddTheButton addSubview:button];

一个注释。如果您将0 传递给列和位置,您的按钮将假定为负的xy。因此,你不会看到它。

【讨论】:

  • 你的代码启发了我,我写了类的构造函数,一切正常,非常感谢!
  • 我不想一直写 addSubView... 所以我对这些方法进行了现代化改造,但我在选择器方面遇到了一些问题,它一直无法识别,据我所知,它试图调用不在我的查看,我的方法 initWithTitle:(NSString*)checkBoxTitle Column:(int)checkBoxColumn Position:(int)checkBoxPosition Dropable:(BOOL)checkBoxDropable Hide:(BOOL)hide selector:(SEL)action viewController:(UIView*)view跨度>
  • 抱歉,您能解释一下您的意思吗?我无法理解...@RomanSimenok
猜你喜欢
  • 2018-01-22
  • 2020-12-26
  • 1970-01-01
  • 2018-07-09
  • 2021-05-13
  • 1970-01-01
  • 2017-10-08
  • 2016-07-15
  • 1970-01-01
相关资源
最近更新 更多