2013-03-04 98 views
0

我是新来的目标c,并尝试学习创建一个用户界面的基础知识。在我的UIView类中,我创建了一个带有按钮的网格,但这些按钮实际上并不存在(据我所知)。理想情况下,当我点击一个按钮时,图像应该会改变,但这不会发生。我应该在哪里解决问题?用按钮填充网格

- (id)initWithFrame:(CGRect)frame { 
    if(self = [super init]){ 
     tiles_ = [NSMutableArray array]; 
     tileClosed = NO; 
    } 
    return self = [super initWithFrame:frame]; 
} 

- (void) initTile : (Tile *) sender 
{ 
    int MINE_COUNT = 16; 
    for(int i = 0; i < MINE_COUNT; i++){ 
     while(1){ 
      int rand = random() % [tiles_ count]; 
      Tile * tile = [tiles_ objectAtIndex:rand]; 
      if(tile != sender && !tile.isMine){ 
       tile.isMine = YES; 
       break; 
      } 
     } 
    } 
    tileClosed = YES; 
} 


- (void)drawRect:(CGRect)rect 
{ 
    NSLog(@"drawRect:"); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
     // shrink into upper left quadrant 
    CGRect bounds = [self bounds];   // get view's location and size 
    CGFloat w = CGRectGetWidth(bounds); // w = width of view (in points) 
    CGFloat h = CGRectGetHeight (bounds); // h = height of view (in points) 
    dw = w/16.0f;       // dw = width of cell (in points) 
    dh = h/16.0f;       // dh = height of cell (in points) 

    NSLog(@"view (width,height) = (%g,%g)", w, h); 
    NSLog(@"cell (width,height) = (%g,%g)", dw, dh); 

    // draw lines to form a 16x16 cell grid 
    CGContextBeginPath(context);    // begin collecting drawing operations 
     for (int i = 1; i < 16; ++i) 
    { 
     // draw horizontal grid line 
     CGContextMoveToPoint(context, 0, i*dh); 
     CGContextAddLineToPoint(context, w, i*dh); 

    } 
    for (int i = 1; i < 16; ++i) 
    { 
     // draw vertical grid line 
     CGContextMoveToPoint(context, i*dw, 0); 
     CGContextAddLineToPoint(context, i*dw, h); 
    } 
    for(int x=1; x<16;x++){ 
     for(int y=1;y<16;y++){ 

      Tile * tile = [[Tile alloc] init]; 
      [tile setFrame:CGRectMake(x * 16.0f, y * 16.0f, 16.0f, 16.0f)]; 
      [tile addTarget:self action:@selector(clickCell:) forControlEvents:UIControlEventTouchUpInside]; 
      [tiles_ addObject: tile];  } 

    } 
    [[UIColor grayColor] setStroke];    // use gray as stroke color 
    CGContextDrawPath(context, kCGPathStroke); // execute collected drawing ops 

} 

- (void) clickCell : (Tile *) sender 
{ 
    if(! tileClosed) [self initTile: sender]; 
    [sender open]; 
} 
+0

此外,是否有任何简单的指南/教程来学习UIView的基础知识? – krikara 2013-03-04 01:06:40

+1

Tile的定义是什么? – occulus 2013-03-04 01:44:01

+0

顺便说一句,不要命名任何方法以'init'开头,除非它实际上是一个init方法。你的initTile忽略了这一点,因此打破了Cocoa Touch命名约定。 – occulus 2013-03-04 01:44:37

回答

0

initwithFrame:方法被打破:回线应该只是return self;

你现在在做的事情实际上是破坏tiles_阵列,所以它最终为零,因此尝试在其中存储瓷砖什么都不做(因此它们不被保留)。

+0

我仍然有问题的按钮。在我的实现中似乎有多个错误。呃,我希望只有一些明确的通用方法来制作按钮。 – krikara 2013-03-04 02:32:25

+0

UIButton或类似的问题? – occulus 2013-03-04 08:54:37

+0

我仍然有同样的问题。我试图点击网格上的不同位置,但没有任何指示按钮实际存在 – krikara 2013-03-04 16:24:53

0

你的init方法不正确,你可以简单地将其更改为

- (id)initWithFrame:(CGRect)frame 
     { 
     self = [super initWithFrame:frame] 
     if(self) 
     { 
      tiles_ = [NSMutableArray array]; 
      tileClosed = NO; 
     } 
     return self; 
     } 

以及按钮将出现你不将它添加到

[self.view addSubview:tile]; 

缺少子视图。

+0

self.view的第二行给我一个编译器错误。在MyView对象上找不到属性“视图”。 – krikara 2013-03-04 16:23:23

+0

我也试过这个'MyView * view = [[MyView alloc] init];',但仍然有相同的错误 – krikara 2013-03-04 16:24:06

+0

你的类是UIView的子类,所以只需添加视图如下[self addSubview:title]一个[self.view addSubview:title]是如果你将它添加到控制器的视图。 – MaheshShanbhag 2013-03-05 06:31:23