2

我有一个类CustomViewUIView没有 xib 文件的子类),我正在其中创建一些标签和按钮。我想在另一个类中使用这个类UIViewController来添加这些标签和按钮。我可以使用自定义视图将标签和按钮添加到我的 viewController,但是如果我向按钮(在自定义视图中)添加一些操作或事件,它就不起作用。请建议我应该怎么做才能为按钮添加操作。

//ViewController code

CustomView *slider=[[CustomView alloc]init];
[self.view addSubview:slider];

//CustomView code


toggleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[toggleButton setTitle:@"" forState:UIControlStateNormal];
toggleButton.userInteractionEnabled=YES;

// add drag listener
[toggleButton addTarget:self action:@selector(wasDragged:withEvent:)
       forControlEvents:UIControlEventTouchDragInside];



// center and size
toggleButton.frame = CGRectMake(frame.origin.x, frame.origin.y, width, frame.size.height);

toggleButton.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.0 alpha:0.1];
[toggleButton.layer setBorderWidth:4.0];
[toggleButton.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];
toggleButton.layer.cornerRadius=4.0;
[toggleButton setTitleColor:[UIColor colorWithRed:0.3 green:0.1 blue:0.4 alpha:1.0] forState:UIControlStateNormal];

// add it, centered
[self addSubview:toggleButton];


  - (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event 
  {
   NSLog(@"inside drag");
  }
4

4 回答 4

2

用以下代码替换您的代码:

自定义视图.h:

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface CustomView : UIView

@property (nonatomic, strong) UIButton *toggleButton;

@end

自定义视图.m:

#import "CustomView.h"

@implementation CustomView

@synthesize toggleButton;

- (id)initWithFrame:(CGRect)frame
{
      self = [super initWithFrame:frame];
      if (self) {
            // Initialization code
            toggleButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [toggleButton setTitle:@"" forState:UIControlStateNormal];
            toggleButton.userInteractionEnabled=YES;

            // add drag listener
            [toggleButton addTarget:self action:@selector(wasDragged:withEvent:)
                       forControlEvents:UIControlEventTouchDragInside];

            // center and size
            toggleButton.frame = CGRectMake(frame.origin.x, frame.origin.y, width, frame.size.height);

           toggleButton.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.0 alpha:0.1];
           [toggleButton.layer setBorderWidth:4.0];
           [toggleButton.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];
           toggleButton.layer.cornerRadius=4.0;
           [toggleButton setTitleColor:[UIColor colorWithRed:0.3 green:0.1 blue:0.4 alpha:1.0] forState:UIControlStateNormal];

           // add it, centered
           [self addSubview:toggleButton];
    }
    return self;
}

- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
      NSLog(@"inside drag");
}

将 CustomView 添加到 ViewController 中,例如:

CustomView *slider=[[CustomView alloc]initWithFrame:CGRectMake(x, y, width, height)];
[self.view addSubview:slider];

我测试了这段代码,它可以工作:)

于 2013-02-21T14:03:14.623 回答
0

替换此行

[toggleButton addTarget:self action:@selector(wasDragged:withEvent:)
       forControlEvents:UIControlEventTouchDragInside];

有了这个

[toggleButton addTarget:self action:@selector(wasDragged:withEvent:)
       forControlEvents:UIControlEventTouchUpInside];

希望这会奏效

于 2013-02-21T06:51:49.103 回答
0

你把方法 wasDragged:withEvent 放在哪里了?因为在CustomView代码中,你将按钮目标设置为self,也就是customview。所以按钮 TouchDragInside 事件将被发送到 customview 而不是 viewController。如果你将 wasDragged:withEvent 放在 ViewController 中,它不会工作

于 2013-02-21T06:53:24.973 回答
0

您添加了 target 作为selfaction wasDragged:withEvent。为此(假设wasDragged:withEvent在您的自定义视图中声明),您必须将代码重写为

[toggleButton addTarget:slider action:@selector(wasDragged:withEvent:)
       forControlEvents:UIControlEventTouchDragInside];
于 2013-02-21T07:21:26.757 回答