Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 调用-drawRect后如何在UIView中绘制文本_Objective C_Cgcontext - Fatal编程技术网

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

Objective c 调用-drawRect后如何在UIView中绘制文本,objective-c,cgcontext,Objective C,Cgcontext,我有一个iPad应用程序、XCode 4.5、iOS 6.1和故事板。我有一个UIView,其中嵌入了两(2)个UIView。第一个UIView(称为Calendar)位于上半部分,第二个UIView(称为Schedule)位于下半部分。我在上半部分创建了一个日历,并在下半部分绘制了一个网格以显示日程表 事件的顺序是向用户显示场景,场景中有日历(完全填写了当前月份)和日程安排(显示空网格)。在日历上点击某一天时,该天的日程安排将显示在日程安排视图中 问题是-drawRect在最初显示场景时被调用

我有一个iPad应用程序、XCode 4.5、iOS 6.1和故事板。我有一个UIView,其中嵌入了两(2)个UIView。第一个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];
}  

也许您可以使用UIView的方法:setNeedsDisplay。这将导致再次调用drawRect:。或者,如果您只想重新绘制视图的一部分,可以使用setNeedsDisplayInRect:,如您所述。 你不应该叫drawRect:你自己

UIGraphicsBeginImageContext(self.bounds.size)
[@"John Doe" drawAtPoint:CGPointMake(114,40) withFont:hourFont];
UIimage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

然后可以使用imageView将其作为子视图添加到视图中

通常,您应该在
drawRect:
方法中完成所有绘图。当用户点击日历中的日期时,您只需将
setNeedsUpdate
消息发送到日程视图。然后,明细表视图将自动重画并调用自己的
drawRect:
方法


调用
setNeedsDisplayInRect:
(或
setNeedsDisplay
)只需通知系统需要重新绘制接收器。您的
drawRect:
方法将在稍后的某个时候被调用,当它适合执行实际的绘图时。

这个问题没有意义;无法清除UIView上的图形,这使得此设计不可行

我现在正在考虑在明细表网格顶部使用UITableView或UIView子视图,这会带来新的问题

谢谢大家的评论

在日历上点击某一天时,该天的日程安排将显示在日程安排视图中

当您的用户点击日历上的“a day”按钮时,请告知要更改的日程安排:

  [self.calendarView displayDate: thatDay];
时间表,反过来,得到信息,计算出它需要做什么,并告诉自己重新显示什么时候该画画

 - (void) displayDate: (YourDateObject*) thatDay
 {
      self.date=thatDay;
      [self setNeedsDisplay: YES]
  }
现在,系统将在适当的时间调用计划的
drawRect:
,您将显示新的计划


一般规则很简单:当您知道视图时设置视图的状态,并让视图在被询问时重新显示自己。不要试图说“画这个”;相反,你可以说:“这是你下次应该画的东西。一旦你准备好了,就举手,老师会叫你的。”

在这两个选项中,我认为setNeedsDisplayInRect最有希望。。。我会告诉你结果的。不起作用。。。我已经用我的代码更新了问题。。。and-drawRect只调用一次;显然,我的代码不会触发它再次被调用。不,它在一个单独的方法中,当用户点击日历中的某个日期时会被调用。“drawAtPoint:withFont:”方法在当前图形上下文中绘制字符串。我假设您正在使用UIGraphicsBeginImageContextWithOptions()函数获取图形上下文?我现在是(UIGraphicsBeginImageContext(self.bounds.size);),但仍然没有显示…:-{Code已更新以显示完整的方法。没有setNeedsUpdate…(在schedule.m中,我做了一个[self-setNeedsUpdate])CGContextClearRect(context,self.bounds)有什么问题;?如果您还有其他问题,请单击按钮提问。我不理解您下面关于不打算清除其中一个视图的评论。您的drawRect:方法可以绘制它想要的任何内容。