0

我目前正在构建一个配置文件对象,它可以返回包含用户所有信息的视图。我希望能够在许多不同的地方重用代码,所以我试图在 NSObject 中构建它,但是当我将它添加为我的视图的子视图时,我无法单击按钮。这些按钮是用一个矩形创建的,然后在它们上面放一个 UILabel。如果我将代码复制到我试图放置的地方,它就可以工作。我也尝试过创建一个委托,但这也没有做任何事情。

编辑

UIView *profile = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 175.0)];
UIView *votesView = [[UIView alloc] initWithFrame:CGRectMake(60.0, 125.0, 60.0, 50.0)];
UILabel *votesLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 0.0, 50.0, 40.0)];
votesView.backgroundColor = [UIColor lightGrayColor];
votesLabel.text = votes;
votesLabel.textAlignment = UITextAlignmentCenter;
votesLabel.backgroundColor = [UIColor clearColor];
votesLabel.tag = 2;
[votesView addSubview:votesLabel];
[profile addSubview:votesView];
4

1 回答 1

1

为什么要子类化 NSObject,可以子类化 UIView 并实现要包含的所有视图,

另外,我看不到您是如何添加按钮的,我只能看到代码中的标签。

但建议您需要提及动作和目标,对于目标,您可以在其中添加您的类,它被称为

[btn addTarget:<class where you will add UIView returned by your class> action:<SEL in your class where you will add UIView> forControlEvents:<UIControlEvents>]
于 2012-10-10T18:41:22.800 回答