Custom UIButton for iOS and custom uibutton for ios
There is a need to click the button to update the text on the button. You can easily implement this requirement with a custom button.
First, write a Custom button.
CustomButton. h
#import <UIKit/UIKit.h>typedef NS_ENUM(NSUInteger, CustomButtonStatus){ CustomButtonStatusNormal = 0, CustomButtonStatusReverse = 1};@interface CustomButton : UIButton@property(nonatomic) CustomButtonStatus buttonStatus;@end
CustomButton. m
# Import "CustomButton. h "@ implementation CustomButton-(void) setButtonStatus :( CustomButtonStatus) buttonStatus {NSString * title; if (CustomButtonStatusNormal = buttonStatus) {title = @" ah ";} else if (CustomButtonStatusReverse = buttonStatus) {title = @ "Oh oh";} [self setTitle: title forState: UIControlStateNormal]; _ buttonStatus = buttonStatus;} @ end
Call
#import "ViewControllerTest.h"#import "CustomButton.h"@interface ViewControllerTest () { CustomButton *button;}@end@implementation ViewControllerTest- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; button = [CustomButton buttonWithType:UIButtonTypeCustom]; [button setButtonStatus:CustomButtonStatusNormal]; [button setFrame:CGRectMake(200, 80, 86, 42)]; [button addTarget:self action:@selector(customButtonClick:) forControlEvents:UIControlEventTouchUpInside]; [button setBackgroundColor:[UIColor redColor]]; [self.view addSubview:button];}-(void) customButtonClick: sender{ button.buttonStatus = !button.buttonStatus;}