Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UIView的子类中添加按钮和操作_Ios_Objective C_Ios5 - Fatal编程技术网

Ios 在UIView的子类中添加按钮和操作

Ios 在UIView的子类中添加按钮和操作,ios,objective-c,ios5,Ios,Objective C,Ios5,我有一个类CustomView(不带xib文件的UIView的子类),我在其中创建一些标签和按钮。我想在另一个UIViewController中使用这个类来添加这些标签和按钮。我可以使用自定义视图将标签和按钮添加到我的viewController中,但是如果我向按钮(在自定义视图中)添加了一些操作或事件,它将不起作用。请建议我如何添加按钮的操作 //ViewController code CustomView *slider=[[CustomView alloc]init]; [self.vi

我有一个类
CustomView
(不带xib文件的
UIView
的子类),我在其中创建一些标签和按钮。我想在另一个
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");
  }
更换这条线

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

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

希望这能起作用

你把wasdrable方法放在哪里了:withEvent?因为在CustomView代码中,您将按钮目标设置为self,即CustomView。因此按钮TouchDragins事件将发送到customview而不是viewController。如果在ViewController中放置wasdrable:withEvent,它将不起作用

您为操作添加了目标作为
self
。要使其工作(假设在自定义视图中声明了
wasdrable:withEvent
),必须将代码重写为

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

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

CustomView.h:

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

@interface CustomView : UIView

@property (nonatomic, strong) UIButton *toggleButton;

@end
将CustomView添加到ViewController,如下所示:

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

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

是的,我的代码仅在CustomView(UIView的子类)中,但控件甚至不在方法中。这段代码仅在CustomView中,我正在CustomView中创建此按钮。因此,只有我使用了self。我更新了上面的代码,我刚刚添加了一个方法,我计划在其中处理UIControlEventTouchDragInside。有没有其他方法来处理该事件?如果我在视图控制器中编写所有内容,同样的代码也能很好地工作,但是现在我的整个代码都在UIView的子类中,这里的按钮操作不起作用。我想问您是想处理切换按钮内的拖动还是在自定义视图内的拖动。在UIView子类(哪个方法)中,您在哪里添加目标和操作?我想在切换按钮中处理拖动,添加的目标方法是-(void)wasdrable:(UIButton*)button withEvent:(UIEvent*)event,我在setFrame方法中添加它这与hitTest相关吗?
CustomView *slider=[[CustomView alloc]initWithFrame:CGRectMake(x, y, width, height)];
[self.view addSubview:slider];