Method invocation in UIViewController from UIButton in preview

Continuing to learn Objective-C and building the structure correctly.

I have an iOS application with a UIViewController that has a specific method called "doSomething". In my opinion, the controller has a view and in this view there are several UIButton that I create programmatically (see the example with one button below).

enter image description here

Now, when I click the button, I want to call my "doSomething" method. The way I am doing this now is as follows:

[myButton addTarget:nil 
             action:@selector(doSomething:)
     forControlEvents:UIControlEventTouchUpInside];

Since my goal is zero, it goes up the defendant chain until it finds the "doSomething" method. It works, but in fact it does not feel good.

@protocol, . , . , , , .

?

!

+5
2

, .

Edit:

[myButton addTarget:controller 
             action:@selector(doSomething:)
     forControlEvents:UIControlEventTouchUpInside];

, , , UIViewController. , .

Edit2:

:

@property (assign) UIViewController* controller;

:

@synthesize controller;

:

- (void) viewDidLoad {
    [super viewDidLoad];
    someView.controller = self;
}
+4

, , addTarget self.

[myButton addTarget:self 
             action:@selector(doSomething:)
     forControlEvents:UIControlEventTouchUpInside]

doSomething , ​​. .

0

All Articles