Nous pouvons l'atteindre en utilisant l'étape suivante. 1. Créez un nouveau fichier avec la sous-classe 'UIView' et nommez-le 'customButom' 2. Ajoutez un fichier d'interface UIView et nommez-le customButtom.xib et liez le fichier customButtom avec xib. 3. Après cela, réglez la taille UIView sur Freedom dans le panneau utilitaire. Et définissez la largeur et la hauteur souhaitées. Dans cet exemple, je l'ai défini 160x50 J'ai créé un protocole, des propriétés et IBAction pour le handle d'événement.
//customButton.h
#import<UIKit/UIKit.h>
// createdcustomButtonDelegate protocol
@protocolcustomButtonDelegate<NSObject>
-(void)performButtonClicked;
@end
@interfacecustomButton : UIView
@property(nonatomic,retain)id<customButtonDelegate>delegate;
-(IBAction)customButtonClicked:(id)sender;
@end
//customButton.m Implemented properties and button action `
#import"customButton.h"
@implementationcustomButton
-(id)initWithFrame:(CGRect)frame {
self = [superinitWithFrame:frame];
if (self) {
UIView *buttonView =[[[NSBundlemainBundle]loadNibNamed:@"customButton"owner:selfoptions:nil] objectAtIndex:0];
[selfaddSubview:buttonView];
}
returnself;
}
-(IBAction)customButtonClicked:(id)sender {
if (self.delegate != nil) {
[self.delegateperformButtonClicked];
}
}
@end
// Viewcontroller.h Import customButton.h file in our view controller and add delegate `
#import<UIKit/UIKit.h>
#import"customButton.h"
@interfaceViewController : UIViewController<UIAlertViewDelegate,customButtonDelegate>
@end
// Viewcontroller.m file
@interfaceViewController()
@end
@implementationViewController {
customButton *buttonView;
}
- (void)viewDidLoad {
[superviewDidLoad];
// we can display/set our custom view on specific location on view. just need to provide your desire Frame
//creating customButton object and added this object to our view.
buttonView = [[customButtonalloc]initWithFrame:CGRectMake(80, 465, 160, 50)];
buttonView.delegate = self;
[self.viewaddSubview:buttonView];
}
#pragma mark custombutton delegate
-(void)performButtonClicked {
NSLog(@"Perform whatever you want to");
}
@end