gpt4 book ai didi

objective-c - 访问 NSMutableArray 的困难

转载 作者:行者123 更新时间:2023-11-28 23:09:14 24 4
gpt4 key购买 nike

每次按下按钮时,我都会尝试构建一个 NSMutableArray。我正在使用 NSLog 在按下每个按钮后检查数组计数并且值为零。代码如下。

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];

虽然我在头文件中声明了数组,但似乎我无法在分配它的方法之外访问它。

最佳答案

@property (nonatomic, retain) NSMutableArray *numberQuery;

你必须平衡它

@synthesize numberQuery;

在.m

正确的创作应该是

self.numberQuery = [NSMuatableArray arrayWithCapacity:someNumber];

通过这样做

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

你没有使用你的@property 你正在创建一个新变量,这就是为什么你得到警告说你的变量没有被使用,因为它的作用域实际上只是 viewDidLoad 方法而不是对象。

关于objective-c - 访问 NSMutableArray 的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8676824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com