gpt4 book ai didi

ios - Objective-C - 有没有办法将 UIView 属性作为数组?

转载 作者:行者123 更新时间:2023-11-28 19:32:07 25 4
gpt4 key购买 nike

我需要访问 UIView 数组项(所有 UIView 都将有自己的独立事件)。但是在分配为 @property (assign, nonatomic) UIView *ui_view[MAXIMUM_UIVIEW]; 时,它会导致构建失败。

有没有办法让它按照我正在尝试的方式或其他更好的方式工作?

错误:

enter image description here

代码:

#import "ViewController.h"
const int MAXIMUM_UIVIEW = 50;

@interface ViewController ()

@property (assign, nonatomic) UIView *ui_view[MAXIMUM_UIVIEW];
@property (assign, nonatomic) NSInteger phone_height;
@property (assign, nonatomic) NSInteger phone_width;


-(void)func_draw_wall:(int)argument_count;

@end

@implementation ViewController

-(void)ui_button_submit:(UIButton*)sender {
int __chronological__;

//UIView *ui_view[MAXIMUM_UIVIEW];
//int phone_height = self.view.frame.size.height;
//int phone_width = self.view.frame.size.width;
int phone_height = self.view.frame.size.height;
int phone_width = self.view.frame.size.width;
self.phone_height = phone_height;
self.phone_width = phone_width;


for (__chronological__ = 0 ; __chronological__ <= 50 ; __chronological__++ ) {
[self func_draw_wall:__chronological__];
}
}

-(void)func_draw_wall:(int)__chronological__ {

self.ui_view[__chronological__] =
[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50,50)];

[self.view addSubview:self.ui_view[__chronological__]];
NSLog(@"Mem Address of localView[%d] = %x\n", __chronological__, self.ui_view);
}

-(void)ui_button {
self.view.backgroundColor = [UIColor blackColor];
UIButton *but = [[UIButton alloc] init];
[but setTitle:@"--- Add ---" forState:UIControlStateNormal];
[but setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[but addTarget:self action:@selector(ui_button_submit:)
forControlEvents:UIControlEventTouchUpInside];

int startHeight = 167;
int frameHeight = self.view.frame.size.height - startHeight;
[but setFrame:CGRectMake(0, startHeight, 320, frameHeight)];

[but setTranslatesAutoresizingMaskIntoConstraints:false];
[but setImage:[UIImage imageNamed:@"Image"] forState:UIControlStateNormal];
[self.view addSubview:but];


NSDictionary *viewsDict = @{@"button" : but};
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[button]-0-|"
options:0 metrics:nil views:viewsDict]];

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:but
attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual
toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0
constant:0];
constraint.active = true;
[self.view addConstraint:constraint];
[self.view layoutIfNeeded];

}

- (void)viewDidLoad {
[super viewDidLoad];
[self ui_button];
}


@end

最佳答案

打字NSArray * , 不是 UIView *[]

您应该能够使用通用类型参数,所以 NSArray<UIView *> *

关于ios - Objective-C - 有没有办法将 UIView 属性作为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42765198/

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