为什么UIRoundedRectButton对isKindOfClass不响应:是[UIButton类]?

时间:2013-01-24 10:01:31

标签: objective-c cocoa-touch uibutton

我在使用

创建的视图中有一个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];给出了所需的结果,但我不能在以后设置按钮类型。

2 个答案:

答案 0 :(得分: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;
    }
}

enter image description here

答案 1 :(得分:0)

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