appcodix
appcodix

Reputation: 342

Using an UIView from one UIViewController in another UIViewController?

as you can read in the title, I want to display an UIView from one UIViewController in another UIViewController. The reason for this is, that I want to design the UIView in an extra UIViewController in the storyboard, but use it as an overlay view in my MainViewController on which the app mainly runs.

I tried the following: In the MainViewController I created an instance of the second UIViewController and saved its view in the UIView i called overlayView.

UIViewController* rvc = [self.storyboard instantiateViewControllerWithIdentifier:@"overlayView"];
self.overlayView = rvc.view;

After that step, I thought I simply add it as a Subview, but sadly it didn't work.

[self.view addSubview:self.overlayView];

Is there anything missing or should I try something completely different ?

Thanks in advance :)

EDIT: Now theres the problem, that an Error called BAD_ACCESS occurs by pressing a button of the overlayView.

I created a ViewController, whose views alpha I regulated to a value of 0.9. On this view I added a simple button up to now. In my MainViewControllers viewDidLoad Method I used the following code to initialize the "new" ViewControllers view:

UIStoryboard* st = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    UIViewController* vc = [st instantiateViewControllerWithIdentifier:@"overlayView"];
    self.overlayView = vc.view;
    [self addChildViewController:vc];
    self.overlayView.alpha = 0.0;

As a next step I "called" the overlayView to appear by pressing a button on the MainViewController and animated the alpha for a smooth fadeIn:

[self.view addSubview:self.overlayView];

[UIView animateWithDuration:1.3 animations:^{
    self.overlayView.alpha = 0.9;
}];

Now if I press the button on the overlayView I want it to call a method of the MainViewController, which is doing some stuff and lets the overlayView disappear. For this case I used a NSNotificationCenter. But by pressing this button (on the overlayView), the following error occurs in main.m:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x128a60cd0)

First I thought, the problem was caused by the NSNotificationCenter. So I tried to call the method using Delegation. But it didn't help.

Delegation Code: OverlayViewController.h

@class OverlayViewController;

@protocol OverlayViewControllerDelegate
- (void) setToNormalAction;
@end

@interface OverlayViewController : UIViewController

@property (weak, nonatomic) id <OverlayViewControllerDelegate> delegate;
@property (strong, nonatomic) IBOutlet UIView *overlayView;

- (IBAction)buttonTouchUpInside:(id)sender;

@end

Button Method:

- (IBAction)buttonTouchUpInside:(id)sender
{
    [self.delegate setToNormalAction];
}

In the MainViewController.h I implemented the created "OverlayViewControllerDelegate" as known and in .m I used the declared method to fadeOut the overlayView and do some other stuff.

Do you have an idea to resolve this ?

Thanks in advance

Upvotes: 1

Views: 1316

Answers (2)

Scrungepipes
Scrungepipes

Reputation: 37590

You could just design it as a view in a nib then there's no need for an extra VC just to have somewhere to put it, and all the hassles that is going to bring.

There's nothing that says you can't continue to use nibs in addition to a having a storyboard.

Upvotes: 1

CrimsonChris
CrimsonChris

Reputation: 4651

Try...

UIViewController* rvc = [self.storyboardinstantiateViewControllerWithIdentifier:@"overlayView"];
self.overlayView = rvc.view;
[self addChildViewController:rvc];
[self.view addSubview:self.overlayView];

Upvotes: 3

Related Questions