【发布时间】:2013-02-24 17:21:37
【问题描述】:
我有一个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 的文件内容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
在我的drawRect 方法中,我有一些代码应该在subViewGrid 上绘制一个“网格”。问题是 drawRect 永远不会被调用。`
我已阅读 UIView 程序员指南并查看了 SO 并进行了 Google 搜索,但没有找到任何解决问题的方法,即:为什么 [self.subViewGrid setNeedsDisplay] 不能从我放置它的位置调用 drawRect ?
【问题讨论】:
-
OK... 我添加了两个属性,一个用于 subViewGrid,另一个用于 subViewData,删除了@interface 代码中的声明。在创建两个 UIView 的所有代码之后,我在 -viewDidLoad 中添加了 [self.subViewGrid setNeedsDisplay]。方法 -drawRect 没有被调用。我应该把它放在哪里?
-
我更新了上面的代码以显示我放置 [self.subViewGrid setNeedsDisplay];
标签: objective-c uiview cgcontext