2011-12-30 97 views
0

我試圖每次按下按鈕時生成一個NSMutableArray。我使用NSLog來檢查按下每個按鈕後的數組數量,值爲零。代碼如下。訪問NSMutableArray困難

ProjectViewController.h

NSMutableArray *numberQuery 
... 
@property (nonatomic, retain) NSMutableArray *numberQuery; 

ProjectViewController.m

- (void)makeButtons{ 
UIButton * pickButton; 
int y_plot = 150; 
int x_plot = 70; 
int z = 0; 

for(int y = 1; y < 10; y++) 
{ 

    for(int x = 1; x < 5; x++){ 
     z++; 

     pickButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     pickButton.frame = CGRectMake(x*x_plot, y_plot, 60, 40); 

     [pickButton setBackgroundImage:[UIImage imageNamed:@"btnUnselected.png"]  forState:UIControlStateNormal]; 
     [pickButton addTarget:self action:@selector(digitClick:) forControlEvents:UIControlEventTouchUpInside]; 
     [pickButton setTitle:[NSString stringWithFormat:@"%d",z] forState:UIControlStateNormal]; 
     pickButton.titleLabel.textColor = [UIColor blackColor]; 
     pickButton.tag = z; 
     [self.view addSubview:aButton]; 
    } 
    y_plot=y_plot+45; 
    } 
} 

//************************ 
- (void)digitClick:(id)sender{ 
UIButton * chosenButton =(UIButton *)sender; 

if ([sender isSelected] ==FALSE) { 
    [sender setSelected:TRUE]; 
    [chosenButton setBackgroundImage:[UIImage imageNamed:@"btnSelected.png"] forState:UIControlStateNormal]; 
    chosenButton.titleLabel.textColor = [UIColor whiteColor]; 

    if([queryNumbersX count]<6){ 
     [self numberSearchArray:chosenButton.tag]; 
     } 
    } 
else 
    {[sender setSelected:FALSE]; 
    [chosenButton setBackgroundImage:[UIImage imageNamed:@"btnUnselected.png"]forState:UIControlStateNormal]; 
    chosenButton.titleLabel.textColor = [UIColor blackColor]; 
} 

forState:UIControlStateNormal]; 
NSLog(@"clicked button with title %d",chosenButton.tag); 
} 

//************************ 
-(void) numberSearchArray:(NSInteger)newNumber; 
{ 

    [self.numberQuery addObject:[NSNumber numberWithInt: newNumber]]; 
    NSLog(@"numberSearchArray %d - count %d",newNumber, [self.numberQuery count]); 
} 

我使用的NSMutableArray的正確的方式...聲明?

這是viewDidLoad方法

NSMutableArray *numberQuery = [[NSMutableArray alloc] initWithObjects:nil]; 

代碼雖然我有陣宣佈詮釋他的頭文件,它好像我不能在其被分配的方法之外訪問它。

回答

1
@property (nonatomic, retain) NSMutableArray *numberQuery; 

必須在.M

平衡與

@synthesize numberQuery; 

和正確的建立將是

self.numberQuery = [NSMuatableArray arrayWithCapacity:someNumber]; 

通過這樣做

NSMutableArray *numberQuery = [[NSMutableArray alloc] initWithObjects:nil]; 

您沒有使用@property創建一個新變量,這就是爲什麼你會得到警告說你的變量沒有使用,因爲它的作用域實際上只是viewDidLoad方法而不是對象。

+0

你也可以使用一個方便的方法'self.numberQuery = [NSMutableArray array];'。這個數組是autoreleased,所以你的保留屬性是平衡的。同樣使用'initWithObjects:'並傳遞nil看起來很可疑。 – vikingosegundo 2011-12-30 16:14:13

+0

@vikingosegundo你是對的我會修改它爲'+(id)arrayWithCapacity:(NSUInteger)numItems'那會更好 – 2011-12-30 16:18:47

+0

THanks,VinceBurn!現在工作完美! – 2011-12-30 16:20:19

1

看起來你從來沒有放過numberQuery。所以它總是零,因此addObject方法被忽略。您需要在init(或您喜歡的任何適合的地方)中進行分配,並在dealloc(或其他合適的地方)發佈。

+0

我實際上在viewDIdLoad方法中分配它。我收到一條警告消息,聲明變量從未被使用過。 – 2011-12-30 06:59:25

+0

請發表那些相關代碼。 – taskinoor 2011-12-30 07:06:01

+0

我已經將原始問題的分配語句添加了。 – 2011-12-30 15:55:46