gpt4 book ai didi

iphone - 通过类中的方法创建标签

转载 作者:行者123 更新时间:2023-11-29 04:39:31 26 4
gpt4 key购买 nike

我正在慢慢开发一个自定义按钮(同时学习如何实现类等)。

我有一个 ViewController,它导入 SuperButton.h (UIControl),然后创建 SuperButton 的实例。 (正如 NSLog 所证明的那样,这是有效的。)

但是我无法获取 SuperButton 中显示标签的方法。我认为这可能与“.center”值或“addSubview”命令有关?

我非常感谢您的帮助。谢谢。

这是我的 SuperButton.m 代码:​​

#import "SuperButton.h"

@implementation SuperButton
@synthesize firstTitle;
@synthesize myLabel;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void) shoutName{
NSLog(@"My name is %@", firstTitle);

self.backgroundColor = [UIColor blueColor];
CGRect labelFrame = CGRectMake(0.0f, 0.0f, 100.0f, 50.0f);
self.myLabel = [[UILabel alloc] initWithFrame:labelFrame];
self.myLabel.text = @"Come on, don't be shy.";
self.myLabel.font = [UIFont italicSystemFontOfSize:14.0f];
self.myLabel.textColor = [UIColor grayColor];
self.myLabel.center = self.center;
[self addSubview:self.myLabel];
}

这是我的 ViewController 中的代码:

- (void) makeButton{
SuperButton *button1 = [[SuperButton alloc] init];
button1.firstTitle = @"Mr. Ploppy";
[button1 shoutName];
}

(编辑:)以防万一,这是 SuperButton.h 代码:

#import <UIKit/UIKit.h>
@interface SuperButton : UIControl

@property (nonatomic, strong) NSString *firstTitle;
@property (nonatomic, strong) UILabel *myLabel;

- (void) shoutName;

@end

最佳答案

我在其他地方找到了答案。我需要添加Subview“按钮”。我的工作代码现在如下所示:

- (void) makeButton{
SuperButton *button1 = [[SuperButton alloc] init];
button1.firstTitle = @"Mr. Ploppy";
[button1 shoutName];
[self.view addSubview:button1.myLabel];
[self.view sendSubviewToBack:button1.myLabel];
}

关于iphone - 通过类中的方法创建标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10601427/

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