gpt4 book ai didi

ios - 在 iOS 中编写 UIButton 时避免代码重复

转载 作者:行者123 更新时间:2023-11-29 11:20:41 25 4
gpt4 key购买 nike

总的来说,我对 iOS 编程和 Objective-C 还很陌生,所以这个问题可能已经被问过几次了。无论如何:

在我的 iOS 应用程序中,我有几个 UIButtons,它们是通过以下方式创建的:

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];

因为我希望按钮的标题易于编辑,所以我将 UILabel 添加到按钮,而不是让它们成为用作按钮背景图像的图像的一部分。

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];

当您每次想要创建一个新按钮时都必须执行此过程时,这会变得非常乏味。简化此过程的最佳方法是什么,这样我就不必在对按钮进行编码时一遍又一遍地重复自己的话?

谢谢。

最佳答案

我怀疑您想要做的是创建 UIButton 的子类。这样一来,您只需编写一次代码,就可以将其用于任意数量的按钮。有点像这样:

// 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;
}

然后创建按钮并将其添加到 View 中,有点像这样:

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];

关于ios - 在 iOS 中编写 UIButton 时避免代码重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7368174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com