1

我有一个UIButton使用创建的视图

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(500, 100, 100, 44);
[self.view addsubview:btn];

当我像这样遍历视图层次结构时:

for (id view in self.view.subviews) 
{        
    NSLog(@"%@",NSStringFromClass([view class]));
    if ([view isKindOfClass:[UIButton class]]) 
    {
        //dsnt get here
        UIButton *btn = view;
    }
}

控制永远不会进入 if 块。

NSLog(@"%@", NSStringFromClass([view class]));打印“UIRoundedRectButton”。我如何检查课程是否是UIButton

UIButton *btn = [[UIButton alloc] init];给出了预期的结果,但我以后无法设置按钮类型。

4

2 回答 2

1

我最终为你写了这个示例......它完美地工作......

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(100, 100, 160, 50);
[myButton setTitle:@"click me" forState:UIControlStateNormal];
[self.view addSubview:myButton];

for (id view in self.view.subviews)
{

    NSLog(@"%@",NSStringFromClass([view class]));

    if ([view isKindOfClass:[UIButton class]])

    {
        //dsnt get here
        UIButton *btn = view;
    }
}

在此处输入图像描述

于 2013-01-24T10:10:40.860 回答
0
for (UIButton *btnNew in self.view.subviews) 
{        
    NSLog(@"%@",NSStringFromClass([btnNew class]));
    if ([btnNew isKindOfClass:[UIButton class]]) 
    {
     //dsnt get here
     UIButton *btn = btnNew;
     }
}
于 2013-01-24T10:09:17.143 回答