开发者

setNeedsDisplay revisited

开发者 https://www.devze.com 2023-02-28 20:33 出处:网络
I have the often discussed drawRect not getting called.I\'ve been through many posts, but have not resolved my issue.

I have the often discussed drawRect not getting called. I've been through many posts, but have not resolved my issue.

The solution is often described as calling -setNeedsDisplay. The answer to question setNeedsDisplay doesn't call drawRect indicates calling it on the subview not self.

I have created a subview to draw on a button. The button is selected.


- (IBAction) selected1:(id)sender {
    NSLog(@"selected1");    
    UIButton *button = (UIButton *)sender; 
    [button setTitle:@"Chosen1" forState:UIControlStateNormal];   
    [button setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];

 // UsableMark *um = [[UsableMark alloc] init ]; // Original
 // UsableMark *um = [[UsableMark alloc] initWithFrame:button.frame]; // 1st fix
    CGRect umFrame = CGRectMake(0,0,button.frame.size.width,button.frame.size.height); // 2nd fix
    UsableMark *um = [[UsableMark alloc] initWithFrame:umFrame ]; // 2nd fix

    [button addSubview:um];
    [um setNeedsDisplay];
    [um release];    
    button.enabled = NO;    
    NSLog(@"finished selected1");
}

A drawRect call in UsableMark is not triggered.

Replacing the [um setNeedsDisplay] with [button setNeedsDisplay] does not trigger a drawRect call.

An attempt at 开发者_如何学Cusing self fails because the selected1 method is a class of type UIApplicaitonDelegate. The Method -setNeedsDisplay is not found.

Trying [self.window setNeedsDisplay] gives no progress.

Directly calling the drawRect (which one doesn't want to do) CGRect rect = button.frame; [um drawRect:rect]; demonstrates the method present, logging is alive.

The resolution may be simple, but it escapes me.


The problem is that init is called instead of initWithFrame:. Since the default init was used, the view's frame had zero size, and UIKit skipped displaying it as an optimization.

0

精彩评论

暂无评论...
验证码 换一张
取 消