我在 NSObject 中定义了一个 C 类型数组,我已经设置了 Setters & getters 我已经在 UIView drawrect{} 方法中填充了数组
- (void)drawRect:(CGRect)rect
{
// Drawing code
int n = 8; // row and column count
int padding = n*2; // distance beetween dots
//float radius=4;
float radius = MIN((rect.size.width-(n+1)*padding)/(n*6), (rect.size.height-(n+1)*padding)/(n*6)); // radius depending on rect size
Board *board=[[Board alloc]init];
for (int y = 0; y<n; y++) {
for (int x = 0; x<n-1; x++) {
CGRect linehor=CGRectMake(1.5*padding+2*radius+(padding+radius*6)*x, padding+.5*radius+(padding+radius*6)*y, 2*padding, 2*radius);
Lines *line=[[Lines alloc]initWithFrame:linehor];
line.IDlabel.text=[NSString stringWithFormat:@"%d,%d",x,y];
[board setthelinestateatcol:x androw:y withstate:YES];//The Setters Part
line.col=x;
line.row=y;
[self addSubview:line];
}
}
for (int y = 0; y<n-1; y++) {
for (int x = 0; x<n; x++) {
CGRect linehor=CGRectMake(1*padding+2*radius+(padding+radius*6)*x, 1.25*padding+.5*radius+(padding+radius*6)*y, 2*radius, 2*padding);
vertLines *line=[[vertLines alloc]initWithFrame:linehor];
line.IDlabel.text=[NSString stringWithFormat:@"%d,%d",x,y];
[board setthevertlinestateatcol:x androw:y withstate:YES];
line.col=x;
line.row=y;
[self addSubview:line];
}
}
NSLog(@"Testing the array%d",[board checklinestateatcol:2 androw:3]);
}
当涉及到 Getter 时,它不起作用,C 数组就像从未填充过一样,都是 0 以下是 h 文件中 viewcontroller 的代码:
@interface ViewController : UIViewController{
Board *board1;
}
在 m 文件中:
-(void)checkthetoucheffect:(NSNotification*)notification{
int x=[[[notification userInfo]objectForKey:@"x"]integerValue];
int y=[[[notification userInfo]objectForKey:@"y"]integerValue];
BOOL hhh= [board1 checklinestateatcol:x androw:y];
BOOL fff= [board1 checklinestateatcol:x-1 androw:y-1];
NSLog(@"this should right this time %d ,%d",hhh,fff);
}
NS Object 中 C 类型数组的定义如下
@implementation Board
{
BOOL horlinearray2[8][8];
BOOL vertlinearray2[8][8];
}
-(BOOL)checklinestateatcol:(NSInteger)xcol androw:(NSInteger)yrow {
return horlinearray2[xcol][yrow];
}
-(void)setthelinestateatcol:(NSInteger)xcol androw:(NSInteger)yrow withstate:(BOOL)state{
horlinearray2[xcol][yrow]=state;
}
我知道 NSObject 和 uiviewcontroller 之间的这个基本共享对象,但我不知道解决它的最佳方法是什么?我应该使用单例还是有其他东西