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

标签 ios view uniqueidentifier

我需要为 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/

相关文章:

ios - 语义问题太多。 Xcode 8.2.1

django - 在另一个应用程序 View 中导入 django 应用程序 View

android - 如何使用另一个布局的新实例来扩充布局?

model-view-controller - 使用MVC,一个人应该如何处理 View 之间的通信?在模型之间?

android - 智能手机设备的唯一设备标识符

ruby - Ruby 中的唯一系统 ID ...?

ios - swift&parse.com : converting String to AnyObject?

iphone - 如何在 iOS 中更改 ePub 图书的字体颜色和字体样式?

ios - 查看从不加载

sql - 对于返回多于 1 个值的 SQL 选择,当 Id 为 GUID 时,它们如何排序?