创建可在同一按钮的后面的点击被添加到一个可变数组? [英] creating a Mutable array that can be added to in later clicks of the same button?
问题描述
一般的小白问题:
(1)如何创建在我可以添加更多的项目到 buttonClicked
操作的 NSMutable
阵列在同一按钮,随后单击?我似乎总是与每点击一个新的数组重新来过(数组打印,只有1项,这是最近期的按钮在一个的NSLog
语句标记)。
(1) How can I create an NSMutable
array in a buttonClicked
action that I can add more entries to during subsequent clicks of the same button? I always seem to start over with a new array at every click (the array prints with only 1 entry which is the most recent button's tag in an NSLog
statement).
我有大约100按钮(一个在我的字符串的每个字符被称为清单)在我的code。通过一个for循环生成的更早,而且每个都分配了一个标签。他们是在一个滚动视图我的的ViewController
的视野内。
I have about 100 buttons (one for each character in my string called "list") generated by a for-loop earlier in my code, and each has been assigned a tag. They are in a scrollview within the view of my ViewController
.
我想保留的按钮跟踪有多少(以及哪些)已被点击与选择的删除这些条目如果点击他们第二次 。
这是我迄今为止:
-(void) buttonClicked:(UIButton *)sender
NSMutableArray * theseButtonsHaveBeenClicked = [[NSMutableArray alloc] initWithCapacity: list.length];
NSNumber *sendNum = [NSNumber numberWithInt:sender.tag];
[theseButtonsHaveBeenClicked addObject:sendNum at index:sender.tag];
NSLog(@"%@",theseButtonsHaveBeenClicked);
}
(2)我已阅读,我也许能够使用的plist字典,而是我真的不明白,我怎么会完成,在code,因为我不能打出来的手工词典中的项目(因为我不知道用户将点击的按钮)。这将是更容易,如果我莫名其妙地装,取而代之的plist文件字典?而且我会怎么做呢?
(2) I have read that I may be able to use a plist dictionary but I don't really understand how I would accomplish that in code since I cant type out the items in the dictionary manually (since I don't know which buttons the user will click). Would this be easier if I somehow loaded and replaced the dictionary in a plist file? And how would I do that?
(3)我也有不知道我应该怎么内存管理这个因为我需要不断更新的阵列。 自动释放
?
(3) I also have no idea how I should memory manage this since I need to keep updating the array. autorelease
?
感谢您的帮助,您可以提供!
Thanks for any help you can provide!
推荐答案
好吧,首先要创建一个本地范围的阵列,正在在每次调用重新初始化为 buttonClicked:。该变量应该是类的init周期的一部分。
Okay, firstly you are creating a locally scoped array that is being re-initialised on every call to buttonClicked:. The variable should be part of the class init cycle.
您也将与的NSMutableDictionary 而不是一个NSMutableArray更好。借助字典,我们没有指定的容量,我们可以使用该按钮的标签作为字典键。
You will also be better off with an NSMutableDictionary instead of an NSMutableArray. With a dictionary we don't have to specify capacity and we can use the button's tags as dictionary keys.
下面是你需要做的,这三个步骤总是一起去:财产/合成/释放。一个好的记忆。
Here's what you need to do, these three steps always go together: property/synthesize/release. A good one to remember.
//Add property declaration to .h file
@property (nonatomic, retain) NSMutableDictionary * theseButtonsHaveBeenClicked;
//Add the synthesize directive to the top of .m file
@synthesize theseButtonsHaveBeenClicked;
// Add release call to the dealloc method at the bottom of .m file
- (void) dealloc {
self.theseButtonsHaveBeenClicked = nil; // syntactically equiv to [theseButtonsHaveBeenClicked release] but also nulls the pointer
[super dealloc];
}
接下来,我们创建一个存储对象时,类实例被初始化。添加到您的类的的init 或 viewDidLoad中方式。
self.theseButtonsHaveBeenClicked = [[NSMutableDictionary alloc] dictionary]; // convenience method for creating a dictionary
和更新后的 buttonClicked:方式应该看起来更像这个
And your updated buttonClicked: method should look more like this.
-(void) buttonClicked:(UIButton *)sender {
NSNumber *senderTagAsNum = [NSNumber numberWithInt:sender.tag];
NSString *senderTagAsString = [[NSString alloc] initWithFormat:@"%@",senderTagAsNum];
// this block adds to dict on first click, removes if already in dict
if(![self.theseButtonsHaveBeenClicked objectForKey:senderTagAsString]) {
[self.theseButtonsHaveBeenClicked setValue:senderTagAsNum forKey:senderTagAsString];
} else {
[self.theseButtonsHaveBeenClicked removeObjectForKey:senderTagAsString]; }
[senderTagAsString release];
NSLog(@"%@", self.theseButtonsHaveBeenClicked);
}
这篇关于创建可在同一按钮的后面的点击被添加到一个可变数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!