我有一个ViewController
,它有六个按钮。每个按钮都有.tag
s 1到6,并且都连接到使用IBAction
来确定要执行的.tag
的同一[self.view viewWithTag: tag]
。 4英寸设备上的一切都很棒。
然而,在3.5英寸设备中,我需要重新排列按钮以使其适合。我尝试使用.frame
检索按钮并更改检索到的按钮-(void) rearrangeButtons
{
int space = 7; // space between the first button and the left side of the screen as well as between individual buttons
for(int i = 0; i < 6; i++) {
// Get the button and remove from superview:
UIButton *button = (UIButton *)[self.view viewWithTag: i+1];
[button removeFromSuperview];
// Resize its width and height:
UIButton *newButton = [UIButton buttonWithType: UIButtonTypeCustom];
newButton.frame = CGRectMake(space + (i*(button.frame.size.width/2)) + (i*space), 170, button.frame.size.width/2, button.frame.size.height/2);
newButton.tag = button.tag;
//[newButton addTarget:self action:@selector(highlightButtonWithTag:) forControlEvents:UIControlEventTouchDown];
[newButton targetForAction:@selector(highlightButtonWithTag:) withSender:newButton];
[newButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"icon%i", i+1]] forState:UIControlStateNormal];
[self.view addSubview: newButton];
}
}
,但这似乎没有做任何事情。因此,我做了以下事情:
- (IBAction) selectIcon:(UIButton *)sender
{
NSLog(@"called");
// "Un"highlight previous button
UIButton *prevButton = (UIButton *)[self.view viewWithTag: self.selectedIcon];
prevButton.highlighted = NO;
// Highlight tapped button:
self.selectedIcon = sender.tag;
[self performSelector:@selector(highlightButtonWithTag:) withObject:@(self.selectedIcon) afterDelay:0];
}
这将删除按钮并使用新按钮放置它们。安排它们工作正常。但是,我想要被调用的选择器没有被调用,我似乎无法操纵按钮的属性,因为我可以使用我之前做的按钮(尽管这也是纯粹基于他们的标签。
{{1}}
我在这里缺少什么?谢谢!
答案 0 :(得分:0)
尝试使用随标记定义的现有按钮实例,而不是创建新的按钮实例。在这种情况下,您不必在superview中添加和删除按钮。只需重复使用它们并在这些按钮上设置框架
-(void) rearrangeButtons
{
int space = 7; // space between the first button and the left side of the screen as well as between individual buttons
for(int i = 0; i < 6; i++) {
UIButton *newButton = (UIButton *)[self.view viewWithTag: i+1] ;
if(newButton == nil) {
newButton = [UIButton buttonWithType: UIButtonTypeCustom];
newButton.tag = i+1;
[self.view addSubview: newButton];
}
newButton.frame = CGRectMake(20, i*(i+1) *20, 50, 50);
[newButton addTarget:self action:@selector(selectIcon:) forControlEvents:UIControlEventTouchDown];
// [newButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"icon%i", i+1]] forState:UIControlStateNormal];
[newButton setBackgroundColor:[UIColor redColor]];
}
}
- (IBAction) selectIcon:(UIButton *)sender
{
NSLog(@"called");
// "Un"highlight previous button
UIButton *prevButton = (UIButton *)[self.view viewWithTag: self.selectedIcon];
prevButton.highlighted = NO;
// Highlight tapped button:
self.selectedIcon = sender.tag;
[self highlightButtonWithTag:self.selectedIcon];
}
-(void)highlightButtonWithTag:(NSInteger) selectedIcon
{
NSLog(@"highlightButton at position: %ld", (long)selectedIcon);
}
答案 1 :(得分:0)
为什么不使用addTarget:action:forControlEvents:?
[newButton addTarget:self
action:@selector(highlightButtonWithTag:)
forControlEvents:UIControlEventTouchUpInside];