Invalid context 0x0 on UITextField (Xcode 5)

2019-05-03 09:53发布

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.
}

2条回答
看我几分像从前
2楼-- · 2019-05-03 10:34

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.

查看更多
小情绪 Triste *
3楼-- · 2019-05-03 10:54

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
           }
查看更多
登录 后发表回答