【问题标题】:How to filter a NSArray of subviews by tag using NSPredicate?如何使用 NSPredicate 按标签过滤子视图的 NSArray?
【发布时间】:2014-03-03 18:08:34
【问题描述】:

我有这个子视图数组:

<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>>

我不知道如何按标签过滤视图。你们中的任何人都知道如何按标签过滤子视图数组吗?

【问题讨论】:

    标签: ios objective-c uiview nsarray nspredicate


    【解决方案1】:

    我想通了:

    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];
      

      【讨论】:

      • 在这一行 NSPredicate *predicate = [NSPredicate predicateWithFormat: "self.tag in %d", [@101, @102, @103]];我收到错误:预期标识符
      【解决方案3】:

      你会反对用标签跟踪你的观点吗?

      设置标签: self.nameLabel.tag = 13

      检索带有标签的视图: UILabel *myLabel = (UILabel *)[UIView viewWithTag:13];

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多