Using a custom UIView from multiple view controllers

I created a custom UIView that I would like to use on several different view controllers in an iPhone application. Currently, I have copied a UIView that contains the controls from the nib file for the first view controller, pasted it into other view controllers, and then connected all the actions to each view controller. This works fine, but is not the optimal way I would like to use this custom view.

My user view is quite simple, it consists of some UIButtons with labels, and clicking on these buttons causes actions that change the contents of the controls in the view controller's view.

What would be a consolidation strategy for defining and using this UIView? Ideally, I would like to simply reference this user view from the nib view controller and check that my view controller responds to actions from this user view.

EDIT: OK, based on J.Biard's suggestions, I tried the following with great luck.

I created another UIView-based nib file with the contents (currently only some UIButton objects) of my reusable view and subclass of UIView.m and .h, and then set the owner class of the nib file for my newly created class name.

Then I added most of the code from J.Biard (I changed the rectangle to 50,50,100,100, at the moment I left setDelegate, since I was just trying to get it to work visually at the moment, and I found that [self.view addSubview: view ] worked much better than [self addSubView: view]) until the end of the viewDidLoad method of the first view controller that appears when the application starts.

And what I get now is my main view with a black square in it. Am I missing the exit somewhere, or is there some kind of initialization needed for initWithFrame or drawRect in a subclass of UIView?

+4
source share
2 answers

If it is very simple, I would recommend creating a subclass of UIView in the code and creating instances of this class (or you can use Interface Builder to create a custom UIView, which is then archived to a NIB file and restored later using code too).

Using the code solution, you can instantiate your custom UIView in the controller by calling something like:

#import "MyCustomView.h" // let the superview decide how big it should be or set it as needed. CGRect sizeRect = CGRectMake(0,0,0,0); // create an instance of your view MyCustomView *view = [MyCustomView alloc] initWithFrame:sizeRect]; // set a custom delegate on the view or set callback methods using @selector()... [view setDelegate:self]; // self = view controller // add the view to the controller somewhere... (eg: update CGRect above as needed) [self addSubView:view]; // don't forget to release the view somewhere ;-) 

This example assumes that you are creating a delegate protocol that your view controller can respond to, or you can dynamically bind events using @selector. If you do not want to create instances of the view in code, you can add "UIView" to your NIB file and set its class type in the inspector window (command drop-down menu → 4 →).

If you want to do everything in the interface builder, you can create your own UIView and use something like "- (NSArray *) loadNibNamed: (NSString *) owner name: (id) owner parameters: (NSDictionary *) options" (see . NSBundle) to load the NIB file dynamically. This presents its own problems, although it is also possible.

The most attractive option would be to create your own xcode user interface library of UIs so that your user control / view can be dragged into each NIB file from the library window, just like any other control provided by Apple.

We hope this clarifies or eliminates some of the options for reusing controls for you.

Cheers -

+3
source

Create your MyCustomView class and nib file.

In the nib file, set File Owner to MyCustomView, and then create your view as usual, with a top-level UIView. Create an IBOutlet in MyCustomView to reference your top-level UIView in the nib file.

In MyCustomView add this method:

 - (BOOL) loadMyNibFile { if (![[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil]) { return NO; } return YES; } 

In your view controllers, you can use this custom view this way

 - (void)viewDidLoad { MyCustomView *customView = [[MyCustomView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; [customView loadMyNibFile]; [self.view addSubview:customView.view]; //customView.view is the IBOutlet you created [customView release]; } 

You can also create a convenience class method in MyCustomView to do this if you want.

+5
source

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


All Articles