Get instance of view controller in AppDelegate.m - ios

In my app I've three view controller embedded in a UITabBarController. The second view controller embed a table view controller. Now I've this bug: I go to the second view controller, then I press home button and the app goes in background. When the app is closed I receive a push notification, so when I open the app it shows me the second view controller with table view without any changes. How I can update the table view automatically? For now I'm using the UIRefreshControl, but I will do it automatically, how I can do that?

You can get your VC's instance from storyboard. First you have to set storyboard ID to your VC
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
YourViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:#"YourViewControllerID"]; // here YourViewControllerID is storyboard Id of your VC.

Related

Xcode 6 - Added Navigation Controller to storyboard, but not appearing in app

I added a Navigation Controller to my storyboard and it appears like so:
Now in the table view controller, I gave the TableViewController a storyboard id and class to a TableViewController Controller
When I run my app, I don't see the Navigation Bar at the top. This has been extremely frustrating and can't find a solution anywhere. PLEASE HELP
To get to the scene, someone clicks a button and this code runs and it goes to my Table View Controller:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
LHFileBrowser *LHFileBrowser = [storyBoard instantiateViewControllerWithIdentifier:#"FileBrowser"];
[self.navigationController pushViewController:LHFileBrowser animated:YES];
[self presentViewController:LHFileBrowser animated:YES completion:nil];
The error is in your code.
If you want to (modally) present a view controller when the user presses a button, you need to present the navigation controller (which will contain the table view controller), not the table view controller itself.
Right now, you're presenting the view controller, which won't show it being embedded in a navigation controller.
Also, you're mixing up two different approaches, by trying to push a view controller onto a navigation controller stack, and also presenting the view controller.
Code Sample:
Here's what you apparently mean to do:
UIStoryboard *storyboard = self.storyboard;
UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:#"MyNavigationControllerID"];
LHFileBrowser *rootViewController = [navigationController topViewController];
// Configure your LHFileBrowser view controller here.
rootViewController.someProperty = ...;
// Modally present the embedded view controller
[self presentViewController:navigationController animated:YES completion:nil];
If you want to change the presentation or transition style, you can set those details in your storyboard.
You didn't explain why you had to programmatically add buttons, but Storyboard segues would have instantiated and presented an embedded view controller for you, without you having to have done it in code.
The more you can do in Storyboard, the less code you have to maintain, support, and update, and the more likely your app will still work properly when a new SDK is released.
Update:
The better way to do this is to let Storyboard do it for you, by adding a segue from the button to the navigation controller that you want to present.

Jump to Uiviewcontroller on handling push in storyboard

I am handling push on alert to jump/open a view in story board like this from delegate
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:#"Main_iphone4" bundle:nil];
ChatViewController *scvc = (ChatViewController *)[storyboard instantiateViewControllerWithIdentifier:#"ChatViewController"];
[self.window.rootViewController presentViewController:scvc animated:YES completion:nil];//not working show nothing while it works to jump using simple xib views.
and if i use
self.window.rootViewController =scvc;//it works but i need to get back on back button which causes it to get back in blank screen.
How to jump to a view controller using storyboard on handling push from delegate.
NOTE: The jump view have a back button,which need to dismiss the view too.
NOTE APP IS NOT NAVIGATION BASED

application:openURL:sourceApplication:annotation: and pushing view controlle.r

My app is is designed to handle the PDF files. Whenever user opens a PDF file I would like to redirect the him/her to appropriate ViewController.
I tried several different approaches and I noticed interesting behavior.
Whenever I reference the navigation controller using window everything works fine:
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
[navigationController pushViewController:_importer animated:YES];
But when I use storyboard
UIStoryboard *st = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UINavigationController * nav =(UINavigationController *)[ st instantiateInitialViewController ];
[nav pushViewController:_importer animated:YES];
It doesn't work. Why?
instantiateInitialViewController creates a new instance of the initial view controller of your storyboard. At that point, its view is not part of the view hierarchy. The pushing works just fine, I'm sure, but since the navigation controller isn't on screen, you won't see the new view controller get pushed.

Navigating to a view controller modally and then navigation using push segue

I have 2 storyboards. I have a situation where I have to navigate from a view controller1 in storyboard 1 to a view controller2 in storyboard 2. The view controller 1 is added as a subview to a parent view controller. When I click on a row in a table view in view controller 1, I have to navigate to storyboard 2.
Initially it was not detecting a touch in the table row. I managed handling it by using a single tap gesture. Now when I click on a row, I am able to navigate to storyboard2 viewcontroller 2 modally. But after navigating ,I am not able to move anywhere on a button click in view controller 2 because it uses all push segues there.
Here is the sample code which I used to navigate.
*NSString * storyboardName = #"OppotunityStoryboard";
NSString * viewControllerID = #"AgreementMainViewController";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
AgreementMainViewController *controller = (AgreementMainViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
controller.productTransactionentity = productTransactionEntity;
[self presentViewController:controller animated:YES completion:nil];*
You have to embed ViewController 2 in a navigation controller in order for it to be able to use push segues when presented modally.

Switching views without initialization in iOS

There are two views in my application.
After launching the app I switch the view with button like this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:[NSBundle mainBundle]];
UIViewController *view = [storyboard instantiateViewControllerWithIdentifier:#"view_a"];
[self presentViewController:view_a animated:NO completion:nil];
But whenever i switch the view the code above initialize the view.
I want to maintain previous status of the view.
How can I solve this problem?
instantiateViewControllerWithIdentifier: always returns a new instance of an UIViewController.
You need to keep a reference to a previous one if you don't want to create it over and over.
On an iPad this will present the second view modally, as dictated by the views modalTransitionStyle. So there you could get back to the original by calling dismissViewControllerAnimated:completion: on the new ViewController.
On the iPhone you can use a UINavigationController in your storyboard to push and then pop the secondViewController.
As long as you are using the storyboard, you can set up the transition there and the perform it using - performSegueWithIdentifier:sender: from your button. Or for that matter you can connect the segue directly to your button in which case the transition will be performed without additional code.

Resources