I usually do the initialization of the button through

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

It is necessary to get rid of duplicate lines, for this I want to make custom initialization through the method

- (instancetype)init { self = [super init]; if (self) {
} return self; }

Is it possible to initialize a button via buttonType , in the init method ??

  • Do you need something like [UIButton customButton] that will return the button instance to you? - Max Mikheyenko 2:21 pm
  • Something like UIButton * button = [[UIButton alloc] initWithCustomButton] - Victor Mishustin

1 answer 1

You need to create a category for UIButton and create a new method there.

1) first create a new objective-C file

enter image description here

2) announce that this will be a category for UIButton

enter image description here

3) write code to create

.h

 @interface UIButton (CustomInits) + (instancetype)buttonWithTypeCustom; @end 

.m

 #import "UIButton+CustomInits.h" @implementation UIButton (CustomInits) + (instancetype)buttonWithTypeCustom { return [UIButton buttonWithType:UIButtonTypeCustom]; } @end 

4) import a category and create buttons

 UIButton *newButton = [UIButton buttonWithTypeCustom]; 
  • Thank you so much !! - Victor Mishustin