如何使用具有多个参数的方法来创建自定义按钮

发布于 2024-09-05 16:18:01 字数 518 浏览 5 评论 0原文

我重复我的代码几次来创建自定义按钮,我试图创建一个方法并通过调用该方法并使用两个参数 btnTitle 和 btnAction 来创建所有所需的按钮。

我的方法代码

-(void) addnewButton:(NSString *) btnTitle withAction:UIAction btnAction; {

  // Add a custom edit navigation button
 editButton = [[[UIBarButtonItem alloc]
   initWithTitle:NSLocalizedString((btnTitle), @"")
   style:UIBarButtonItemStyleBordered
   target:self
   action:@selector(btnAction)] autorelease];
 self.navigationItem.rightBarButtonItem = editButton;

}

现在我该如何调用这个方法来创建一个按钮?

I am repeating my codes few times to create custom buttons, I am trying to create a method and create all my required buttons just by called that method and using two parameters, btnTitle and btnAction.

My Codes for the method

-(void) addnewButton:(NSString *) btnTitle withAction:UIAction btnAction; {

  // Add a custom edit navigation button
 editButton = [[[UIBarButtonItem alloc]
   initWithTitle:NSLocalizedString((btnTitle), @"")
   style:UIBarButtonItemStyleBordered
   target:self
   action:@selector(btnAction)] autorelease];
 self.navigationItem.rightBarButtonItem = editButton;

}

Now how shall I call this method to create a button?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甚是思念 2024-09-12 16:18:01

不太确定这里的问题是什么?

当应用程序的设计完成后,我构建了一个 ISInterfaceElements 类,其中包含我在应用程序中需要的东西。 (UIlabels、UIColors、UIButtons 等)UIElements 的实例化只会占用空间,使问题变得复杂,如果您需要更改在 14 个位置使用的按钮或颜色,那么在不同的位置编写所有设置代码确实很糟糕地方。无论如何,我通常这样做:

//  ISInterfaceElement.h


#import <Foundation/Foundation.h>

typedef enum {

    DarkBackground, 
    LightBackground,


} ISColorType;

typedef enum {

    Headline, 
    Detail,


} ISLabelType;

@interface ISInterfaceElement : NSObject {

}

+ (UIColor*) getColor:(ISColorType) colorType;
+ (UILabel*) getLabel:(ISLabelType) labelType;

@end

使用漂亮的枚举来使其易于记住。

//  ISInterfaceElement.m

#import "ISInterfaceElement.h"


@implementation ISInterfaceElement

+ (UIColor*) getColor:(ISColorType) colorType {

    int value;

    switch (colorType) {

        case DarkBackground:
            value = 0x2d3c3f;
            break;          
        case LightBackground:
            value = 0x627376;
            break;
        default:
            value = 0x000000;
            break;
    }

    int r, g, b;
    b = value & 0x0000FF;
    g = ((value & 0x00FF00) >> 8);
    r = ((value & 0xFF0000) >> 16);

    return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1.0f]; 


}

+ (UILabel*) getLabel:(ISLabelType) labelType {

    UILabel *label = [[[UILabel alloc] init] autorelease];
    [label setBackgroundColor:[UIColor clearColor]];    

    switch (labelType) {
        case Headline:
            [label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14]];
            [label setTextColor:[UIColor whiteColor]];
            break;
        case Detail:
            [label setFont:[UIFont fontWithName:@"HelveticaNeue" size:14]];
            [label setTextColor:[UIColor whiteColor]];
            break;  
        default:
            break;
    }

    return label;
}

因为这些都是类方法,所以我不需要实例化 ISInterfaceElement 类来使用它们。

我只是说:

UILabel aHeadlineLabel = [ISInterfaceElement getLabel:Headline];

你可以为你的按钮构建类似的东西,你需要做的就是在你的方法中构建一个新的 UIButton,设置标题和操作,最后返回 myButton,就像我对标签所做的那样。

希望有帮助

Not quite sure what the question is here?

When the design of an App is sort of laid out, I have build a ISInterfaceElements Class that holds stuff I will be needing around the app. (UIlabels, UIColors, UIButtons etc.) the instantiation of UIElements is just taking up space, complicating matters and if you need to change a button or color that is used in 14 places it is really bad to have written all the setup code in different places. Anyways, I usually do it like this:

//  ISInterfaceElement.h


#import <Foundation/Foundation.h>

typedef enum {

    DarkBackground, 
    LightBackground,


} ISColorType;

typedef enum {

    Headline, 
    Detail,


} ISLabelType;

@interface ISInterfaceElement : NSObject {

}

+ (UIColor*) getColor:(ISColorType) colorType;
+ (UILabel*) getLabel:(ISLabelType) labelType;

@end

With nice enums to make it easy to remember.

//  ISInterfaceElement.m

#import "ISInterfaceElement.h"


@implementation ISInterfaceElement

+ (UIColor*) getColor:(ISColorType) colorType {

    int value;

    switch (colorType) {

        case DarkBackground:
            value = 0x2d3c3f;
            break;          
        case LightBackground:
            value = 0x627376;
            break;
        default:
            value = 0x000000;
            break;
    }

    int r, g, b;
    b = value & 0x0000FF;
    g = ((value & 0x00FF00) >> 8);
    r = ((value & 0xFF0000) >> 16);

    return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1.0f]; 


}

+ (UILabel*) getLabel:(ISLabelType) labelType {

    UILabel *label = [[[UILabel alloc] init] autorelease];
    [label setBackgroundColor:[UIColor clearColor]];    

    switch (labelType) {
        case Headline:
            [label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14]];
            [label setTextColor:[UIColor whiteColor]];
            break;
        case Detail:
            [label setFont:[UIFont fontWithName:@"HelveticaNeue" size:14]];
            [label setTextColor:[UIColor whiteColor]];
            break;  
        default:
            break;
    }

    return label;
}

Because these are all Class methods I don't need to instantiate the ISInterfaceElement class to use them.

I just go:

UILabel aHeadlineLabel = [ISInterfaceElement getLabel:Headline];

You can build something like that for your buttons, all you need to do is build a new UIButton inside your method, set the title and action and lastly return myButton, mush like I do with the labels.

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文