开发者

Programmatically creating a UIButton inside a custom UIView subclass, how does one access the file owner to add a target method? delegates?

开发者 https://www.devze.com 2023-02-26 06:29 出处:网络
I have a root view controller which loads a custom UIView subclass I have created and adds it as a subview.

I have a root view controller which loads a custom UIView subclass I have created and adds it as a subview.

Inside this custom UIView subclass I code/generate a UIButton in the awakeFromNib method.

Is there a simple way to access the File Owner without creating a delegate if the UIButton's action method is inside the root view con开发者_如何学Pythontroller?

E.g

[myButton addTarget:[self.file_owner ?] action:@selector(methodInFileOwner:) ....

Using Interface Builder it's still easy to assign a UIView my custom UIView subclass and just drag a UIButton's selector reference to the file owner. Voila!

How is this done through code though? Do I have to create a delegate and use

[myButton addTarget:[self.delegate] ... 

?


File's Owner is an Interface Builder concept. It doesn't exist on the programming side, basically, because it's not needed. In interface Builder, File's Owner is the class that instantiates the nib file. Often, it just refers to the class of the nib file you're currently working with. Since you're working with a view controller, the File's Owner is your view controller subclass, and it allows you to make connections to instance variables and methods of that class.

On the programming side, in this case, the equivalent of File's Owner would just be self. And, you access an instance variable, using properties, as self.instanceVariable.

On to your question. If you want the selector method to be in the view controller, that makes perfect sense. But then, the view controller can create the button, set its target/action, and add it as a subview to the custom view. You could do this in -viewDidLoad, which is called after the nib file is loaded and is the standard place where you would make any programmatic additions to the view controller. So, you could do it as follows:

- (void)viewDidLoad {
    self.myButton = [[[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)] autorelease];
    self.myButton.buttonType = ...;
    [myButton addTarget:self action:@selector(actionMethod)...];
    self.myCustomView = [[[MyCustomViewClass alloc] initWithFrame:...] autorelease];
    [self.myCustomView addSubview:self.myButton.view];
    [super viewDidLoad];
}

The above code is just an example. You can initialize your objects in different ways. In this case, the button would now be an instance variable of the view controller. But, you could just as easily leave it in the custom view and just refer to it as: self.myCustomView.myButton

I hope this is helpful.

Correction: The above code should be in viewWillAppear rather than viewDidLoad. When viewDidLoad is called, the geometry (i.e. the view's bounds) has not yet been set. So, in order to set the frame of any object, it must be done in viewWillAppear.


The target should be an object of the root view controller class. In your UIView subclass you will need a reference to your root view controller class.


If a nested widget is hidden from the controller, then that essentially means that the custom view should manage all aspects of that nested widget. Here are some options (and probably not a complete list of them either):

  • You could have the custom subview handle UIControl events and propagate them into the button. Your custom subview would implement the methods of UIControl and essentially hand them down to the button.
  • You could also use a delegate like you mentioned.
  • Or you could restructure it so that the widget hierarchy is flattened, but their display is nested.

If you plan on making a custom component that you reuse in multiple places, then the first and second options are probably better since they are more flexible. If this is not the case, the third option is probably best since there is actual interaction between the button and the controller.

The Delegates and DataSources section of the Cocoa Fundamentals Guide gives an example of what the code looks like to create delegates.

0

精彩评论

暂无评论...
验证码 换一张
取 消