: 여기 잘못된 컨텍스트 오류
Jul 20 13:24:40 ps2xipas3qfe Chess[277] <Error>: CGContextSetRGBFillColor: invalid context 0x0
Jul 20 13:24:40 ps2xipas3qfe Chess[277] <Error>: CGContextFillRects: invalid context 0x0
오류를 생성하는 코드입니다 : 나는 "유효하지 않은 상황"오류에 대한 웹 검색하면
- (void)drawRect: (CGRect)rect {
//[super drawRect:rect];
for(int i=0; i<8; i++) {
for(int j=0; j<8; j++) {
CGRect TheRect = CGRectMake(i*30+30,j*30+30,30,30);
CGContextRef context = UIGraphicsGetCurrentContext();
if(i%2 == j%2) {
CGContextSetRGBFillColor(context,1.0,0.0,0.0,0.0);
}
else {
CGContextSetRGBFillColor(context,0.0,0.0,0.0,0.0);
}
CGContextFillRect(context,TheRect);
}
}
}
, 내가 가진 응답 그래픽 컨텍스트가 단지 "의 drawRect"멤버 함수 내에서 검색 할 수 있었지만, 이것은 "의 drawRect 내 "기능을 사용하고 있으며 여전히 오류가 발생합니다. 여기 클래스는 UIView
에서 상속받은 ChessBoard
입니다.
도움을 주셔서 감사합니다. 그러나 프로그램을 실행할 수 없어 매우 혼란 스럽습니다. 나는 더 이상 사용하지 못했던 오류를 얻지 못했지만 지금은 빈 화면 만 얻습니다. 나는 setNeedsDisplay
과 setNeedsDisplayInRect
을 시도했지만 어느 것도 작동하지 않는 것 같습니다.
- (void)viewDidLoad
{
[super viewDidLoad];
ChessBoard* TheBoard = [ChessBoard new];
[self.view addSubview: TheBoard];
// [TheBoard setNeedsDisplayInRect: CGRectMake(0,0,400,400)];
[TheBoard setNeedsDisplay];
}
은 내가 ChessBoard.m
에있는 함수 :
- (void)drawRect: (CGRect)rect {
[super drawRect:rect];
UILabel* HelloWorld = [UILabel new];
HelloWorld.text = @"Hello, World!";
[HelloWorld sizeToFit];
HelloWorld.frame = CGRectMake(1,1,100,20);
[self addSubview:HelloWorld];
for(int i=0; i<8; i++) {
for(int j=0; j<8; j++) {
CGRect TheRect = CGRectMake(i*30+30,j*30+30,30,30);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextFillRect(context,TheRect);
if(i%2 == j%2) {
CGContextSetRGBFillColor(context,1.0,0.0,0.0,1.0);
}
else {
CGContextSetRGBFillColor(context,0.0,0.0,0.0,1.0);
}
}
}
}
예, 명시 적으로 'drawRect'를 호출하고 있습니다. 나는 내가 그렇게하기로되어 있지 않다는 것을 깨닫지 못했습니다. – user1672637
'drawRect :'를 절대 호출하지 마라. 그것은 당신에게'UIView drawRect :'에 대한 문서에서 이것을 알려줍니다. 뷰를 강제로 다시 그리려면 뷰에서'setNeedsDisplay'를 호출하십시오. Josh가 참조한 복제물을보십시오. – rmaddy