2013-07-20 10 views
0
엑스 코드 4.6.3에 아이폰 OS 프로그램을 실행하는 동안, 나는 다음과 같은 오류의 긴 목록을 점점 계속

: 여기 잘못된 컨텍스트 오류

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입니다.


도움을 주셔서 감사합니다. 그러나 프로그램을 실행할 수 없어 매우 혼란 스럽습니다. 나는 더 이상 사용하지 못했던 오류를 얻지 못했지만 지금은 빈 화면 만 얻습니다. 나는 setNeedsDisplaysetNeedsDisplayInRect을 시도했지만 어느 것도 작동하지 않는 것 같습니다.

다음
- (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); 
      } 
     } 
    } 
} 
+0

예, 명시 적으로 'drawRect'를 호출하고 있습니다. 나는 내가 그렇게하기로되어 있지 않다는 것을 깨닫지 못했습니다. – user1672637

+1

'drawRect :'를 절대 호출하지 마라. 그것은 당신에게'UIView drawRect :'에 대한 문서에서 이것을 알려줍니다. 뷰를 강제로 다시 그리려면 뷰에서'setNeedsDisplay'를 호출하십시오. Josh가 참조한 복제물을보십시오. – rmaddy

답변

0

귀하의 코드는 매우 가까이 여기

내가 ChessViewController.m에있는 기능입니다! ChessBoard 인스턴스를 인스턴스화하는 데 사용하는 코드가 올바르지 않습니다. "new"(어쨌든 Objective-C에서 항상 피해야 만 함)를 사용하는 대신 UIView 문서에서 initWithFrame : initializer 메서드를 사용해야한다고 지적합니다.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    CGRect boardFrame = CGRectMake(0, 0, 240, 240); 
    ChessBoard *theBoard = [[ChessBoard alloc] initWithFrame:boardFrame]; 
    [self.view addSubview:theBoard]; 
} 

그리기 코드가 작동하는지 확인해야합니다.

+0

도움에 감사드립니다. 꽤 효과가있었습니다. – user1672637

관련 문제