A simple example of how to solve the problem of no response when embedding UIButton in UIView and delegate

Source: Internet
Author: User

 

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.