gpt4 book ai didi

ios - 自动 View id 就像在 android 中一样

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

我需要为 IOS 编写一些 gui,但我已经习惯了 android 拥有的自动 View ID,以至于在不在界面构建器中放置显式标签的情况下识别 View 时我不知所措

是否有某种方法可以从界面构建器或其他方式自动识别 iOS View ?

也许图书馆可以做到这一点?

最佳答案

这就是您拥有 IBOutlets 的原因。为您的 View 创建一个 IBOutlet,例如 IBOutlet UIView *view1; 等等,然后将 IBOutlet 链接到您在 Interface Builder 中的 View 。现在您可以以编程方式使用 view1 变量,这将修改与界面构建器中的 IBOutlet 关联的 View \

更新

以编程方式创建所有 View ,如下所示:

for(int i = 0; i < 20; i++){
//set your x and y coordinates with some awesome maths, maybe you want to create a grid, so update the x coordinate accordingly
UIView *view = [UIView alloc] initWithFrame:CGRectMake(whateverframe)];
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
[imageView addGestureRecognizer:singleTapGesture];

[singleTapGesture release]; //remove line if you have an ARC project

view.tag = i; //this is just for you since you like to handle your views with id tags
}

然后您的一个方法将处理所有代码的点击

- (void) viewTapped:(UIGestureRecognizer*)recognizer
{
UIView *viewTapped = recognizer.view; // This is the view associated with gesture recognizer.
int id = viewTapped.tag; //heres the tag id do whatever you like

//do something with either that view variable which will return the view that was tapped
//maybe you wanted to change the color of it or something or change the contents of it

//or you can do something with the id tag of that view.

//or maybe you just want to handle it with if else statements like so
//if(viewTapped.tag == 1) //do this
//elseif(viewTapped.tag == 2) // etc etc
}

关于ios - 自动 View id 就像在 android 中一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21155959/

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