Ios 已加载对UIView元素的更改
我将Ios 已加载对UIView元素的更改,ios,uiview,Ios,Uiview,我将UIView加载到ViewController中,并希望在加载后进行更改(例如,当您单击该UIView以更改文本颜色时) 通过这种方式,我已将此UIView加载到所需的UIViewController的中 -(void)showSubviews { int x = 0; for (int i = 0; i < array.count ; i++) { NSArray* nibViews = [[NSBundle mainBundle] loa
UIView
加载到ViewController
中,并希望在加载后进行更改(例如,当您单击该UIView
以更改文本颜色时)
通过这种方式,我已将此UIView
加载到所需的UIViewController的
中
-(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];
}
未看到先前加载的ui视图的任何更改
有人能帮我提些建议吗?
谢谢在-(void)ExtendedCollapsed:(id)sender
中的sender参数是您的ui按钮*actionView
而不是类别菜单
我假设您在CategoryMenu
类实现中有一个方法,它将actionView.tag
设置为CategoryMenu
对象tag
,这样您就可以在-(void)ExtendedCollapsed:(id)sender>中的循环中比较它们,并找到相应的CategoryMenu
如果是这样,你甚至不必这样做,只要调用sender.superview
,你就会得到与按下的actionView
对应的CategoryMenu
对象。如果[sender.superview是类:[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];
}
}
}
在showsubviews中,您在数组中填充什么?nslog for tempUserView正在显示值?是的,此nslog正在显示信息。我写这行是因为我想检查UIView的地址。Jonathan-我使用此数组保留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");
}
}
}
- (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];
}
}
}