Custom UIView XIB Add On UIViewController

ViewController.h

#import <UIKit/UIKit.h>

#import “EPPZDecoupledView.h”

@interface ViewController : UIViewController<EPPZDecoupledViewDelegate>

-(IBAction)showDecoupledView;

@end

ViewController.m

#import “ViewController.h”

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    

    /*

    // Instantiate the nib content without any reference to it.

    NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@”View” owner:nil options:nil];

    // Find the view among nib contents (not too hard assuming there is only one view in it).

    UIView *plainView = [nibContents lastObject];

    // Some hardcoded layout.

//    CGSize padding = (CGSize){ 220.0, 220.0 };

//    plainView.frame = (CGRect){padding.width, padding.height, plainView.frame.size};

    plainView.frame = CGRectMake(0, 0, 100, 100);

    // Add to the view hierarchy (thus retain).

    [self.view addSubview:plainView];

    

//    plainView.hidden = YES;

    */

    

    // Do any additional setup after loading the view, typically from a nib.

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(IBAction)showDecoupledView

{

    [EPPZDecoupledView presentInViewController:self];

}

-(void)decoupledViewTouchedUp:(EPPZDecoupledView*) decoupledView

{ /* Whatever feature. */ }

-(void)decoupledViewDidDismiss:(EPPZDecoupledView*) decoupledView

{ /* Acknowledge sadly. */ }

@end

EPPZDecoupledView.h

#import <UIKit/UIKit.h>

@class EPPZDecoupledView;

@protocol EPPZDecoupledViewDelegate

-(void)decoupledViewTouchedUp:(EPPZDecoupledView*) decoupledView;

-(void)decoupledViewDidDismiss:(EPPZDecoupledView*) decoupledView;

@end

@interface EPPZDecoupledView : UIView

// Indicate that this view should be presented only controllers those implements the delegate methods.

+(void)presentInViewController:(UIViewController<EPPZDecoupledViewDelegate>*) viewController;

-(IBAction)viewTouchedUp;

-(IBAction)dismiss;

@end

EPPZDecoupledView.m

#import “EPPZDecoupledView.h”

@interface EPPZDecoupledViewOwner : NSObject

@property (nonatomic, weak) IBOutlet EPPZDecoupledView *decoupledView;

@end

@implementation EPPZDecoupledViewOwner

@end

@interface EPPZDecoupledView ()

@property (nonatomic, weak) UIViewController <EPPZDecoupledViewDelegate> *delegateViewController;

@end

@implementation EPPZDecoupledView

+(void)presentInViewController:(UIViewController<EPPZDecoupledViewDelegate>*) viewController

{

    // Instantiating encapsulated here.

    EPPZDecoupledViewOwner *owner = [EPPZDecoupledViewOwner new];

    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:owner options:nil];

    

    // Pass in a reference of the viewController.

    owner.decoupledView.delegateViewController = viewController;

    

    // Add (thus retain).

    [viewController.view addSubview:owner.decoupledView];

}

-(IBAction)viewTouchedUp

{

    // Forward to delegate.

    [self.delegateViewController decoupledViewTouchedUp:self];

}

-(IBAction)dismiss

{

    [self removeFromSuperview];

    

    // Forward to delegate.

    [self.delegateViewController decoupledViewDidDismiss:self];

}

@end

Download Sample Project From Github

Leave a comment