获取子视图的所有子视图

时间:2014-10-20 23:21:14

标签: ios objective-c uiview uiviewcontroller uibutton

嗯,我的问题是,你可以在图像中看到我有一个子视图,在那个子视图中我有其他视图,在里面我有一个uilabel和一些uibuttons。

viewcontroller layout

我的问题是如何在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 image

感谢所有帮助。

4 个答案:

答案 0 :(得分:3)

您可以创建IBOutlets,也可以在要引用的所有对象中使用唯一标记,并使用viewWithTag获取对象指针的引用。

如果使用viewWithTag,请确保将对象指针检查为nil,以避免运行时崩溃。

要使用viewWithTag,您需要在IB中指定唯一标记,请参见屏幕截图:

enter image description here

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

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

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

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

答案 1 :(得分:2)

我的方式是:

    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的建议。

答案 2 :(得分:2)

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

如果您在- (void)viewDidLoad-(void)viewWillAppear:(BOOL)animated办理登机手续,即使使用viewWithTag:,您也无法访问子视图。

你应该办理入住手续:

- (void)viewDidLayoutSubviews

-(void)viewDidAppear:(BOOL)animated

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

答案 3 :(得分:0)

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

}