在UIViewController的代码中,[self.subViewGrid setNeedsDisplay]没有调用-drawRect

时间:2013-02-24 17:21:38

标签: objective-c uiview cgcontext

我有一个iPad应用,使用StoryboardsXCode 4.6iOS 6.1。我有一个包含UIViewController的场景。在UIViewController里面,我有一个UIScrollController,都是使用IB创建的。以编程方式,在viewDidLoad我创建了两个(2)UIViews(一个名为subViewGrid,另一个名为subViewData)并将其添加到UIViewController;它们都在模拟器中正确显示。这是代码:

- (void)viewDidLoad  {

[super viewDidLoad];

// notify me when calendar has been tapped and CFGregorianDate has been updated
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(calendarTapNotification:)
                                             name:@"calendarDateSelected" object:nil ];

//   UIScrollVIew settings
CGSize scrollableSize = CGSizeMake(760, 1379);  //  set size of scheduleView
[self.schedScrollView setContentSize:scrollableSize];
self.schedScrollView.contentInset = UIEdgeInsetsMake(0,0,44,44);  //  allow for scroll bar
self.schedScrollView.directionalLockEnabled = YES;  //  prevents diagonal scrolling

//  create a sub-view to hold the appointment GRID
CGRect frame = CGRectMake(0,0,760,1390);  //  110,48,760,1390
subViewGrid = [[UIView alloc] initWithFrame:frame];
subViewGrid.tag = 12;  //  use tag to get correct sub-view
subViewGrid.backgroundColor = [UIColor grayColor];
subViewGrid.alpha = 1.0;  //  make it opaque
[self.schedScrollView addSubview:subViewGrid];

//  create a sub-view to hold the appointment DATA
frame = CGRectMake(110,48,670,750); 
subViewData = [[UIView alloc] initWithFrame:frame];
subViewData.tag = 22;  //  use tag to get correct sub-view
subViewData.backgroundColor = [UIColor redColor];
subViewData.alpha = 0.2;  //  make it sort of transparent
[self.schedScrollView addSubview:subViewData];

[self.subViewGrid setNeedsDisplay];  //  ****  UPDATED ****

}

以下是.h的<{1}}文件内容:

UIViewController

在我的 @interface CalendarViewController : UIViewController { UIView *subViewGrid; UIView *subViewData; } @property (strong, nonatomic) IBOutlet UIScrollView *schedScrollView; - (void) calendarTapNotification:(NSNotification *) notification; -(NSDate *)beginningOfDay:(NSDate *)date; -(NSDate *)endOfDay:(NSDate *)date; @end 方法中,我有一些代码应该在subViewGrid上绘制一个“网格”。问题是drawRect永远不会被调用。

我已阅读drawRect程序员指南,并查看了SO并进行了Google搜索,但没有找到解决该问题的任何内容,即:UIView为什么不会[self.subViewGrid setNeedsDisplay]致电drawRect从哪里放到它?

2 个答案:

答案 0 :(得分:3)

您的视图控制器需要为其控制的视图调用setNeedsDisplay,而不是为自己调用。所以,你想要

 [self.subViewGrid setNeedsDisplay]

答案 1 :(得分:1)

这只是您阅读文档时的错误。理解文档对于Objective-C编程至关重要,因此我将尽力帮助您掌握它。

如果查看setNeedsDisplay的文档,您会发现它是CALayerUIView类方法。如果您继续查看继承,则会看到UIViewUIResponder:NSObjectCALayerNSObject。这些都不会从UIViewController继承,这就是您收到错误的原因。您需要致电[self.subViewGrid setNeedsDisplay]