iphone - 从存储在 NSArray 中的对象获取类/类型

标签 iphone ios objective-c

我有不同对象的数组,例如:UISegmentedControl、UILabel 等。
如果我使用“objectAtIndex”,我如何获取对象类型(类)?

我试着这样做:

//while my object count ok
while (i< [allChandeableSubview count]) {

  id random = [allChandeableSubview objectAtIndex:i];

  if([random isKindOfClass:[UISegmentedControl class]]){

     UISegmentedControl *newSegment = [[UISegmentedControl alloc]init];
     newSegment = random;
     newSegment.selectedSegmentIndex = 0;

   }
i++;
}

但我认为这是错误的。请帮助我!

最佳答案

试试这个:)

    for (id object in array) 
    {
        if ([object isKindOfClass:[UISegmentedControl class]])  
        {
             UISegmentedControl *segment = (UISegmentedControl *) object;
             segment.selectedSegmentIndex = 0;
             break;
        }
    }

关于iphone - 从存储在 NSArray 中的对象获取类/类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14752462/

相关文章:

ios设置AppleLanguages并提交到应用商店

iphone - 双重释放会导致崩溃吗?

ios - 检测 iPhone/iPad/iPod touch 的颜色?

objective-c - __block 实例变量问题上的属性。

ios - "No provisioned iOS devices are available with a compatible iOS version"升级后

ios - 替代iOS消息传递的“层”消息传递框架

iphone - 有没有更简单的方法来制作这样的东西?

iphone - 为现有的 Rails 应用程序创建 iphone 和 android 应用程序

iphone - Web 服务或套接字级通信

ios - 如何使用 SKAction 循环播放音乐?