2013-02-21 37 views
2

我有一個類CustomViewUIView的子類沒有xib文件),我在其中創建一些標籤和按鈕。我想在我的另一個UIViewController中使用這個類來添加這些標籤和按鈕。我能夠使用自定義視圖添加標籤和按鈕到我的viewController,但如果我添加一些操作或事件到按鈕(這是在自定義視圖中),它不起作用。請建議我應該如何爲按鈕添加操作。在UIView的子類中添加一個按鈕和動作

//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"); 
    } 
+0

這是相關的則hitTest? – user1787741 2013-02-21 08:40:37

回答

2

與以下替換代碼:

CustomView.h:

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

@interface CustomView : UIView 

@property (nonatomic, strong) UIButton *toggleButton; 

@end 

CustomView.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]; 

我測試此代碼,它的工作原理:)

0

替換該行

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

與此

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

希望這將工作

+0

謝謝,爲答案,但它不工作。 – user1787741 2013-02-21 07:45:34

0

你在哪裏把方法wasDragged:withEvent?因爲在CustomView代碼中,您將按鈕目標設置爲self,即customview。所以按鈕TouchDragInside事件將被髮送到customview而不是viewController。如果你在ViewController中放置了wasDragged:withEvent,它不會工作

+0

是的,我的代碼只在CustomView(UIView的子類)中,但控件甚至沒有進入方法。 – user1787741 2013-02-21 07:42:22

0

你添加了目標爲self的行動wasDragged:withEvent。對於這個工作(假設wasDragged:withEvent在您自定義視圖聲明),您必須將代碼重寫

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

此代碼僅在CustomView內部,我在CustomView中創建此按鈕。只有我使用了自己。 – user1787741 2013-02-21 07:46:27

+0

我已經更新了上面的代碼,我剛剛添加了一個方法,我打算處理UIControlEventTouchDragInside。有沒有其他辦法來處理這個事件? – user1787741 2013-02-21 09:01:45

+0

相同的代碼完美工作如果我在我的視圖控制器中編寫所有東西,但現在我的整個代碼是在UIView的子類中,並且在這裏它的按鈕操作不起作用 – user1787741 2013-02-21 09:04:27

相關問題