Referencing objects programmatically configured with Interface Builder

I created several views, each of which contains a UIPickerView, each of which contains a UIToolbar, and each of which, in turn, has a UIBarButtonItem. All this is configured in IB. I have pointers to UIPickerViews in code. I need to reference UIBarButtonItems as part of a function that behaves differently depending on which of the UIPickerViews it deals with. I have a (id) sender as an argument to this function. The sender is always one of the UIBarButtonItems. I am trying to find which UIPickerView belongs to this particular UIBarButtonItem. I assume that I need to run the UIPickerView hierarchy and work with the UIBarButtonItem. I think this is the right approach because I cannot make a simple comparison, since I have no explicit reference to the UIBarButtonItem object,since it was configured in IB. Also, since subviews do not have a pointer to their parent view, I cannot work the way up the hierarchy. Right? If I understood this correctly, let's move on to the view hierarchy in each UIPickerView, find the UIToolBar, and then find the UIBarButtonItem? What exactly am I checking at both the UIToolBar level and the UIBarButtonItem level? I mean, do I execute the if statement, checking if the object is of type UIToolBar or UIBarButtonItem, respectively? How to find the type of an object in an if statement?and then find the UIBarButtonItem? What exactly am I checking at both the UIToolBar level and the UIBarButtonItem level? I mean, do I execute the if statement, checking if the object is of type UIToolBar or UIBarButtonItem, respectively? How to find the type of an object in an if statement?and then find the UIBarButtonItem? What exactly am I checking at both the UIToolBar level and the UIBarButtonItem level? I mean, do I execute the if statement, checking if the object is of type UIToolBar or UIBarButtonItem, respectively? How to find the type of an object in an if statement?

A related, but more general question is, should I just set it all up in code, so that I have an explicit reference to UIBarButtonItem, which I could compare with the sender (id) in the first place?

+3
source share
1 answer

To find out which one you need, do two things:

One. In code add IBOutlet <object-type> <var-name>to @interface. For example, if you have a UIButton to pause, you can do

@interface blabla {
    ...
    IBOutlet UIButton btnPause;
}

Two. In Interface Builder ctrl-drag from "File Owner" to the button and select "btnPause".

In the place where you receive (id)sender, you can do, for example,

if (sender == btnPause) { ... }
+2
source

Source: https://habr.com/ru/post/1782195/


All Articles