我有一个模板测验应用程序,已经在应用商店以各种形式存在了一段时间,一般来说它收到良好的评论,没有错误报告。
最近我有两个错误报告,人们使用带有iOS 8.1.2或8.1.3的iPad,并说现在我用来显示问题的UITextView是空白的。
我无法复制这个错误,但如果有人能够对它有所了解,我将不胜感激。
对象questions
和userAnswer
不是零,所以它肯定是一个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:@"\n"]; for (Question* q in questions) { if (i == [userAnswer count]) break; NSAttributedString *qtext =[[NSAttributedString alloc] initWithString:q.questionText]; NSAttributedString *ctext =[[NSAttributedString alloc] initWithString:@"Correct Answer:\n"]; NSAttributedString *atext =[[NSAttributedString alloc] initWithString:q.answerText]; NSAttributedString *ytext =[[NSAttributedString alloc] initWithString:@"Your Answer:\n"]; 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-Editable
和Non-Selectable
的Storyboard
在iOS 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 !!!
}