Setting up UIViewController before pushing/presenting it - ios

In my app, I use a UINavigationController to switch between many different UIViewControllers. The only problem is that with one of my views, upon it loading, I have to do lots of customization (both data access and graphics, both of which must be done in code). When the view controller is pushed, the animation is extremely choppy, because the phone is forced to both animate the transition and set up the view at the same time.
Setting up the view after it is loaded is not an option, so is it possible to set it up (there is a method called setupViewDidLoad that has all of the necessary code in it) during allocation before it is pushed/presented?
Edit:
Let me modify my question slightly. I have found that if I present the view controller using [self presentModalViewController:animated:] instead of pushing it with the navigation controller, there is no choppiness. What is the reason for this?

Your best bet is to do this. Set up any code you need in viewWillAppear in your views .m file. If you have to access data remotely you may want to think about creating a data storage class of its own to handle and store that so it can do it whenever - even if the view isn't loaded or hasn't been loaded yet.

Related

Using the master .h / .m for a secondary Popup view - Xcode 5

So I have a "main" View linked with my main .h/.m ViewController files.
In a process of the application, a second View is called that overlays part of my main View in a "popup" style.
I'm wondering if it's a good idea to use the original main ViewController .h/.m files for both the main View, and the second View? If not, I'm genuinely interested in why that is not to be considered a good idea. If this is in-fact a standard practice, what is the cleanest way to do so?
It's typically good practice for your view controller to "control" what comes on and off the displayed view its responsible for. If you are just presenting something very simple, it would be fine leaving it (the code) in your view controller - an example of this might be a background view with a different color.
If however, your view has many responsibilities, such as responding to touch events, you may want to create a subclass of UIView and put your code in that file. Then back in your original view controller, you would simply import that subclass and instantiate the view and present it (add subview) when needed.
Using a common pattern like delegation, your subclassed UIView would control it's own logic and perhaps via delegation, send messages back to the view controller when needed for items like dismiss, or save or any number of functions it might do. A common example of this is UITableView.
hope that helps

UIViewController - where to put init of instance variables (using Storyboards)

I'm using Storyboards. I need to init instance variables of UIViewController just once. viewDidLoad and awakeFromNib fire each time I open a viewController from menu (I'm using SWRevealViewController for sidebar menu as in Facebook app). Is it normal for awakeFromNib to fire many times or is it SWRevealViewController bug?
So is there some special init method or do I need to set instance variables from outside of viewController (in AppDelegate?)
EDIT:
I found this question
SWRevealViewControllerSegue, reusing Viewcontrollers
I didn't realize that each time you show a controller via segue it creates a new instance of controller. So what does it mean? Does it mean that if I have a table of data loaded from web API - it will be reloaded each time I go back and forth through segue?.. Doesn't seem very effective to me. Is it normal for iOS?
View controllers are either recreated or reused depending on the situation. When you push a view controller onto a navigation controller's stack, you almost always push a new instance. When using a tab bar controller, the view controllers it contains are often held in memory. To avoid excessive memory consumption, it makes sense that view controllers are cleaned up when possible, rather than held around.
In the case of your SWRevealViewController then yes, it sounds as though your view controller should be recreated every time. You ideally shouldn't be directly loading any data from a web API within your view controller; instead, extract your API calls and models into separate classes that you can reuse from any view controller. Then, rather than reloading the data over the network when your view controller loads, you just need to fetch the latest data from your model / store class.

The right way to present view controller with custom transition/animation in iOS<7

I want to present view controllers as simple as with native presentViewController:animated:completion:, dismissViewControllerAnimated:completion: methods, but use custom animations for this.
The common way to do this is to perform custom animation (using view screenshots instead of view itself) and call presentViewController:vc animated:NO completion:… after.
But in this case view controller life-cycle messages are sent not in time. viewWillApper: and viewDidAppear: sent together instead of normal way: the first one just before animation and the last — after. Also they sent with animated parameter set to NO.
The other bad thing is that screenshot of a view (for animation) captured before viewWillApper: called. So it can be some outdated, and this will cause flicking at the end of animation.
I've searched lot of related projects on github/cocoapods (SO answers too), but did not find any with the right life-cycle of presented view controller. There are sometimes even worse solutions like simple views changing without any calls to life-cycle methods.
Possible solutions I know:
iOS7 — I can't, I need it in iOS6 too
Use transition between contained view controllers — Not as simple and reusable as "modal presenting"
Use tricky animations like here — my animation can't be implemented in similar way
While writing this question I thought about custom segue with overridden perform: method. But looks like it's not easy too. At least I don't know how to use it without storyboard and how to do dismiss in easy way.
Do you know how to present view controller with custom animation in iOS6 and make it's life-cycle happy?
Update: finally I've decided to use container view controller. It has ability to customize transitions of it's children and preserve VC's life-cycle. The bad side: it's not as easy as custom transitions in iOS7. It needs container, making xib some harder. And needs additional code to support.

How to instantiate a particular view controller with storyboard in iOS at early stage of loading?

When using tabs with storyboard in iOS 5, some of them may take quite a long time to initialize when switching to it (for example, a tab containing GLKViewController).
This happens because an amount of work in viewDidLoad method in this controller could be very big.
Is there a way to initialize particular view controller (and call it's viewDidLoad method) defined in the storyboard at early stage - when an application starts? Having done this, the delay should be eliminated.
Are you sure it's the view controller's instantiation and not the viewDidLoad method? The view controllers are probably all created when the storyboard is unpacked, but a view controller tries to delay loading its actual view object as long as possible; viewDidLoad isn't called until the view property of your UIViewController subclass is accessed.
So a way around this could be to manually access the view property:
__unused CGRect frame = [[tabBarController.viewControllers objectAtIndex:index] view].frame;
If the slowdown is, in fact, in the instantiation and the view controller isn't being created until you switch to that tab, then you'll have do force the view controller to be instantiated by accessing it programmatically, like in the above example.
Calling the frame of the vewcontroller or the .view property will most likely work,
but i dont advice you to mess up with the viewcontroller initializations and view settings
For the following reasons
changes you make will not be standard, they will be tricks and hacks that will later on get out of hand
changes that you make will not be carried with you easily to other projects you create
If i faced a problem like this i would create the GLKViewController separately for example in the app delegate and held it there, untill the viewDidLoad gets called in the viewController, then i would move this initilized GLKViewController to the viewController

the Benifits of awakeFromNib?

I've been learning coredata by making a lot of simple test apps based on the xcode Navigation controller template with "use coredata" checked.
The awakeFromNib method in the App delegate has been a source of problems for me, because I'm adding other views to the controller and changing the load sequence, so that RootViewController may be a second or third choice.
I've figured out what awakeFromNib is doing, and I've removed it so the app delegate is no longer tied to any particular view. (So when I do want to load RootViewController, I'll load it as a regular view, and use its own viewDidLoad to initialize the managedObjectContext for the view).
My question: are there performance gains or other benefits by using awakeFromNIb in the AppDelegate? or is it just another way of doing the same thing as I'm doing from the viewDidLoad method?
All the methods fire at different times and different circumstances.
awakeFromNib is called when the nib file associated with a class is loaded from disk. Any class that can own a nib can use it. viewDidLoad is used only by view controllers. It is usually called when loading from nib as well but it can also be called by a view created in memory (very rare circumstance.)
In either case, you only put functionality in either that you only want to run once when the instance is first loaded. E.g. a common nubie mistake is to put code in viewDidLoad that needs to run every time the view appears. Say as with master view that opens a detail view and then reappears when the detail view is dismissed. If the code for the master view is in viewDidLoad it will only run the first time the master view is loaded but not any of the subsequent times the master view disappears and reappears.
You generally don't initialize any other views or do much of anything in the app delegate's awake from nib. That is usually performed in applicationDidFinishLaunching.

Resources