ajay92x
ajay92x

Reputation: 73

Open Storyboard on top of previous view

I have created storyboard which I want to open on top of another view or in childView so that when I close or destroy this view of storyboard the earlier view on which the storyboard is opened remains the same.

When I run the following code:-

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"neolick"];
[[UIApplication sharedApplication].keyWindow setRootViewController:detailViewController];

First, the storyboard opens but I don't know whether it opens on top of previous view or it destroys the previous view & then open. Second, the functions needed on that storyboard runs automatically which is as I want but how these things are working.

If anyone can help me understand the above code and its working.

NOTE: I cannot call the earlier view again in same state because of some reason.

Thanks in Advance!!

Upvotes: 0

Views: 267

Answers (1)

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16456

Here you are setting root view controller

it will not keep your back screen as it is what you want

If you want to keep current screen and show other screen on that

you have two approaches

1) Present ViewController 2) Push View Controller

1) Present ViewController

for this you can present your screen on top of other screen which is visible

for example

- (IBAction)btnNextTapped:(id)sender {
     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
     ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"neolick"];
     [self presentViewController:detailViewController animated:true completion:nil]

 }

2) Push View Controller

For that you need NavigationController and need to push your ViewController from current visible screen

i.e

[self.navigationController pushViewController:vc animated:true];

EDIT

as per discussion you need to find current top view controller then you should present it

Add this method below your method

- (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

while (topController.presentedViewController) {
    topController = topController.presentedViewController;
}

return topController;
}

And Replace this method with code

- (void)goToNewPage:(CDVInvokedUrlCommand*)command 
    { 
       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil]; 
     ViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"neolick"]; 

      [[self topMostController] presentViewController:detailViewController animated:true completion:nil]; 
       }

Upvotes: 1

Related Questions