Question

Important:

I had figured out the problem but a moderator deleted it for some reason:

I tried calling the function directly from the button instead of having the function in the view controller.

I still don't fully understand why it didn't work, so I will accept any answer to that question.

Main Post

I am trying to do some drawing after pressing a UIButton. My drawing is done in a subclass of UIView. I have an instance of this subclass in my ViewController. I also have a UIButton in my ViewController that calls a function of the subclass. In that function I call

[self setNeedsDisplay];

When I load up my app, drawRect is called once at the when the view loads, but is never called again, although setNeedsDisplay is being called every time the button is pressed. I looked at many similar questions and I have not found a solution.

I will post all of my code here:

AEViewController.h

#import <UIKit/UIKit.h>
#import "AEGraphView.h"

@interface AEViewController : UIViewController{

IBOutlet AEGraphView *theView;

}
-(IBAction)updateAEGraphView:(id)sender;
@end

AEViewController.m

#import "AEViewController.h"

@interface AEViewController ()

@end

@implementation AEViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    theView = [[AEGraphView alloc] init];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } 
    else {
        return YES;
    }
}
-(IBAction)updateAEGraphView:(id)sender{
    [theView UpdateView];
}

@end

AEGraphView.h

#import <UIKit/UIKit.h>

@interface AEGraphView : UIView{
    
    
    CGContextRef mainContext;
    int power;
    int multiplier;
    
    
}
-(void)prepareGraphics;
- (void) drawLineFrom:(CGPoint)p1 To:(CGPoint)p2;
-(void)UpdateView;
@end

AEGraphView.m

#import "AEGraphView.h"

@implementation AEGraphView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    [self prepareGraphics];
    // Drawing code
    NSLog(@"called");
    if (power == 0) {
        [self drawLineFrom:CGPointMake(100,100) To:CGPointMake(-50, -2)];
    }
    else {
        [self drawLineFrom:CGPointMake(0, 0) To:CGPointMake(150, 150)];
    }
    [super drawRect:rect];
    
}

-(void)prepareGraphics{
    mainContext = UIGraphicsGetCurrentContext();
    CGContextSetShouldAntialias(mainContext, TRUE);
    CGContextSetLineWidth(mainContext, 2);
    CGContextSetLineCap(mainContext, kCGLineCapRound);
    
}
- (void) drawLineFrom:(CGPoint)p1 To:(CGPoint)p2{
    NSLog(@"%f,%f  to %f,%f", p1.x,p1.y,p2.x,p2.y);
    // Begin a path.
    CGContextBeginPath(mainContext);
    
    // Add a line to the path.
    CGContextMoveToPoint(mainContext, p1.x, p1.y);
    CGContextAddLineToPoint(mainContext, p2.x, p2.y);
    
    // Stroke and reset the path.
    CGContextStrokePath(mainContext);
}
-(void)UpdateView{
    if (power == 0) {
        power = 1;
    }
    else {
        power = 0;
    }
    [self setNeedsDisplay];
}
@end
Was it helpful?

Solution

Solution was having the function called from the button instead of called from a function in the view controller that was called by the button.

OTHER TIPS

Are you sure your outlet that points to your custom UIView is hooked up? Check if it's nil. Also, did you verify your function is being called?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top