0

UIViewロードされており、ロードViewController後に変更を加えたい (たとえば、それをクリックして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;

このようにして、必要な場所にこれをロードしUIViewましUIViewController'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];
    }

を押すと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

の送信者パラメーター-(void)ExtendedCollapsed:(id)senderはあなたのUIButton *actionViewものであり、CategoryMenu.

CategoryMenuクラスの実装にメソッドがあり、actionView.tagオブジェクトをCategoryMenuオブジェクトに設定tagして、ループ内でそれらを比較し、対応する?-(void)ExtendedCollapsed:(id)senderを見つけることができると思います。CategoryMenu

その場合は、これを行う必要さえありません。呼び出すだけで、押された に対応するオブジェクトsender.superviewが取得されます。の場合は、準備完了です。CategoryMenuactionView[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 に答える