2012-02-09 74 views
0

我在陣列越來越CGPoint值這樣在iPhone的xcode從另一個類訪問數組

@interface CanvasView : UIView{ 
NSMutableArray *_array; 
} 
@property (nonatomic, retain) NSMutableArray *_array; 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGPoint currentPoint = [touch locationInView:self]; 
    [_array addObject:[NSValue valueWithCGPoint: currentPoint]]; 
} 

我把這種現象稱之爲數組中touchesEnded其做工精細,我會得到nspoint值

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
     NSLog(@"roomAtX:%@ ",_array); 
    PieClass *myPieClass=[[PieClass alloc]initWithFrame:CGRectMake(8, 297, 200, 300)]; 
    [self addSubview:myPieClass]; 
} 

這是我的控制檯進行放

"NSPoint: {595, 158}", 
"NSPoint: {558, 154}", 
"NSPoint: {535, 152}", 
"NSPoint: {518, 152}", 
"NSPoint: {500, 160}", 
"NSPoint: {482, 175}", 

現在我想,這樣我CAL使用該陣列到另一個類這導致我的另一個類 這樣

- (void)drawRect:(CGRect)rect 
{ 
     CanvasView *cnava =[[CanvasView alloc]init]; 
     NSLog(@"nextview:%@ ",cnava._array); 

} 

但在這裏我得到nextview:(null) 親切指導如何可以訪問陣列中的其他類。

+0

我不明白問題是什麼。你初始化一個新的CanvasView,它的屬性數組是空的,因爲它沒有被數據初始化,直到touchesEnded:withEvent:被執行。所以在發生數組之前,數組是空的(null)。那麼問題是什麼? – Gabriel 2012-02-09 07:35:12

+0

你不初始化數組,你可以告訴我們你初始化_array的地方(你給它的數組是什麼值)? – Radu 2012-02-09 07:39:59

+0

@Radu我初始化觸摸移動數組的方法像這樣[_array addObject:[NSValue valueWithCGPoint:currentPoint]]; – 2012-02-09 07:50:05

回答

1

只是有PieClass

@interface PieClass : UIView{ 
    // ... other things. 
} 
@property (nonatomic, retain)NSMutableArray *pointsArray; 

一個屬性,不要忘了合成它。現在,當你初始化PieClass通該數組這也

PieClass *myPieClass=[[PieClass alloc]initWithFrame:CGRectMake(8, 297, 200, 300)]; 
myPieClass.pointsArray = _array; 
[self addSubview:myPieClass]; 

現在在你的drawRect

- (void)drawRect:(CGRect)rect 
{ 
     //CanvasView *cnava =[[CanvasView alloc]init]; you don't need to do this 
     NSLog(@"nextview:%@ ",self.pointsArray); //passed from previous class 

} 
0

您必須使用舊對象CanvasView。所以,如果你分配並初始化一個新的對象CanvasView _array將爲空。您必須爲_array分配內存並對其進行初始化,否則必須先將其設置爲屬性。

因此,對於要繪製的當前對象,您需要傳遞CanvasView對象的引用以檢測觸摸並保存點。

我希望我已經夠清楚了。

0

保存對象(在這一點上,數組)NSUserDefaults的第一類

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
     CGPoint currentPoint = [touch locationInView:self]; 
     [_array addObject:[NSValue valueWithCGPoint: currentPoint]]; 

     NSUserDefaults *myArray=[NSUserDefaults standartuserdefaults]; 
     [myArray setObject:_array forKey:@"keyToArray"]; 

    } 



    - (void)drawRect:(CGRect)rect 
    { 
      NSArray *yourArray=[[NSUserDefaults standartuserdefaults] objectForKey:@"keyToArray"]; 
      //CanvasView *cnava =[[CanvasView alloc]init]; 
      NSLog(@"nextview:%@ ",yourArray); 

    } 

試試這個

0

如果我理解正確,你需要clas s到是辛格爾頓

你先在.h文件中創建一個類的方法:

@interface CanvasView : UIView{ 
NSMutableArray *_array; 
} 
... 
+ (CanvasView *)sharedInstance; 
... 

然後在.m文件:

@implementation CanvasView 
... 
static CanvasView *sharedHelper = nil; 
... 
+ (CanvasView *)sharedInstance 
{ 
    @synchronized(self) 
    { 
     if (!sharedHelper) 
     { 
      sharedHelper = [CanvasView new; 
     } 
    } 
    return sharedHelper; 
} 

,並調用它像:

CanvasView *view = [CanvasView sharedInstance];