0

我有一个UIView加载到 aViewController并希望在加载后带来更改(例如,当您单击它UIView以更改文本颜色时)

      @interface CategoryMenu : UIView
    {
        UIImageView *img;
        UIButton *disclosureBtn;
        UIButton *actionView;
        UILabel  *titleLabel;
    }
 @property (nonatomic,retain) IBOutlet UIImageView *img;
 @property (nonatomic,retain) IBOutlet UIButton *disclosureBtn;
 @property (nonatomic,retain) IBOutlet UIButton *actionView;
 @property (nonatomic,retain) IBOutlet UILabel  *titleLabel;

通过这种方式,我已将其加载UIViewUIViewController's需要的地方。

  -(void)showSubviews
{
    int x = 0;
    for (int i = 0; i < array.count ; i++)
    {
        NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"CustomMenu" owner:self options:nil];

        CategoryMenu* tempUserView = [nibViews objectAtIndex:0];
        tempUserView.userInteractionEnabled = YES;
        [tempUserView setFrame:CGRectMake(0, x, 280, 50)];
         x+=60;

        tempUserView.tag = i;

        tempUserView.titleLabel.text = [array objectAtIndex:i];
        [tempUserView.actionView addTarget:self action:@selector(ExtendedCollapsed:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:tempUserView];
    }

现在,当您按下 a 时UIView,除了动作本身之外,我还想更改UILabel文本的颜色。

-(void)ExtendedCollapsed:(id)sender
{
    NSLog(@"[self.view subviews] = %@",[self.view subviews]);
    UIView *view1 = (UIView *)sender;

    for(UIView *view in [self.view subviews])
    {
        NSLog(@"view = %@ \n ", view);

        if([view isKindOfClass:[CategoryMenu class]])
        {
            if (view.tag == view1.tag)
            {
                NSLog(@"We find the view !!!!");

                CategoryMenu* tempUserView = (CategoryMenu*)view1;

                 NSLog(@"tempUserView = %@ \n ", tempUserView);

                tempUserView.titleLabel.text = @"Text Changed";
                tempUserView.titleLabel.textColor = [UIColor whiteColor];

                NSLog(@"tempUserView.titleLabel.text = %@",tempUserView.titleLabel.text);

                break;
            }
        }
        else
        {
            NSLog(@"view is not a button");
        }
    }

}

看不到UIViews之前加载的任何变化。

有人可以帮我一些建议吗?谢谢

4

1 回答 1

2

中的 sender 参数-(void)ExtendedCollapsed:(id)sender是您的UIButton *actionView,而不是CategoryMenu.

我想您的CategoryMenu类实现中有一个方法将您设置actionView.tag为您的CategoryMenu对象tag,以便您可以在循环中比较它们-(void)ExtendedCollapsed:(id)sender并找到对应的CategoryMenu?

如果是这样,您甚至不必这样做,只需调用sender.superview即可获得与CategoryMenupress 对应的对象actionView。如果[sender.superview isKindOfClass:[CategoryMenu class]] == TRUE,你很高兴。

然后,在更改 的颜色后tempUserView.titleLabel,尝试调用[tempUserView setNeedsLayout]

该方法现在应该如下所示:

- (void)ExtendedCollapsed:(id)sender {
    if ([sender isKindOfClass:[UIButton class]]) {
        if ([sender.superview isKindOfClass:[CategoryMenu class]]) {
            CategoryMenu *tempUserView = (CategoryMenu *)sender.superview;
            tempUserView.titleLabel.text = @"Text Changed";
            tempUserView.titleLabel.textColor = [UIColor whiteColor];
            [tempUserView setNeedsLayout];
        }
    }
}
于 2013-02-19T12:59:01.007 回答