【问题标题】:Getting all subviews of a subview获取子视图的所有子视图
【发布时间】:2014-12-16 01:51:56
【问题描述】:

好吧,我的问题是,正如您在图像中看到的那样,我有一个子视图,在该子视图中我有其他视图,并且在其中我有一个 uilabel 和一些 uibuttons。

我的问题是如何访问 viewDidLoad 中每个子视图上的所有 uibuttons 和 uilabel,以便在应用程序启动时更改它们的某些方面。

在我的测试中,我尝试更改按钮的颜色。

我尝试使用此代码,但没有成功:

for (UIView *view1 in self.view.subviews) {
    NSLog(@"%@----", view1);

    for(UIView *view2 in view1.subviews){

       NSLog(@"%@", view2);

       if ([view2 isKindOfClass:[UIButton class]]) {
          [(UIButton *)view2 setBackgroundColor:[UIColor redColor]];
    }
  }
}

nslog 给出了这个:

感谢所有帮助。

【问题讨论】:

  • 尝试在其中添加一些 NSLog 以确保 view1 和 view2 是您期望的视图。虽然如果你在 Interface Builder 中放置视图并且只需要在应用加载时设置它们的颜色,你不能使用 interface builder 设置它们的颜色吗?
  • 是的,我可以使用界面生成器,但我也可能需要为它们添加边框以便模拟网格,如果我没记错的话,界面生成器没有添加边框的选项按钮。我将在问题上添加 nslog
  • 就个人而言,我会创建 UIView 的子类并为您要更改的每个子视图创建属性。 IMO,这比遍历多个子视图数组更清晰、更具可读性。

标签: ios objective-c uiview uiviewcontroller uibutton


【解决方案1】:

您可以创建IBOutlets,或者在您希望引用的所有对象中使用唯一标签,然后使用viewWithTag 来获取对对象指针的引用。

如果您使用viewWithTag,请务必检查对象指针是否为 nil,以避免运行时崩溃。

要使用viewWithTag,你需要在IB中分配一个唯一的Tag,见截图:

如您所见,我的UILabel 的标记为 253,要访问它的指针对象,我必须使用 viewWithTag

UILabel *myTagLabel = (UILabel*)[self.view viewWithTag:253];

正如我在使用该指针之前建议的那样,检查nil

if (myTagLabel) {
     //object pointer retrieved successfully
     myTagLabel.text = @"Hello";
}

【讨论】:

  • 感谢您的提示,我考虑过使用 IBOutlets,但由于它仍然有很多按钮,我不想为每个按钮创建一个插座。所以我去使用@property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *conj_btn; 然后for (UIButton *botao in self.conj_btn) { [botao setBackgroundColor:[UIColor redColor]]; } 就成功了。在我给你答案复选标记之前,我会等着看@carlodurso 是否可以帮助我解决我的疑问。我也尝试使用 viewWithTag 但我现在才如何在单元格上使用它。如何更改所有带有标签的按钮?
  • 非常感谢,我会试试的,并开始检查我的代码中的 nil。
【解决方案2】:
Can you please try this.

int screensize;
UIScrollView *scroll;
-(void)setUpView{
    screensize = self.view.frame.size.width;
    scroll = [[UIScrollView alloc]init];
    scroll.frame = CGRectMake(0, 64, screensize, SCROLLHEIGHT);
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.showsVerticalScrollIndicator = NO;

    int x = 5;

    for (int i=0; i<5; i++) {

        UIView *bgview = [[UIView alloc]init];
        bgview.frame = CGRectMake(x, 5, VIEWWIDTH, VIEWHEIGHT);
        bgview.backgroundColor = [UIColor whiteColor];

        UIImageView *carimage = [[UIImageView alloc]init];
        carimage.frame = CGRectMake(0, 5, VIEWWIDTH, IMAGEHEIGHT);
        [carimage setImage:[UIImage imageNamed:@"User"]];
        carimage.backgroundColor = [UIColor whiteColor];
        carimage.contentMode = UIViewContentModeScaleAspectFit;

        UILabel *underlinelbl = [[UILabel alloc]init];
        underlinelbl.frame = CGRectMake(0, carimage.frame.origin.y+carimage.frame.size.height+2, VIEWWIDTH, UNDERLINELABELHEIGHT);
        underlinelbl.backgroundColor = [UIColor lightGrayColor];

        UILabel *textlbl = [[UILabel alloc]init];
        textlbl.frame = CGRectMake(0, underlinelbl.frame.origin.y+underlinelbl.frame.size.height, VIEWWIDTH, TEXTLABELHEIGHT);
        textlbl.font = [UIFont regularFont10];
        textlbl.text = @"name";
        textlbl.textColor = [UIColor customBlackColor];
        textlbl.textAlignment = NSTextAlignmentCenter;

        UILabel *numberlbl = [[UILabel alloc]init];
        numberlbl.frame = CGRectMake(0,textlbl.frame.origin.y+textlbl.frame.size.height ,VIEWWIDTH , TEXTLABELHEIGHT);
        numberlbl.text = @"000";
        numberlbl.font = [UIFont regularFont10];
        numberlbl.textColor = [UIColor customBlackColor];
        numberlbl.textAlignment = NSTextAlignmentCenter;

        UIButton *carname = [[UIButton alloc]init];
        carname.frame = CGRectMake(bgview.frame.origin.x, bgview.frame.origin.y, VIEWWIDTH, VIEWHEIGHT);
        carname.tag = i+1;
        [carname addTarget:self action:@selector(buttonclick:) forControlEvents:UIControlEventTouchUpInside];
        [carname setBackgroundColor:[UIColor clearColor]];


        [scroll addSubview:bgview];
        [bgview addSubview:carimage];
        [bgview addSubview:underlinelbl];
        [bgview addSubview:textlbl];
        [bgview addSubview:numberlbl];
        [scroll addSubview:carname];

        x += bgview.frame.size.width+5;
    }
    scroll.contentSize = CGSizeMake( x, SCROLLHEIGHT);
    scroll.backgroundColor = [UIColor customLightGrayColor];
    [self.view addSubview:scroll];
}

-(void)buttonclick:(id)sender{

    UIButton *selectedButton = (UIButton *)sender;

    for (UIView *subView in scroll.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton*)subView;
            if (btn.tag == selectedButton.tag) {
                btn.layer.borderWidth = 1.0f;
                btn.layer.borderColor = [UIColor darkGrayColor].CGColor;
            }else{
                btn.layer.borderWidth = 1.0f;
                btn.layer.borderColor = [UIColor clearColor].CGColor;
            }
        }
    }

}

【讨论】:

    【解决方案3】:

    我认为这从 Xcode 6 开始发生,这不是因为你有太多的嵌套视图。

    如果您签入- (void)viewDidLoad-(void)viewWillAppear:(BOOL)animated,您将无法访问子视图,即使使用viewWithTag:

    您应该办理登机手续:

    - (void)viewDidLayoutSubviews

    -(void)viewDidAppear:(BOOL)animated
    

    您的子视图将在这些方法中被识别。

    【讨论】:

    • 不错的一个。不知道那件事。我现在想,当同样的问题再次出现时,我是否会记住你的答案。
    【解决方案4】:

    我的做法是:

        for (int i=0; i<[self.view.subviews count]; i++) {
    
            if ([[self.view.subviews objectAtIndex:i] isKindOfClass:[UIButton class]]) {
                UIButton *button = (UIButton *)[self.view.subviews objectAtIndex:i];
                [button setBackgroundColor:[UIColor redColor]];
            }
        }
    

    为了更好地控制每个子视图,我会遵循 @Lefteris 的建议。

    【讨论】:

    • 我认为这会起作用,但奇怪的是我添加了一个 nslog 来显示 self.view.subviews,它只显示:“<_uilayoutguide: ..... : ...... if>
    • 我明白了。如果您附上示例项目,我们可能会为您提供进一步的帮助。
    • 不需要,我看到你的代码运行良好,并且做了它应该做的事情。我看到在视图中有很多视图给我带来了问题,但现在我使用 IBOutletCollection 解决了我的问题。感谢您的帮助和代码。将来它可能会派上用场。
    猜你喜欢
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多