我有一个iPad
应用,使用Storyboards
,XCode 4.6
和iOS 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
从哪里放到它?
答案 0 :(得分:3)
您的视图控制器需要为其控制的视图调用setNeedsDisplay
,而不是为自己调用。所以,你想要
[self.subViewGrid setNeedsDisplay]
答案 1 :(得分:1)
这只是您阅读文档时的错误。理解文档对于Objective-C编程至关重要,因此我将尽力帮助您掌握它。
如果查看setNeedsDisplay
的文档,您会发现它是CALayer
或UIView
类方法。如果您继续查看继承,则会看到UIView
为UIResponder:NSObject
且CALayer
为NSObject
。这些都不会从UIViewController
继承,这就是您收到错误的原因。您需要致电[self.subViewGrid setNeedsDisplay]