【问题标题】:Use one UIButton same as UISwitch使用一个与 UISwitch 相同的 UIButton
【发布时间】:2014-05-02 03:20:26
【问题描述】:

我想将 UIButton 用作开关,第一次点击打开,第二次点击关闭 - 及以后。

我阅读了stackoverflow article 并按照描述创建了一个新课程ToggleButton。通过从堆栈溢出中得到善意的建议,在编码级别上没有出现错误消息。然而,在运行程序时,按下了切换按钮并被错误“[ViewController ToggleOn:]: unrecognized selector sent to instance”终止。

是否需要进一步的编码改进?

我的示例代码如下。

//ToggleButton.h:

@interface ToggleButton : UIButton
@property (nonatomic, getter=isOn) BOOL on;
-(void)toggle;
@end


//ToggleButton.m:

#import "ToggleButton.h"
@interface ToggleButton ()
@property (nonatomic, strong) UIImage *offStateImage;
@property (nonatomic, strong) UIImage *onStateImage;
-(void)touchedUpInside:(UIButton*) sender;
@end

@implementation ToggleButton
@synthesize on = _on;
@synthesize offStateImage = _offStateImage;
@synthesize onStateImage = _onStateImage;

-(void)awakeFromNib
{
[super awakeFromNib];

self.offStateImage = [self imageForState:UIControlStateNormal];
self.onStateImage = [self imageForState:UIControlStateHighlighted];

[self addTarget:self
         action:@selector(touchedUpInside:)
forControlEvents:UIControlEventTouchUpInside];
}


-(void)touchedUpInside:(UIButton*) sender
{ [self toggle]; }

-(void)toggle
{self.on = (!_on);}

-(void)setOn:(BOOL) on
{
_on = on;

if (on)
    [self setImage:self.onStateImage forState:(UIControlStateNormal)];
else
    [self setImage:self.offStateImage forState:(UIControlStateNormal)];
}

@end


//  ViewController.h`
#import <UIKit/UIKit.h>
#import "ToggleButton.h"

@interface ViewController : UIViewController
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *ai;
@property (nonatomic, retain) IBOutlet UISwitch *sw;
@property (nonatomic, retain) IBOutlet ToggleButton *tb;
@end


//ViewController.m

#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController
@synthesize ai;
@synthesize sw;
@synthesize tb;

- (void)viewDidLoad
{
[super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

//IBSwitch implementation

- (IBAction)SwitchChanged:(id)sender {
if(sw.on){    
[ai startAnimating];

}else{

[ai stopAnimating];}

}


//ToggleButton implementation

- (IBAction)ToggleButton:(id)sender {

if(tb.on){

    [ai startAnimating];

}else{

    [ai stopAnimating];}
}
@end

【问题讨论】:

  • 请查找最新教程。您的代码中没有理由使用 @synthesize 行。
  • 在 Image Builder 中,您可以添加 UIButton,然后将其自定义类设置为 ToggleButton - 然后将创建您的类的实例,而不是通用的 UIButton
  • @Paulw11:界面生成器
  • 噢!是的,他说的

标签: ios objective-c uibutton uiswitch


【解决方案1】:

在界面生成器上添加一个按钮并将自定义类设置为 ToggleButton(左起第三个选项卡)。

在 ViewController.h 中导入 ToggleButton.h 并添加这一行

@property (nonatomic, retain) IBOutlet ToggleButton *tb;

tb- (IBAction)ToggleButton:(id)sender 连接到界面生成器上的按钮。

在您的 ToggleButton.m 类中替换此方法

-(void)toggle
{self.on = (_on);}

-(void)toggle
{self.on = (!_on);}

现在它可以正常工作了。

【讨论】:

    【解决方案2】:

    如果您使用的是界面生成器:如 Paulw 所说,将自定义类设置为 ToggleButton

    如果您在代码中创建按钮,您只需在视图控制器中创建ToggleButton 类的新实例。

    【讨论】:

      猜你喜欢
      • 2011-08-02
      • 2016-07-26
      • 2011-11-03
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多