我正在研究一个项目,我需要一些建议。我有这个按钮处理程序代码来处理我的所有按钮点击(如调查)。我想单击一个按钮,然后按钮将相应的对象添加到NSMutable数组。现在我的问题是使用可变数组或字典是否安全,因为可以添加或删除可变对象。以及如何检查对象是否正确添加到数组中。我有一个NSUInteger来查看对象是否正确添加但是我在数组1中的NSlog对象中接收,每当我点击另一个按钮添加另一个对象时,我收到相同的消息,但是它的NSlog再次打印1我不确定对象是否正确添加。
-(IBAction)checkBoxButtonHandler:(id)sender
{// array being allocated
NSMutableArray *arrayOfselections = [NSMutableArray array];
if (sender == checkButton1)
{
if (!buttonChecked)
{
[arrayOfselections addObject:@"Freshman"];
NSLog(@"Freshman Checked");
[checkButton1 setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
buttonChecked = YES;
}
else
{
[checkButton1 setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
buttonChecked = NO;
}
}
if (sender == checkB2)
{
// [arrayOfselections addObject:@"Sophmore"];
// NSLog(@"check 2");
if (!buttonChecked)
{
[arrayOfselections addObject:@"Sophmore"];
NSLog(@"Sophmore Checked");
[checkB2 setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
buttonChecked = YES;
}
else
{
[checkB2 setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
buttonChecked = NO;
}
}
[arrayOfselections accessibilityElementCount];
if (sender == checkBox3)
{
//NSLog(@"check 3");
if (!buttonChecked)
{
[arrayOfselections addObject:@"Junior"];
NSLog(@"Junior Checked");
[checkBox3 setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
buttonChecked = YES;
}
else
{
[checkBox3 setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
buttonChecked = NO;
}
}
if (sender == checkBx4)
{
NSLog(@"Checked 4");
if (!buttonChecked)
{
[arrayOfselections addObject:@"Senior"];
NSLog(@"Senior Checked");
[checkBx4 setImage:[UIImage imageNamed:@"checkBoxMarked.png"] forState:UIControlStateNormal];
buttonChecked = YES;
}
else
{
[checkBx4 setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
buttonChecked = NO;
}
NSUInteger arraySize = [arrayOfselections count];
NSLog(@"Number of stuff inside array: %ui",arraySize);
}
答案 0 :(得分:2)
每次按下按钮都会创建新数组,这发生在以下行中:
NSMutableArray *arrayOfselections = [NSMutableArray array];
您只需创建一次。最好的方法是将它移动到viewDidLoad方法,它应该完成这项工作。
答案 1 :(得分:1)
您需要使数组成为视图控制器的属性:
MyViewController.m:
@interface MyViewController ()
@property (nonatomic) NSMutableArray *arrayOfSelections;
@end
在viewDidLoad
中初始化:
- (void)viewDidLoad {
[super viewDidLoad];
self.arrayOfSelections = [NSMutableArray new];
...
}
-(IBAction)checkBoxButtonHandler:(id)sender
{
// Remove this line:
//NSMutableArray *arrayOfselections = [NSMutableArray array];
...
// and change any references to self.arrayOfSelections:
[self.arrayOfselections addObject:@"Freshman"];
}
答案 2 :(得分:0)
更漂亮的设计是使用标签和字典而不是if-else,所以
// assign tags:
checkButton1.tag = 1;
checkButton2.tag = 2;
// etc., then setup data beforehand
NSDictionary *mapTags = @{ @1:@"Freshman", @2:@"Sophomore" /* etc */ };
NSMutableArray *arrayOfselections = [NSMutableArray array];
// then look how small this gets:
-(IBAction)checkBoxButtonHandler:(id)sender {
NSNumber *tagNumber = [NSNumber numberWithInt:sender.tag];
[self.arrayOfselections addObject:self.mapTags[tagNumber]];
}