0

我有一个应用程序,iPad使用和。我有一个包含. 在里面,我有一个,全部使用 IB 创建的。以编程方式,我创建了两 (2) 个(一个称为,另一个称为)并将它们添加到; 它们都在模拟器中正确显示。这是代码:StoryboardsXCode 4.6iOS 6.1UIViewControllerUIViewControllerUIScrollControllerviewDidLoadUIViewssubViewGridsubViewDataUIViewController

- (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永远不会被调用。`

I have read the UIView Programmer's Guide and looked in SO and did a Google search, but found nothing that addresses the issue, which is: why won't [self.subViewGrid setNeedsDisplay] call drawRect from where I have it placed?

4

2 回答 2

3

Your view controller needs to call setNeedsDisplay for the view it controls, not for itself. So, you want

 [self.subViewGrid setNeedsDisplay]
于 2013-02-24T17:23:56.657 回答
1

This is just an error in your reading the documentation. Understanding the documentation is critical for objective-C programming so I'll try to help you get a grasp of it.

If you look at the documentation for setNeedsDisplay you will see that it is either a CALayer or UIView class method. If you then look at inheritance, you will see that UIView is UIResponder:NSObject and CALayer is NSObject. None of these inherit from UIViewController which is why you are getting the error. You need to call [self.subViewGrid setNeedsDisplay]

于 2013-02-24T17:29:24.080 回答