如何根据objective-c中的另一个按钮状态更改一个按钮的标题?

时间:2015-02-13 22:02:36

标签: ios objective-c button

我对Objective-c很新。这就是我想要实现的目标:

我有两个名为buttonOne和buttonTwo的按钮。 buttonOne的默认标题是“One”。现在,当我触摸buttonTwo时,我希望它保持突出显示(背景更改),就像被按下一样,同时我希望buttonOne的标题更改为“Two”。当我再次触摸buttonTwo时,我希望它恢复到正常状态,而buttonOne的标题也会变回“One”。我做过这样的事情:

[_buttonTwo addTarget:self action:@selector(buttonTwoPressed:)  forControlEvents:UIControlEventTouchDown];
[_buttonTwo addTarget:self action:@selector(buttonTwoReleased:) forControlEvents:UIControlEventTouchUpInside];

- (IBAction)buttonTwoPressed:(id)sender {
[_buttonOne setTitle:@"Two" forState:UIControlStateNormal];
[sender setBackgroundColor:[UIColor grayColor]];
}

- (IBAction)buttonTwoReleased:(id)sender {
[_buttonOne setTitle:@"One" forState:UIControlStateNormal];
}

这不能正常工作。当我触摸buttonTwo时,按钮的背景会改变,但buttonOne的标题不会改变。我必须不断按住buttonTwo以查看buttonOne的标题更改为“Two”。此外,当我再次触摸buttonTwo时,没有任何反应。

提前致谢!

2 个答案:

答案 0 :(得分:0)

在当前代码中,当用户从按钮上取下手指时,您将立即重置标题。

您可以使用单一方法执行此操作:

[_buttonTwo addTarget:self action:@selector(buttonTwoPressed:) forControlEvents:UIControlEventTouchUpInside];

实施如下方法:

- (IBAction)buttonTwoPressed:(UIButton *)sender
{
   sender.selected = !sender.selected;
   if (sender.selected)
   {
     [_buttonOne setTitle:@"Two" forState:UIControlStateNormal];
     [sender setBackgroundColor:[UIColor grayColor]];
   }
   else
   {
     [_buttonOne setTitle:@"One" forState:UIControlStateNormal];
   }
}

答案 1 :(得分:0)

在某些情况下,您可能在此实施中忽略了这些情况。例如,因为您对控制事件UIControlEventTouchUpOutside没有任何操作,您可以在触摸时捕获状态更改但是错过了更改,因为触摸事件结束之前触摸区域移动(在释放/ touchUp之前拖动按钮...)

我正在寻找控制状态的变化,而不是touchEvents。不幸的是 - (UIControlState)状态是只读的。所以没有可以覆盖的setter,但是我们可以捕获紧接/更改controlState之前的事件。这是UIControl的东西,让我们称这个策略为A. 另一种方法可能是覆盖UIView的触摸事件,让我们调用这个策略B. 这两种策略都利用了默认实现,所以在我们通知我们的委托之前,将消息传递给super(超类,即如果我们根本没有实现该方法会发生什么)是很重要的

UIButton子类并执行类似的操作。不要忘记你需要更换你的笔尖或故事板中的按钮类或者你做的任何事情来设置它们。

接口:

#import <UIKit/UIKit.h>

@protocol StateButtonDelegate;

@interface StateButton : UIButton
@property (nonatomic, weak) id<StateButtonDelegate> delegate;
@end


@protocol StateButtonDelegate <NSObject>
-(void)button:(StateButton *)button didChangeState:(UIControlState )newState;
@end

实现:

    //  StateButton.m
    #import "StateButton.h"

    @implementation StateButton

//strategyA 

    -(void)setSelected:(BOOL)selected{
      [super setSelected:selected];
      [self.delegate button:self didChangeState:self.state];
    }
    -(void)setHighlighted:(BOOL)highlighted{  
      [super setHighlighted:highlighted];
      [self.delegate button:self didChangeState:self.state];
    }
    -(void)setEnabled:(BOOL)enabled{
      [super setEnabled:enabled];
      [self.delegate button:self didChangeState:self.state];
    }

//or
//strategyB

-(void )touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  [super touchesBegan:touches withEvent:event];
  [self.delegate button:self didChangeState:self.state];
}
-(void )touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
  [super touchesCancelled:touches withEvent:event];
  [self.delegate button:self didChangeState:self.state];
}
-(void )touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
  [super touchesEnded:touches withEvent:event];
  [self.delegate button:self didChangeState:self.state];
}
    @end

然后将协议添加到您的控制器,将其委托给两个按钮,您可能有一些控制

 -(void)button:(StateButton *)button didChangeState:(UIControlState )newState{

if (button == _buttonOne){

  if (newState & UIControlStateNormal){
    //doStuff
    }else if (newState & UIControlStateHighlighted){
    //doOtherStuff
    }else if (newState & UIControlStateSelected){
    //doDifferentStuffAgain
    } 
}else if (button == _buttonTwo){

   if (newState & UIControlStateNormal){
    //doStuff
    }else if (newState & UIControlStateHighlighted){
    //doOtherStuff
    }else if (newState & UIControlStateSelected){
    //doDifferentStuffAgain
    } 
  }  
}
相关问题