我制作了一款允许用户提问的应用。在我的addQuestion类中,我有一个用户写出问题的UITextView。
我的问题是偶尔在我的iPhone上键盘将不可见(这不会每次都发生 - 只有我第一次打开应用程序 - 有时它甚至可以立即工作)。奇怪的是,如果键盘可见,我仍然可以按下键盘的键并根据键的位置键入响应。问题只在于可见性(我尝试移动UITextView以便它不会阻止键盘)。大约20或25秒后键盘出现。
我还使用了苹果公司推荐的故障排除方法:
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
NSLog(@"isKeyWindow = %d window level = %.1f frame = %@ class = %@\n",
window.isKeyWindow, window.windowLevel,
NSStringFromCGRect(window.frame), window.class.description);
}
但是没有覆盖UITextEffectsWindow的自定义窗口
打印出来:
isKeyWindow = 1 window level = 0.0 frame = {{0, 0}, {375, 667}} class = UIWindow**
isKeyWindow = 0 window level = 10.0 frame = {{0, 0}, {375, 667}} class = UITextEffectsWindow**
当它最终工作并且键盘可见时:
isKeyWindow = 1 window level = 0.0 frame = {{0, 0}, {375, 667}} class = UIWindow
isKeyWindow = 0 window level = 1.0 frame = {{0, 0}, {375, 667}} class = UITextEffectsWindow
也许它与窗口级别为10.0有关?当键盘最终显示窗口级别是1.0 ...
这是.m类以获取更多信息
@interface addQuestion () <UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *questionText;
@property (strong, nonatomic) NSDictionary *dictionary;
@end
@implementation addQuestion
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.questionText becomeFirstResponder];
self.questionText.delegate = self;
self.questionText.text = @"Ask a good yes or no question!";
self.questionText.textColor = [UIColor lightGrayColor];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Post" style:UIBarButtonItemStylePlain target:self action:@selector(posted)];
self.navigationItem.rightBarButtonItem = anotherButton;
// for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
// NSLog(@"isKeyWindow = %d window level = %.1f frame = %@ class = %@\n",
// window.isKeyWindow, window.windowLevel,
// NSStringFromCGRect(window.frame), window.class.description);
// }
}
- (void)textViewDidChangeSelection:(UITextView *)textView{
if ([textView.text isEqualToString:@"Ask a good yes or no question!"] && [textView.textColor isEqual:[UIColor lightGrayColor]])[textView setSelectedRange:NSMakeRange(0, 0)];
}
- (void)textViewDidBeginEditing:(UITextView *)textView{
[textView setSelectedRange:NSMakeRange(0, 0)];
}
- (void)textViewDidChange:(UITextView *)textView
{
if (textView.text.length != 0 && [[textView.text substringFromIndex:1] isEqualToString:@"Ask a good yes or no question!"] && [textView.textColor isEqual:[UIColor lightGrayColor]]){
textView.text = [textView.text substringToIndex:1];
textView.textColor = [UIColor blackColor]; //optional
self.navigationItem.leftBarButtonItem.enabled=YES;
}
else if(textView.text.length == 0){
self.navigationItem.leftBarButtonItem.enabled=NO;
textView.text = @"Ask a good yes or no question!";
textView.textColor = [UIColor lightGrayColor];
[textView setSelectedRange:NSMakeRange(0, 0)];
}
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@""]) {
textView.text = @"Ask a good yes or no question!";
textView.textColor = [UIColor lightGrayColor]; //optional
}
//[textView resignFirstResponder];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if (text.length > 1 && [textView.text isEqualToString:@"Ask a good yes or no question!"]) {
textView.text = @"";
textView.textColor = [UIColor blackColor];
}
if([[textView text] length] > 140){
return NO;
} else {
return YES;
}
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
@end
答案 0 :(得分:0)
[self.questionText becomeFirstResponder];
remove this
答案 1 :(得分:0)
我的问题是我试图在后台线程中进行segue(在从NSURLSession回调之后)。
我应该做的是确保我在主线程中调用了segue。
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"proceed" sender:self];
});