How to filter a NSArray of subviews by tag using NSPredicate?

本小妞迷上赌 提交于 2019-12-11 09:17:37

问题


I have this array of subViews:

<UIButton: 0xa2b1600; frame = (41 20; 42 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0xa2b06e0>>,
<UIButton: 0xa2b1290; frame = (121 694; 69 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0xa2b0dc0>>,
<SequenceViews: 0xb16fba0; frame = (62 393; 280 323); tag = 100; gestureRecognizers = <NSArray: 0xb16dc90>; animations = { position=<CASpringAnimation: 0xb16c990>; }; layer = <CALayer: 0xb168880>>,
<SequenceViews: 0xb171c30; frame = (62 52; 280 323); tag = 101; gestureRecognizers = <NSArray: 0xb176290>; animations = { position=<CASpringAnimation: 0xb16bb50>; }; layer = <CALayer: 0xb173980>>,
<SequenceViews: 0xb17d430; frame = (372 222; 280 323); tag = 102; gestureRecognizers = <NSArray: 0xb17d140>; animations = { position=<CASpringAnimation: 0xb176070>; }; layer = <CALayer: 0xb16b060>>,
<SequenceViews: 0xb178ba0; frame = (682 393; 280 323); tag = 104; gestureRecognizers = <NSArray: 0xb178e80>; animations = { position=<CASpringAnimation: 0xb180680>; }; layer = <CALayer: 0xb1795c0>>,
<SequenceViews: 0xb16a9f0; frame = (682 52; 280 323); tag = 105; gestureRecognizers = <NSArray: 0xb1819d0>; animations = { position=<CASpringAnimation: 0xb16aad0>; }; layer = <CALayer: 0xb182710>>

By I can not figure out how can I filter the views by tag.Any of you knows how I can filter the array of subviews by tag?


回答1:


I figure out:

NSPredicate *tagPredicate = [NSPredicate predicateWithFormat:@"self.tag >= 100"];
NSArray *resultArray = [[self.view subviews] filteredArrayUsingPredicate:tagPredicate];



回答2:


UIView *mainView = self.view;

NSArray *subViews = [mainView subViews];
NSPredicate  *predicate = [NSPredicate predicateWithFormat: @"self.tag in %@", @[@101, @102, @103]];
NSArray *filteredViews = [subViews filteredArrayUsingPredicate: predicate];



回答3:


Would you be opposed to tracking your views with tags?

to set tag: self.nameLabel.tag = 13

to retrieve a view with a tag: UILabel *myLabel = (UILabel *)[UIView viewWithTag:13];



来源:https://stackoverflow.com/questions/22154067/how-to-filter-a-nsarray-of-subviews-by-tag-using-nspredicate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!