1

私はObjective-CとiOSが初めてです。おそらく簡単に修正できる問題が発生していますが、なぜそれが起こっているのかわかりません。

私がやっていることの簡単な説明をしようと思います.エラーを見つけるのに十分な情報があることを願っています.

PolygonView.h は UIView 型のクラスで、次のように NSmutablearray を宣言します。

@property(nonatomic, retain) NSMutableArray *polys;

PolygonView.m の initWithFrame:(CGRect)Frame メソッドで次のように初期化します。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    polys = [[NSMutableArray alloc]init];
    [self loadCsv];
    return self;
}

loadCSV 内で csv をロードし、それを使用していくつかのポリゴン オブジェクトを作成し、その他の操作を行いますが、その要点は次のとおりです。

poly* p;
p = [[poly alloc] init];
// here I set a few properties on p from the csv file and then add p to polys like this:
[polys addObject:p];

メソッドの最後で、次のように nslog を使用してカウントを出力します。

NSLog(@"polys size = %i", [polys count]);

これにより、コンソールに「polys size = 100」が出力されます。

そして、自分で drawRect を実行することにしました。最初は同じ nslog を使用してサイズ/カウントを出力しますが、「polys size = 0」がコンソールに出力されます。

私は何を間違っていますか?この配列に追加したオブジェクトに drawRect メソッドからアクセスするにはどうすればよいですか?

編集: ここに完全な polygonView.h ファイルがあります:

#import <UIKit/UIKit.h>

@interface PolygonView : UIView{
        NSMutableArray *polys;
}

@property(nonatomic, retain) NSMutableArray *polys;

-(void)loadCSV;

@end

編集 2: 完全なポリゴン

//
//  PolygonView.m
//  Polygon2
//

#import "PolygonView.h"
#import "poly.h"


@implementation PolygonView
@synthesize polys;

-(id)init
{
    NSLog(@"regular init");

    self = [super init];
   if (self) {
        self.polys = [[NSMutableArray alloc]init];
        [self loadCsv];

    }
    return self;
}
- (id)initWithFrame:(CGRect)frame
{
    NSLog(@"init with frame");
    self = [super initWithFrame:frame];
    if (self) {
        self.polys = [[NSMutableArray alloc]init];
        [self loadCsv];

    }
    return self;
}

-(void)loadCsv
{
    poly* p;
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"DivisoesEstaduais" ofType:@"csv"];
    NSString *testString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    NSString *newString = [testString stringByReplacingOccurrencesOfString:@"," withString:@"."];
    if (newString) {
        NSMutableArray *arrr = [newString componentsSeparatedByString:@"\n"];
        for (int i = 0; i < [arrr count]; i++){
            if ([[arrr objectAtIndex:i] length] > 0){
                p = [[poly alloc] init];
                NSMutableArray *arrayatual = [[arrr objectAtIndex:i] componentsSeparatedByString:@";"];
                [p setPolyid:[arrayatual objectAtIndex:0]];
                [p setPartid:[arrayatual objectAtIndex:1]];
                [p setPointid:[arrayatual objectAtIndex:2]];
                [p setx:[arrayatual objectAtIndex:3]];
                [p sety:[arrayatual objectAtIndex:4]];
                [self.polys addObject:p];
                NSLog(@"polys size = %i", [self.polys count]); // this increases from 1 to 100
            }  
        }
        NSLog(@"polys size = %i", [self.polys count]); // this prints 100
    }else{
        NSLog(@"not");
    }
}

- (void)drawRect:(CGRect)rect{
    srandom( time( NULL ) );
    CGContextRef context= UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, random() % 250, random() % 250, random() % 250, 1.0);
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CGContextFillPath(context);
    int oldtemp = 0;
    NSLog(@"drawRect polys pointer = %@", self.polys); // this is null
    NSLog(@"polys size = %i", [self.polys count]); // this is zero
    for (int i = 0; i < [polys count]; i++){
        poly *p = [polys objectAtIndex:i];
        int temp = p.getPolyid.intValue;
        if (temp == oldtemp){
            CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
            CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
            CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
            UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
            CGContextSetFillColorWithColor(context, color.CGColor);
            CGContextFillPath(context);
            NSLog(@"movetopoint");
            CGContextMoveToPoint(context, [p.getx floatValue]+160, [p.gety floatValue]+160);
            oldtemp++;
        }else{
            CGContextAddLineToPoint(context, [p.getx floatValue]+160, [p.gety floatValue]+160);
            NSLog(@"addlinetopoint");
        }
    }

    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
   UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];

    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillPath(context);
 }

@end

ここにviewController.mがあります:

//
//  ViewController.m
//  Polygon2
//
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     self.view.multipleTouchEnabled = YES;
    PolygonView * pv = [[PolygonView alloc] init];
    [self.view addSubview:pv];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

編集3:

George Sachin の解決策はうまくいきました。viewDidLoad の代わりに viewDidAppear を使用してください。しかし、viewDidLoad が原因で問題が発生したのはなぜですか? これは、誰かが見て理解しようとする場合に備えて、圧縮されたプロジェクトです。 http://www.mediafire.com/download/b9ueqkqbmxswd7a/Polygon2_3.zip

私には ViewDidload が私が使用すべきものだと思われます。絶対に使ってはいけませんか?

4

2 に答える 2

2

最終編集:

UIView のイニシャライザを作成するときは、.xib を使用するときに常にスーパーの initWithFrame: または initWithCoder: を呼び出す必要があります。

コードを次のように変更します。

-(id)init
{
NSLog(@"regular init");

self = [super initWithFrame:CGRectMake(0, 0, 20, 20)]; // Replace with the dimensions you want
if (self) {
    self.polys = [[NSMutableArray alloc]init];
    [self loadCsv];

}
NSLog(@"(init)polys size = %i", [self.polys count]); 

return self;
}
于 2013-06-17T19:13:41.637 に答える