如何在调用-drawRect后在UIView中绘制文本

Spo*_*ude 2 objective-c cgcontext

我有一个iPad应用程序,XCode 4.5,iOS 6.1和Storyboard.我有一个UIView,其中嵌入了两(2)个UIViews.第一个UIView(称为Calendar)位于上半部分,第二个UIView(称为Schedule)位于下半部分.我在上半部分创建一个日历,并绘制一个网格以显示下半部分的时间表.

事件序列是向用户呈现带有日历的场景(完全填写当月)和计划(显示空网格).在日历上点击一天时,该日程表将显示在"日程表"视图中.

-drawRect在用户能够选择要为日程表显示的日期之前,在最初显示场景时调用问题.因为我必须从-drawRect中进行绘图,所以我无法弄清楚如何填写以外的时间表-drawRect.我想知道是否使用setNeedsDisplayInRect : ,会完成我需要做的事情吗?

我已经看了几天SO和谷歌了,发现没有什么能回答这个问题.这不是这个问题的重复,而是一个后续,因为项目已经重新定义.任何想法将不胜感激.

更新:这是我使用setNeedsDisplayInRect的代码:

- (void) drawSchedule  {
    const float rectWidth = 120.0;

    //  draw detail for selected day for each staff member
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextSetLineWidth(context, 1.0);
    CGContextSetRGBFillColor(context, 0, 0, 1, 0.3); 

    UIGraphicsBeginImageContext(self.bounds.size);

    //  draw customer name using bounds from CGRectMake
    const float nameFontSize = 12;
    UIFont *hourfont=[UIFont systemFontOfSize: nameFontSize];
    [[UIColor blackColor] set];
    [self setNeedsDisplayInRect: CGRectMake(160.0, 150.0, 120.0, 50.0)];  //  mark as needs to be redrawn

    //  customer for this time slot
    [@"John Doe" drawAtPoint:CGPointMake(114, 40) withFont:hourfont];
}  
Run Code Online (Sandbox Code Playgroud)

Mar*_*ein 5

在日历上点击一天时,该日程表将显示在"日程表"视图中.

当您用户点击日历上的一天时,请告知计划更改:

  [self.calendarView displayDate: thatDay];
Run Code Online (Sandbox Code Playgroud)

反过来,附表得到消息,弄清楚它需要做什么,并告诉自己在绘制时间时重新显示.

 - (void) displayDate: (YourDateObject*) thatDay
 {
      self.date=thatDay;
      [self setNeedsDisplay: YES]
  }
Run Code Online (Sandbox Code Playgroud)

现在,系统将drawRect:在适当的时间调用时间表,您将显示新的时间表.

一般规则就是这样:在您知道视图时设置视图的状态,并在被询问时让视图重新显示.不要试图说"画这个"; 相反,说,"这是你下次应该画的东西.一旦你准备好了,举起你的手;老师会打电话给你."