我不确定以前是否有人问过这个问题,但我还是要问一下。我正在处理一个按钮网格,每个按钮的标题都是字母。我正在从数组中获取这些字母的内容,我相信这就是我的问题所在。标题看起来很好,但是当我按下阵列中的任何按钮时,这就是我的问题所在。
每次我按下一个字母,我的 NSLog 的一部分 NSLog(@"Letter Added to Array: %@", self.sectionButton.titleLabel.text);
显示
添加到数组的字母:S
这就是显示的全部内容。无论按下什么按钮。我想这可能只是因为 S
是我数组中的最后一个对象,这就是它这么说的原因。我不知道,所以任何帮助将不胜感激。
这是我的代码:
- (void) didTouchButton
{
[self.lettersPressedArray addObject:self.sectionButton.titleLabel.text];
NSLog(@"Letter Added to Array: %@", self.sectionButton.titleLabel.text);
NSLog(@"Letters Pressed = %@", self.lettersPressedArray);
}
- (void)showGridWithRows:(int)r columns:(int)c arrayOfContent:(NSArray *)content withSizeOfContent:(CGFloat)contentSize
{
for (int i = 0; i < content.count; i++) {
// vars
int row = (int)i / r; // to figure out the rows
int col = i % c; // to figure out the columns
float x = 0;
float y = 0;
// sizing options
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize screen = [UIScreen mainScreen].bounds.size;
if (screen.height == 480) {
x = 1 + (40 * col);
y = 1 + (30 * row);
}
else {
x = 2 + (40 * col);
y = 1 + (31 * row);
}
}
else {
x = .5 + (90 * col);
y = 1 + (90 * row);
}
//button
self.sectionButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.sectionButton setTintColor:[UIColor lightGrayColor]];
self.sectionButton.frame = CGRectMake(x, y, contentSize, contentSize);
self.sectionButton.tag = 100 + row * c + col;
[self.sectionButton addTarget:self action:@selector(didTouchButton) forControlEvents:UIControlEventTouchUpInside];
// font stuff
self.sectionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
self.sectionButton.titleLabel.backgroundColor = [UIColor clearColor];
self.sectionButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:22];
[self.sectionButton setTitleColor:[UIColor blackColor]/*[UIColor colorWithRed:241.0/255.0 green:124.0/255.0 blue:22.0/255.0 alpha:1.0]*/ forState:UIControlStateNormal];
// title
s = (NSString *)[content objectAtIndex:i];
[self.sectionButton setTitle:s forState:UIControlStateNormal];
// color
if ([s isEqualToString:@"A"] ) {
[self.sectionButton setBackgroundColor:[UIColor greenColor]];
}
else if([s isEqualToString:@"Z"]) {
[self.sectionButton setBackgroundColor:[UIColor redColor]];
}
else {
[self.sectionButton setBackgroundColor:[UIColor lightGrayColor]];
}
//layer
self.sectionButton.layer.borderWidth = .65;
//self.sectionButton.layer.cornerRadius = 5.0;
for(int j = 0; j < c; j++) {
for (int k = 0; k < r; k++) {
[self addSubview:self.sectionButton];
}
}
}
最佳答案
改变这个:
- (void) didTouchButton
{
[self.lettersPressedArray addObject:self.sectionButton.titleLabel.text];
NSLog(@"Letter Added to Array: %@", self.sectionButton.titleLabel.text);
NSLog(@"Letters Pressed = %@", self.lettersPressedArray);
}
对此:
- (void) didTouchButton:(UIButton *)sender
{
[self.lettersPressedArray addObject:sender.titleLabel.text];
NSLog(@"Letter Added to Array: %@", sender.titleLabel.text);
NSLog(@"Letters Pressed = %@", self.lettersPressedArray);
}
当您分配按钮选择器时添加一个“:”
所以,这个:
@selector(didTouchButton)
将是:
@selector(didTouchButton:)
可能是这样实现的:
[someButton addTarget:self action:@selector(didTouchButton:) forControlEvents:UIControlEventTouchUpInside];
因为:
您最初通过 self.selectionButton.titleLabel.text
引用按钮的方式访问按钮的一个特定实例。这意味着无论什么按钮触发 didTouchButton
,它都会获取 self.selectionButton
的内容,我假设它显示一个“S”。这意味着无论发生什么,您都会向数组中添加更多字母“S”。通过像我们一样配置我们的操作方法,它会将“self”作为参数传递。这意味着我们有一个变量代表在方法中调用该方法的人。我们将使用它来获取我们的内容并将它们添加到数组中。
关于ios - 获取 UIButton 按下的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23644305/