Invalid context 0x0 on UITextField (Xcode 5)

廉价感情. 提交于 2019-12-07 05:55:46

问题


I am using a UITextField in my app. I am using [texfield becomeFirstResponder]. This works just fine and loads the keyboard when the view is loaded. However, this error comes up when I click on the UITextField again after it has been brought up. I don't understand exactly why, but here is the output I am getting:

CGContextSetFillColorWithColor: invalid context 0x0.
CGContextSetStrokeColorWithColor: invalid context 0x0. 
CGContextSaveGState: invalid context 0x0. 
CGContextSetFlatness: invalid context 0x0. 
CGContextAddPath: invalid context 0x0. 
CGContextDrawPath: invalid context 0x0. 
CGContextRestoreGState: invalid context 0x0. 
CGContextSaveGState: invalid context 0x0. 
CGContextSetFlatness: invalid context 0x0. 
CGContextAddPath: invalid context 0x0.
CGContextDrawPath: invalid context 0x0. 
CGContextRestoreGState: invalid context 0x0.

Here is my code:

// ViewController.h
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *textField;

@end


// ViewController.m
@synthesize textField;

- (void)viewDidLoad
{
    [textField becomeFirstResponder];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

回答1:


I believe you've found an iOS bug (or at least a simulator bug). You can reproduce it even more simply:

  • New Project (Single View)
  • Drag UITextField onto Storyboard
  • Run
  • Click on UITextField.
  • Click off of UITextField
  • Click on UITextField
  • (In some cases, you need to click off of UITextField again)
  • Observe errors

I recommend opening a radar.




回答2:


It seems like you are drawing some context on the screen . You need to write the code for drawing in the drawRect method . You mite have written the code elseWhere. Within the drawRect method make sure you check if the context exist as well .

CGContextRef context = UIGraphicsGetCurrentContext();
   if (context) {
        // Do your stuff
           }


来源:https://stackoverflow.com/questions/19469603/invalid-context-0x0-on-uitextfield-xcode-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!