创建按钮的方法

时间:2013-12-24 10:15:27

标签: ios objective-c uibutton subview

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

-(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 ,我不明白为什么?也许我的方法应该返回这些按钮?谢谢。

1 个答案:

答案 0 :(得分:0)

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

因此,例如,您可以遵循这种方法。

#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。因此,你不会看到它。