【问题标题】:In UIViewController's code, [self.subViewGrid setNeedsDisplay] not calling -drawRect在 UIViewController 的代码中,[self.subViewGrid setNeedsDisplay] 没有调用 -drawRect
【发布时间】:2013-02-24 17:21:37
【问题描述】:

我有一个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 的文件内容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


【解决方案1】:

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

 [self.subViewGrid setNeedsDisplay]

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 谢谢你,我找到了答案。我需要在 IB 中创建的两 (2) 个 UIViews……一旦有了这些,我就创建了两个类,每个视图一个。去正确的班级进行绘图的简单问题。再次感谢...
    • 很高兴我能提供帮助。我可以对答案进行修改,以便为可能偶然发现它的其他人改进它吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多