2010-05-23 4 views
4

Ma sous-classe personnalisée s'étendre UIControl (EditableImageView) Fondamentalement, il contient 3 sous-vues:événement avant de la sous-classe UIControl personnalisée

  • UIButton (UIButtonTypeCustom)
  • UIImageView
  • UILabel

Toutes les La classe fonctionne bien mais maintenant je veux connecter certains événements générés de cette classe à l'autre. En particulier lorsque le bouton est pressé je veux transférer l'événement au propriétaire viewcontroller afin que je puisse gérer l'événement. Le problème est que je ne peux pas comprendre comment implémenter ce comportement. Dans EditableImageView Je peux attraper l'événement tactile en utilisant [button addTarget: self action: @selector (buttonPressed :) forControlEvents: UIControlEventTouchUpInside] mais je ne sais pas comment le transférer dans le sélecteur buttonPressed. J'ai aussi essayé de mettre en œuvre touchesBegan, mais il ne semble jamais appelé ...

Je voudrais saisir l'événement presse bouton de la viewcontroller de cette façon:

- (void)viewDidLoad { 
[super viewDidLoad]; 

self.imageButton = [[EditableImageView alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 80.0f, 80.0f)]; 
[imageButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:imageButton]; 
[imageButton setEditing:NO]; 

}

C'est ma méthode d'initialisation de la sous-classe UIControl:

- (id)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
     [self setBackgroundColor:[UIColor clearColor]]; 

     button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame = CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height); 
     [button setImage:[UIImage imageNamed:@"nene_70x70.png"] forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

     [self addSubview:button]; 

     transparentLabelBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"editLabelBackground.png"]]; 
     transparentLabelBackground.hidden = YES; 
     [self addSubview:transparentLabelBackground]; 

     // create edit status label 
     editLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
     editLabel.hidden = YES; 
     editLabel.userInteractionEnabled = NO; // without this assignment the button will not be clickable 
     editLabel.textColor = [UIColor whiteColor]; 
     editLabel.backgroundColor = [UIColor clearColor]; 
     editLabel.textAlignment = UITextAlignmentLeft; 
     UIFont *labelFont = [UIFont systemFontOfSize:16.0]; 
     editLabel.font = labelFont;  
     editLabel.text = @"edit"; 
     labelSize = [@"edit" sizeWithFont:labelFont]; 

     [self addSubview:editLabel]; 
    } 

    return self; 
} 

Merci.

Répondre

6

Je résolus de cette façon:

EditableImageView.h:

@interface EditableImageView : UIControl { 
UIButton *button; 
UIImageView *transparentLabelBackground; 
UILabel *editLabel; 
CGSize labelSize; 

BOOL editing; 
} 

@property (nonatomic, retain) UIButton *button; 
@property (nonatomic, retain) UILabel *editLabel; 
@property (nonatomic, retain) UIImageView *transparentLabelBackground; 
@property (nonatomic, getter=isEditing) BOOL editing; 

- (void)buttonPressed:(id)sender; 
@end 

EditableImageView.m:

(id)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
     [self setBackgroundColor:[UIColor clearColor]]; 

     button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame = CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height); 
     [button setImage:[UIImage imageNamed:@"nene_70x70.png"] forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     ....... 
    } 
} 


- (void)buttonPressed:(id)sender { 
[self sendActionsForControlEvents:UIControlEventTouchUpInside]; 
} 

MyController.m:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.imageButton = [[EditableImageView alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 80.0f, 80.0f)]; 
    [imageButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:imageButton]; 
} 

- (void)buttonPressed:(id)sender { 
    NSLog(@"buttonPressed!"); 
} 
Questions connexes