iOS 8.1仅间歇性地显示UITextView

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 8.1仅间歇性地显示UITextView相关的知识,希望对你有一定的参考价值。

我有一个模板测验应用程序,已经在应用商店以各种形式存在了一段时间,一般来说它收到良好的评论,没有错误报告。

最近我有两个错误报告,人们使用带有ios 8.1.2或8.1.3的iPad,并说现在我用来显示问题的UITextView是空白的。

我无法复制这个错误,但如果有人能够对它有所了解,我将不胜感激。

对象questionsuserAnswer不是零,所以它肯定是一个UITextView问题。

控制器在这里:

@interface ReviewController ()
@property (strong, nonatomic) IBOutlet UITextView *output;

@end

@implementation ReviewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setReviewText];
   // Do any additional setup after loading the view.
}

-(void)setReviewText{

    NSArray* questions = [[NSArray alloc] init];
    questions = [_manager getAllQuestions];
    NSMutableArray* userAnswer = [[NSMutableArray alloc] init];
    userAnswer = [self answersAttributed];

    if(questions == nil)
        _output.text = @"Questions Error";

    if (userAnswer == nil)
    {
        _output.text = @"Error";
    }

    NSMutableAttributedString *mutableAttString = [[NSMutableAttributedString alloc] init];

    int i = 0;
    NSAttributedString *linebreak =[[NSAttributedString alloc] initWithString:@"
"];
    for (Question* q in questions)
    {
        if (i == [userAnswer count])
            break;

        NSAttributedString *qtext =[[NSAttributedString alloc] initWithString:q.questionText];
        NSAttributedString *ctext =[[NSAttributedString alloc] initWithString:@"Correct Answer:
"];

        NSAttributedString *atext =[[NSAttributedString alloc] initWithString:q.answerText];
        NSAttributedString *ytext =[[NSAttributedString alloc] initWithString:@"Your Answer:
"];
        NSAttributedString *utext =[[NSAttributedString alloc] initWithAttributedString:userAnswer[i]];
        [mutableAttString appendAttributedString:qtext];
        [mutableAttString appendAttributedString:linebreak];
        [mutableAttString appendAttributedString:ctext];
        [mutableAttString appendAttributedString:atext];
        [mutableAttString appendAttributedString:linebreak];
        [mutableAttString appendAttributedString:ytext];
        [mutableAttString appendAttributedString:utext];
        [mutableAttString appendAttributedString:linebreak];
        [mutableAttString appendAttributedString:linebreak];
        i++;
    }
    NSAttributedString* outText = [[NSAttributedString alloc] init];
    outText = [mutableAttString mutableCopy];
    _output.attributedText = outText;
    [_output setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
}


-(NSMutableArray*)answersAttributed
{
    NSMutableArray* ansAtt = [[NSMutableArray alloc] init];
    NSArray* questions = [[NSArray alloc] init];
    questions = [_manager getAllQuestions];

    NSArray* userAnswers = [[NSArray alloc] init];
    userAnswers = [_manager getAllUserAnswers];


    if ([questions count] != [userAnswers count])
    {
        return ansAtt;
    }


    int i = 0;
    for (NSString* userAnswer in userAnswers )
    {
        if([[questions[i] answerText] isEqualToString:userAnswer] )
        {
            NSDictionary *attributes = @{NSBackgroundColorAttributeName:[UIColor greenColor]};
            NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:userAnswer attributes:attributes];
            [ansAtt addObject:attrString];
        }
        else
        {
            NSDictionary *attributes = @{NSBackgroundColorAttributeName:[UIColor redColor]};
            NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:userAnswer attributes:attributes];
            [ansAtt addObject:attrString];

        }
        i++;
    }

    return ansAtt;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"review2results"])
    {
        // Get reference to the destination view controller
        ReviewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setManager:_manager];
    }
}
答案

使用自动布局时,如果您没有足够的约束来完全指定大小和位置,那么有时您会看到并且有时您不会。未指定的约束可以具有随机值,例如0高度或屏幕外位置。

检查以确保完全指定约束。

另一答案

在我的情况下,在textview中标记为Non-EditableNon-SelectableStoryboardiOS 8.1上具有相同的奇怪行为(在iOS 9+中没有问题)。

- (void)viewDidLoad {
    [super viewDidLoad];
    textview.attributedText = [[NSAttributedString alloc] initWithString:@"non-nil"]];
    value = textview.attributedText;
    //^ value is nil !!!
}

修正了这个问题:

- (void)viewDidLoad {
    [super viewDidLoad];
    textview.editable = YES;
    textview.attributedText = [[NSAttributedString alloc] initWithString:@"non-nil"]];
    textview.editable = NO;
    value = textview.attributedText;
    //^ value is @"non-nil" now !!!
}

以上是关于iOS 8.1仅间歇性地显示UITextView的主要内容,如果未能解决你的问题,请参考以下文章

ios UITextView 显示不正确(错误?)

动态 UIScrollView 中的动态 UITextView - iOS

Chromecast 会间歇性地自动暂停 iOS 中的会话?

ios - 带有 UITextView 的 UITapGestureRecognizer

如何在 ios 中点击 UITextView 打开 Pickerview(dropdown)?

UITextView 属性检查器未显示完整的属性集