1
votes

I'm using a library, which is a class that inherits from UIView. How do I programmatically create an UIViewController that uses this class, and not a normal UIView?

The ViewController's .h file looks as follows:

#import <UIKit/UIKit.h>
#import "PLView.h"

@interface HelloPanoramaViewController : UIViewController {
IBOutlet PLView * plView;
}

@property (nonatomic, retain) IBOutlet PLView *plView;

@end

The .m file as follows:

#import "HelloPanoramaViewController.h"

@implementation HelloPanoramaViewController

@synthesize plView;

- (void)viewDidLoad 
{
    [super viewDidLoad];
    // Do stuff here...
}

- (void)dealloc 
{
    [plView release];
    [super dealloc];
}

@end

And then I should use a nib to let "plView variable pointing to the view". But without using Interface Builder, how would I do this programmatically? How could I let this UIViewController create an PLView, instead of an UIView?

2
Just FYI, in case you weren't aware, you can actually do this IB as well, by changing the 'Class' property of your main UIView in your nib to be the desired classname.occulus

2 Answers

3
votes

your UIViewController will something that looks like

#import "HelloPanoramaViewController.h"

@implementation HelloPanoramaViewController

- (void)loadView 
{
    self.view = [PLView plview]//or whatever it takes to create the plview.
}

- (void)viewDidLoad
{
    //create more objects
}

- (void)viewDidUnload
{
    //release unwanted objects that were created viewDidLoad
}

-(void) dealloc
{
    // release all 
    [super dealloc];
}
@end

more info... here

0
votes

In the place where you create your viewController, also create an instance of your custom view, and then set that view to your controller's view:

HelloPanoramaViewController *controller = [[HelloPanoramaViewController alloc] init];
PLView *view = [[PLView alloc] init];
controller.view = view;