开发者

Avoid code repetition when coding UIButtons in iOS

开发者 https://www.devze.com 2023-04-04 01:51 出处:网络
I\'m pretty new to iOS programming and Objective-C in general, so this question has probably been asked quiet a few times. Anyway:

I'm pretty new to iOS programming and Objective-C in general, so this question has probably been asked quiet a few times. Anyway:

In my iOS app, I have several UIButtons that I create the following way:

UIButton *webButton = [UIButton buttonWithType:UIButtonTypeCustom];
[webButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
webButton.frame = CGRectMake(10, 315, 300, 44);
[webButton setBackgroundImage:[UIImage imageNamed:@"WebButton.png"] forState:UIControlStateNormal];
[webButton setBackgroundImage:[UIImage imageNamed:@"WebButtonPressed.png"] forState:UIControlStateHighlighted];

Since I want the title of the button to be easily editable, I then add UILabels to the button instead of having them be part of the image used as the buttons background image.

UILabel *webLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 300, 15)];
webLabel.text = @"Some website";
webLabel.font = [UIFont boldSystemFontOfSize:16.0];
webLabel.textColor = [UIColor colorWithRed:62.0 / 255 green:135.0 / 255 blue:203.0 / 255 alpha:1];
webLabel.textAlignment = UITextAlignmentCenter;
webLabel.backgroundColor = [UIColor clearColor];
[webButton addSubview:webLabel];
[webLabel release开发者_开发百科];

This gets very tedious, when you have to go through this procedure every single time you want to create a new button. What is the best way to simplify this process, so I don't have to repeat myself over and over again when coding buttons?

Thanks.


I suspect want you want to do is to create a subclass of UIButton. That way you write your code once, but use it for any number of buttons. Something a bit like this:

// in your .h file

#import <UIKit/UIKit.h>
@interface WebButton : UIButton
+ (WebButton*) webButtonWithBackgroundImageNamed: (NSString*) imageName title: (NSString*) string andTarget: (id) target;
@end

// in your .m file
#import "WebButton.h"
@implementation WebButton

+ (WebButton*) webButtonWithBackgroundImageNamed: (NSString*) imageName title: (NSString*) string andTarget: (id) target;
{
    WebButton *webButton = [WebButton buttonWithType:UIButtonTypeCustom];
    [webButton addTarget:target action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    webButton.frame = CGRectMake(0, 0, 300, 44);
    [webButton setBackgroundImage:[UIImage imageNamed: imageName] forState:UIControlStateNormal];
    [webButton setBackgroundImage:[UIImage imageNamed: [NSString stringWithFormat:@"%@pressed", imageName]] forState:UIControlStateHighlighted];

     UILabel *webLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 14, 300, 15)];
     webLabel.text = string;
     webLabel.font = [UIFont boldSystemFontOfSize:16.0];
     webLabel.textColor = [UIColor colorWithRed:62.0 / 255 green:135.0 / 255 blue:203.0 / 255 alpha:1];
     webLabel.textAlignment = UITextAlignmentCenter;
     webLabel.backgroundColor = [UIColor clearColor];
     [webButton addSubview:webLabel];
     [webLabel release];

     return webButton;
}

And then to create and add the button to a view something a bit like this:

WebButton* webButton = [WebButton webButtonWithBackgroundImageNamed:@"WebButton" title:@"testing" andTarget:self];
CGRect webButtonFrame = [webButton frame];
webButtonFrame.origin.x = 10;
webButtonFrame.origin.y = 30;
[webButton setFrame:webButtonFrame];
[[self window] addSubview:webButton];
0

精彩评论

暂无评论...
验证码 换一张
取 消