0

重要:

私は問題を理解しましたが、モデレーターが何らかの理由でそれを削除しました:

ビューコントローラに関数を含めるのではなく、ボタンから直接関数を呼び出してみました。

なぜうまくいかなかったのかまだよくわかりませんので、その質問への回答を受け付けます。

メインポスト

UIButtonを押した後、描画を行おうとしています。私の描画はUIViewのサブクラスで行われます。ViewControllerにこのサブクラスのインスタンスがあります。また、サブクラスの関数を呼び出すUIButtonがViewControllerにあります。その関数で私は呼び出します

[self setNeedsDisplay];

アプリをロードすると、ビューがロードされるときにdrawRectが1回呼び出されますが、ボタンが押されるたびにsetNeedsDisplayが呼び出されますが、再度呼び出されることはありません。私は多くの同様の質問を調べましたが、解決策は見つかりませんでした。

ここにすべてのコードを投稿します:

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
4

2 に答える 2

1

解決策は、ボタンによって呼び出されたView Controllerの関数からではなく、ボタンから関数を呼び出すことでした。

于 2012-08-13T17:12:19.593 に答える
0

カスタム UIView を指すアウトレットが接続されていることは確かですか? かどうかを確認しnilます。また、関数が呼び出されていることを確認しましたか?

于 2012-07-15T23:02:36.707 に答える