我需要为IOS编写一些gui,但我已经习惯了android所拥有的自动视图ID,在识别视图而不在界面构建器中放置显式标记时我感到很茫然
是否有某种方法可以从界面构建器或其他方式自动识别iOS视图?
也许是一个可以做到这一点的图书馆?
答案 0 :(得分:1)
这就是为什么你有IBOutlets。为您的视图创建一个IBOutlet,如IBOutlet UIView *view1;
等等,然后将IBOutlet链接到Interface Builder中的视图。现在,您可以通过编程方式使用view1
变量,该变量将修改与界面构建器中的IBOutlet关联的视图\
<强>更新强>
以协方式方式创建所有视图:
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
}
答案 1 :(得分:0)
使用属性setTag
为每个视图设置标记,并使用– viewWithTag:
检索相应的视图。 [abaseView subViews]
返回一组子视图。使用Class
和Tag
过滤子视图。