gpt4 book ai didi

ios - 复制 UIView,其中包含 UIButtons

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:04:14 26 4
gpt4 key购买 nike

我正在尝试了解如何复制其中包含一组 uibutton 的 uiview。

一直在尝试关注这个问题/答案,但我真的很困惑 atm:

Make a deep copy of a UIView and all its subviews

基本上是尝试制作一个显示两组带按钮的 uiviews 的 vc。这是常规 View 的样子:

Points of team 1:
+ + + +
1 2 3 P
- - -


Points of team 2:
+ + + +
1 2 3 P
- - -

我需要复制一份。我可能可以直接将对象拖到 View Controller 上,但如果我创建另一个副本,它将有太多的 IBactions。

关于如何处理这个问题的想法?

编辑:这就是我解决添加多个按钮的方法

Add a multiple buttons to a view programmatically, call the same method, determine which button it was

最佳答案

首先,我会创建一个名为 PointsView 之类的 UIView 子类。

这看起来像这样......

Points of [name label]:
+ + + +
1 2 3 P
- - -

它将具有类似 NSString *teamName 的属性,并根据相关标签设置这些属性。

它也可能具有 NSUInteger score 的属性,因此您可以设置 PointView 对象的分值。

这完全独立于您的 UIViewController。

现在,在您的 UIViewController 子类中,您可以执行类似...

PointsView *view1 = [[PointsView alloc] initWithFrame:view1Frame];
view1.teamName = @"Team 1";
view1.score1 = 1;
view1.score2 = 2;
view1.score3 = 3;
[self.view addSubView:view1];

PointsView *view2 = [[PointsView alloc] initWithFrame:view2Frame];
view2.teamName = @"Team 2";
view2.score1 = 1;
view2.score2 = 2;
view2.score3 = 3;
[self.view addSubView:view2];

现在不涉及复制。您只需创建一个对象的两个实例。

编辑

正在创建您的 View 子类...

创建 View 子类的最简单方法是执行以下操作...

创建文件...PointsView.m 和 PointsView.h

.h 文件看起来像这样......

#import <UIKit/UIKit.h>

@interface PointsView : UIView

@property (nonatomic, strong) UILabel *teamNameLabel;
// other properties go here...

@end

.m 看起来像这样......

#import "PointsView.h"

@implementation PointsView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.teamNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 21)];
self.teamNameLabel.backgroundColor = [UIColor clearColor];
[self addSubView:self.teamNameLabel];

// set up other UI elements here...
}
return self;
}

@end

然后在您的 View Controller 中,您在代码中将 PointsView 添加到它(即不使用 Interface builder),就像这样...

- (void)viewDidLoad
{
[super viewDidLoad];

PointsView *pointsView1 = [[PointsView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
pointsView1.teamNameLabel.text = @"Team 1";
[self.view addSubView:pointsView1];

// add the second one here...
}

您也可以在 Interface Builder 中创建和添加这些 View ,但在这里解释起来要困难得多。

如果您以这种方式设置它,那么您可以使用 IB 来设置您的 UIViewController 的其余部分。只是不要使用 IB 来设置 PointsViews。它不适用于我在此处展示的方式。

关于ios - 复制 UIView,其中包含 UIButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16589097/

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