I encountered a problem when I encapsulated the UIView for a project. I didn't expect a buddy to write a blog. I would like to add it to my favorites!
The problem is that several interfaces use the same custom return button, so they want to encapsulate the button separately and add a UIView class to customize UIButton, use delegate to implement click events
// The header file XZXTopView. h of the UIView class
# Import <UIKit/UIKit. h>
@ Protocol BtnDelegate <NSObject> // defines a delegate
-(Void) dismissViewController; // declare a delegate Method
@ End
@ Interface XZXTopView: UIView {
Id <BtnDelegate> delegate; // declare the delegate variable
}
@ Property (nonatomic, strong) id <BtnDelegate> delegate; // declare the delegate attribute
@ End
// UIView class XZXTopView. m
# Import "XZXTopView. h"
@ Implementation XZXTopView
@ Synthesize delegate;
-(Id) initWithFrame :( CGRect) frame
{
Self = [super initWithFrame: frame];
If (self ){
// Initialization code
// Customize a UIButton
UIButton * button = [UIButtonbuttonWithType: UIButtonTypeCustom];
UIImage * image = [UIImage imageNamed: @ "B _back"];
[Button setImage: image forState: UIControlStateNormal];
[Button setFrame: CGRectMake (5 .., 7 .., image. size. width, image. size. height)];
[Button addTarget: selfaction: @ selector (buttonClicked :) forControlEvents: UIControlEventTouchUpInside];
[Self addSubview: button];
}
Return self;
}
-(Void) buttonClicked :( UIButton *) sender {
[Delegate dismissViewController]; // click the button to execute this delegate Method
}
// Header file of the UIViewController class XZXHelpViewController. h
# Import <UIKit/UIKit. h>
# Import "XZXTopView. h"
@ Interface XZXHelpViewController: UIViewController <BtnDelegate> // here
@ End
// The XZXHelpViewController. m file of the UIViewController class
# Import "XZXHelpViewController. h"
@ InterfaceXZXHelpViewController ()
@ End
@ Implementation XZXHelpViewController
-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil
{
Self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];
If (self ){
// Custom initialization
}
Returnself;
}
-(Void) viewDidLoad
{
[SuperviewDidLoad];
// Do any additional setup after loading the view from its nib.
TopView. delegate = self; // specify its proxy as its own when defining XZXTopView
[Self. view addSubview: topView];
}
-(Void) didReceiveMemoryWarning
{
[SuperdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// The specific execution method after clicking the button
-(Void) dismissViewController
{
[SelfdismissViewControllerAnimated: YES completion: NULL];
}
@ End
This is a simple proxy usage.
After the code is compiled and executed, the button is displayed normally, but the button does not respond. Why?
Due to the influence of inertial thinking, I think that since it can be displayed, the button is clicked. In fact, the button is not actually clicked, because we didn't set the frame of the UIView class on UIButton, that is
Correct initialization method:
Alternatively, you can set your own frame in XZXTopView.