Transition between 2 storyboards - iphone

I have 2 storyboard files in my app and I'd like to transition between a ViewController in one to a ViewController in the other. I've hooked up an IBAction in response to a button press on the first ViewController, which calls a method in the AppDelegate. I have verified that this signal reaches the AppDelegate method.
Here is the relevant method I have in the AppDelegate, however, no transition occurs. Can anyone tell me why, or is it a silly idea to have 2 storyboards?
-(void) presentSecondViewController {
UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController* mainViewController = [mainStoryboard instantiateViewControllerWithIdentifier:#"main_viewcontroller"];
UIStoryboard* secondStoryboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
UIViewController* secondViewController = [secondStoryboard instantiateViewControllerWithIdentifier:#"second_viewcontroller"];
[mainViewController presentViewController: secondViewController animated:YES completion: NULL];
}

You create a second instance of the initial view controller of the first storyboard. That instance was never shown on the screen as it is different from the one being already shown and thus probably won't show your second view controller. You need the instance of the view controller already being shown. The best way would be to change your implementation to
-(void) presentSecondViewControllerFromViewController:(UIViewController *)sourceController
{
UIStoryboard* secondStoryboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
UIViewController* secondViewController = [secondStoryboard instantiateViewControllerWithIdentifier:#"second_viewcontroller"];
[sourceController presentViewController: secondViewController animated:YES completion: NULL];
}
and call it by passing the view controller that contains the button.

Related

present a UIviewcontroller programmatically from UINavigationController with Storyboard

My question: How can I switch between viewcontrollers with 1 rootNavigationController in code, while maintaining customization I set in Storyboard and data that I load up in my viewcontrollers?
Currently I am implementing REMenu, which provides a simple dropdown tableview to change views with. When one of those cells is pressed in the dropdown, I want to switch my view. For example, if I press "Home", I want to go to my MasterViewController The method to switch views is called from within rootnavigationcontroller.m, and looks like the following:
REMenuItem *homeItem = [[REMenuItem alloc] initWithTitle:#"Home"
subtitle:#"Return to Home Screen"
image:[UIImage imageNamed:#"Icon_Home"]
highlightedImage:nil
action:^(REMenuItem *item) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UINavigationController *vc = [sb instantiateViewControllerWithIdentifier:#"myNewTableView"];
// vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:vc animated:NO completion:nil];
}];
Clearly, however, something is wrong with this implementation, because it ignores the UI Customization I made in Storyboard, as well as a datacontroller I invoke in MasterViewController.m Any help on this matter would be greatly appreciated!

How to get an instance of my view controller from the app delegate, storyboards, iOS6

Trying to "instantiate" my initial view controller from the app delegate. I trying to populate an NSMutableArray from the app delegate. A property of the view controller "myMutabelArray" gets an array that is created within the app delegate. With the code below the array is uneffected, even though it's count is 4 (has four objects), as created in the app delegate.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle: nil];
ViewController *controller = (ViewController*)[mainStoryboard instantiateInitialViewController];
controller.myMutableArray = mutableArrayCreatedInAppDelegate;
When I log the count from within the AppDelegate I get 4.
When I log the count from within the ViewController I get 0.
I also tried the following which makes me suspect that I am not getting a pointer to the view controller as needed.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle: nil];
ViewController *controller = (ViewController*)[mainStoryboard instantiateInitialViewController];
[controller.view setBackgroundColor:[UIColor lightGrayColor]];
Try the following:
ViewController *controller = (ViewController*)self.window.rootViewController;
It will return the initial view controller of the main storyboard.

Issue with popViewController and pushController

I'm facing issue with redirecting the viewControllers using pop/pushViewControllers. I've three view controller like below image -
In my 3rd view i've some ToggleButton based on that toggleButton the data which is in 2nd View will change. After, changing something on 3rd view and press Done button in my 3rd View i just started my 2nd ViewController using pushViewController And, the changes are occured successfully.
But, what was the issue is, when i try to press the back button on my 2nd view page its redirecting me again to 3rd viewController (Because of pushViewController)
I try to start my 2nd view using popViewController but, its giving exception.
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.
I've used below code -
SecondViewController *second = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
second.array = resultArray;
second.indexValue = ind;
[self.navigationController popToViewController:second animated:YES];
How do i solve this issue? Any idea?
From 3rd viewcontroller you just need to call this method to pop to 2nd viewcontroller :
[self.navigationController popViewControllerAnimated:YES];
EDIT :
For notifying the second vc for data change you can use delegate. Follow this :
On ThirdViewController.h
#protocol DataChangeProtocol;
#interface ThirdViewController : UIViewController
#property(nonatomic,assign) id<DataChangeProtocol> delegate;
#end
#protocol DataChangeProtocol
- (void)thirdViewcontroller:(ThirdViewController*)vc dataChangedto:(NSDictionary *)changedData;
#end
And before pushing the thridVC from secondVC assign secondVC as the delegate of thirdVC :
ThirdViewController *thirdVC = [[ThirdViewController alloc]init..];
thirdVC.delegate = self;
[self.navigationController pushViewController:thirdVC animated:YES];
and in SecondViewController.m
- (void)thirdViewcontroller:(ThirdViewController*)vc dataChangedto:(NSDictionary *)changedData {
// Change your values in the UI using the passed value from changedData.
}
You create our viewcontroller:
SecondViewController *second = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
and then you want to pop it from your stack but this controller is not in the stack, bacause you create it in the third viewController.
//it used in first class
SecondViewController *second = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
second.array = resultArray;
second.indexValue = ind;
[self.navigationController pushViewController:second animated:YES];
//it move to second class
//it used in second class
[self.navigationController popViewControllerAnimated:YES];
//it move to firsr class
If you want to go to your current previous view controller then you have to do this:
[self.navigationController popViewControllerAnimated:YES];

From One Storyboard to another

For Example, I have 2 Storybards. In the first Storyboard I have a View with a button and when you press the button the second storyboard should appear.
How can I do that?
Take a look at the docs for the storyboard class
You can create a storyboard using
UIStoryBoard *storyboard = [UIStoryboard storyboardWithName:#"secondStoryboard" bundle:nil];
and get view controllers from it like
UIViewController *initialViewController = [storyboard instantiateInitialViewController];
or
UIViewController *otherViewcontroller = [storyboard instantiateViewControllerWithIdentifier:#"otherController"];
Once you've got your view controllers you can just push them onto your navigation controller I guess.
However, I don't know what will happen with using two storyboard objects in the same view hierachy - it's probably fine but you never know :)
Even though your question is a bit confusing, I think I know what you are trying to do.
You want to use two storyboards. UIStoryboard has a method to retrieve an instance of any storyboard with a given name. So first, set a name for your storyboards and view controllers in Xcode and then load them up, and then from within any view controller:
UIStoryboard *anotherStoryboard = [UIStoryBoard storyboardWithName:#"SomeStoryboardName" bundle:nil];
Afterwards, instantiate the desired UIViewController from any storyboard:
UIViewController *anotherViewController = [anotherStoryboard instantiateViewControllerWithIdentifier:#"SomeViewControllerName"];
You could then push it into your navigation stack, for instance:
[self.navigationController pushViewController:anotherViewController animated:YES];
Modifided to be generic here's the way I do this in my app...
UIStoryboard *alternateStoryboard;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
alternateStoryboard = [UIStoryboard storyboardWithName:#"Alternate_iPad" bundle:nil];
} else {
alternateStoryboard = [UIStoryboard storyboardWithName:#"Alternate_iPhone" bundle:nil];
}
AlternateController *altController = [alternateStoryboard instantiateInitialViewController];
[altController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:altController animated:YES];
If your app is only iPhone or iPad that can be reduced to...
UIStoryboard *alternateStoryboard = [UIStoryboard storyboardWithName:#"Alternate" bundle:nil];
AlternateController *altController = [alternateStoryboard instantiateInitialViewController];
[altController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:altController animated:YES];
You'll probably want to change the last 2 lines to suit the presentation style that you want.

View won't switch properly

I'm trying to switch views in an iPhone application, but whenever I click on the button to bring up the second view, all I get is a blank screen, even though I've filled the view with buttons and whatnot.
I've checked the second ViewController in my storyboard file and its custom class is the one that it needs to be (When I originally created the second ViewController class a new interface file came with it, which I promptly deleted). What might I be doing wrong?
- (IBAction)Transition_NEXT:(id)sender
{
nextViewController = [[NextViewController alloc]
initWithNibName:#"NextViewController"
bundle:[NSBundle mainBundle]];
[self presentModalViewController:nextViewController animated:NO];
}
I don't think you're using Storyboards correctly. You shouldn't be using initWithNibName, as that's the old method of instantiating a view controller. Try using the new instantiateViewControllerWithIdentifier like so:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *nextView = [storyboard instantiateViewControllerWithIdentifier:#"NextViewController"];
[self.view presentModalViewController:nextView animated:YES];
If you already have a reference to your storyboard in your controller, then you shouldn't initialize a new one, but instead you should just use the reference you have.
UIViewController *nextView = [self.storyboard instantiateViewControllerWithIdentifier:#"NextViewController"];
[self.view presentModalViewController:nextView animated:YES];