2013-10-05 17 views
-1

我正在更新iOS 6到iOS 7的一個應用程序。但是我得到了關於類的問題。在iOS 6中,它顯示類名稱自定義按鈕,但在iOS 7中顯示_UITextContainerView。在iOS 7中檢查類的種類

for (UIView *subView in textViewButton.subviews) 
     { 
      NSLog(@"yourObject is a: %@", [subView class]); 
      @autoreleasepool 
      { 
       if([subView isKindOfClass:[CustomButton class]]) 
       { 
        CustomButton *button = (CustomButton*)subView; 
        button.backgroundColor = [UIColor redColor]; 

        [button setType:kButtonTypeQuestion]; 
        button.titleLabel.font = kFontForContentPhone; 

        if (button.tag == 62254 || button.tag == 62263) 
        { 
         CGRect tempFrame = button.frame; 
         tempFrame.origin.x = button.frame.origin.x - 3.0f; 


         button.frame = tempFrame; 
        } 
        if (self.soundFile != nil) 
        { 

         CGRect tempFrame = button.frame; 
         tempFrame.size.width = button.frame.size.width + 28.0f; 
         button.frame = tempFrame; 
         [button setContentEdgeInsets:UIEdgeInsetsMake(5.0f, 30.0f, 5.0f, 10.0f)]; 
        } 
       } 
      } 
      } 

更新

編譯馬克 - 文本段落iPhone

self.questionScrollView = [[CustomScrollView alloc] initWithFrame:CGRectMake(0, currentYPosition, self.frame.size.width, self.frame.size.height - currentYPosition - 60.0f)]; 
    self.questionScrollView.showsVerticalScrollIndicator = NO; 

    self.questionsContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.questionScrollView.frame.size.width, self.questionScrollView.frame.size.height)]; 
    currentYPosition = 0; 

     for (Question *question in self.questions) 
     { 
      @autoreleasepool 
      { 
       self.ttmItem = [[TapToMoveItem alloc] initWithFrame:CGRectMake(0, currentYPosition, self.frame.size.width, self.frame.size.height)]; 
       self.ttmItem.ttmDelegate = self; 
       self.ttmItem.variant = self.exercise.variant; 
       ttmItem.x_Position = question.x_position; 
       ttmItem.y_Position = question.y_position; 

       if ([question.sound_file length] > 0 || question.sound_file != nil) 
        self.ttmItem.hasAudio = YES; 
       else 
        self.ttmItem.hasAudio = NO; 

       if ([kPrefixImage isEqualToString:@"preint_"] && ([self.exercise.exercise_id integerValue] == 54 || [self.exercise.exercise_id integerValue] == 91)) { 
        ttmItem.soundFile = self.exercise.header_title; 
       } 

       self.ttmItem.prefix = question.prefix; 
       self.ttmItem.text = question.text; 
       self.ttmItem.longestPrefix = longestPrefix; 

       self.ttmItem.longestAnswer = longestAnswer; 
       self.ttmItem.buttonWidth = sizeForButton.width; 

       //get and set answers 
       ttmItem.answers = [self.answers objectForKey:question.question_id]; 

       [self.ttmItem createForPhone]; 

       [self.questionsContainer addSubview:self.ttmItem]; 

       currentYPosition += ttmItem.frame.size.height + SPACE_BET_VIEWS; 

       //   itemHeight = ttmItem.frame.size.height; //hack 
       //   itemWidth = ttmItem.frame.size.width; 

       ttmCount++; 


       if ([ttmItem.arrayOfTextViewButtons count] > 0) { 
        [dictionaryOfItemButtons setObject:ttmItem.arrayOfTextViewButtons forKey:question.question_id]; 
       } 

      } 

     } 


    self.questionsContainer.frame = CGRectMake(questionsContainer.frame.origin.x, questionsContainer.frame.origin.y, questionsContainer.frame.size.width, currentYPosition); 

    [self.questionScrollView setContentSize:self.questionsContainer.frame.size]; 

    [self.questionScrollView addSubview:self.questionsContainer]; 
    [self addSubview:self.questionScrollView]; 
    [self setScrollEnabled:NO]; 



} 

我無法找到爲什麼它顯示的任何對象的類名稱不同。此外,我沒有發現任何關於谷歌的問題。如果有人對此有所瞭解,請幫助我。

在此先感謝。

+0

的問題是不能以'isKindOfClass',這是事實這個班是不同的。顯示如何創建這些視圖。 – trojanfoe

+0

@trojanfoe請檢查我的更新代碼 –

+0

我沒有看到在新代碼中添加到'self.textViewButton'的子視圖。 – trojanfoe

回答

2

我已經這樣做解決了這個問題:

for (UIView *subView in textViewButton.textInputView.subviews) 
      { 
       // const char* className = class_getName([yourObject class]); 
       NSLog(@"yourObject is a: %@", [subView class]); 
       @autoreleasepool 
       { 
        if([subView isKindOfClass:[CustomButton class]]) 
        { 
         CustomButton *button = (CustomButton*)subView; 
         button.backgroundColor = [UIColor clearColor]; 

         [button setType:kButtonTypeQuestion]; 

         button.titleLabel.font = kFontForContentPhone; 

         if (button.tag == 62254 || button.tag == 62263) 
         { 
          CGRect tempFrame = button.frame; 
          tempFrame.origin.x = button.frame.origin.x - 3.0f; 


          button.frame = tempFrame; 
         } 
         if (self.soundFile != nil) 
         { 

          CGRect tempFrame = button.frame; 
          tempFrame.size.width = button.frame.size.width + 28.0f; 
          button.frame = tempFrame; 
          [button setContentEdgeInsets:UIEdgeInsetsMake(5.0f, 30.0f, 5.0f, 10.0f)]; 
         } 
        } 
       } 
       } 
+0

非常好的答案....... –

0

循環遍歷所有子視圖,直到找到對象。如果你碰巧找對象退出循環,以節省不必要的資源

for (UIView *subView in textViewButton.subviews) 
{ 
    NSLog(@"yourObject is a: %@", [subView class]); 
    @autoreleasepool 
    { 
     if([subView isKindOfClass:[CustomButton class]]) 
     { 
      CustomButton *button = (CustomButton*)subView; 
      button.backgroundColor = [UIColor redColor]; 

     [button setType:kButtonTypeQuestion]; 
     button.titleLabel.font = kFontForContentPhone; 

     if (button.tag == 62254 || button.tag == 62263) 
     { 
      CGRect tempFrame = button.frame; 
      tempFrame.origin.x = button.frame.origin.x - 3.0f; 


      button.frame = tempFrame; 
     } 
     if (self.soundFile != nil) 
     { 
      CGRect tempFrame = button.frame; 
      tempFrame.size.width = button.frame.size.width + 28.0f; 
      button.frame = tempFrame; 
      [button setContentEdgeInsets:UIEdgeInsetsMake(5.0f, 30.0f, 5.0f, 10.0f)]; 
     } 

    } 
    else if ([subView isKindOfClass:NSClassFromString(@"_UITextContainerView")]) { 
     // do a another loop 
     for (UIView *innerSubViews in subView.subviews) { 
      if([subView isKindOfClass:[CustomButton class]]) 
      { 
       // call your custom button method here 
       break; // stop inner loop 
      } 
     } 
    } 
} 
}