Is willMoveToSuperview called (with nil) when removing a view from a hierarchy? - ios

If you implement willMoveToSuperview on a UIView subclass, is it guaranteed to be called (with nil) when removeFromSuperview is called on your view?
The docs say that the newSuperview parameter may be nil and that it's called "whenever the superview changes" but I'm not sure if I can interpret this to mean it will be called when the view is removed from its superview even when not being moved to a different superview.

Whenever a view receives removeFromSuperview, and the view's superview was not already nil, the view will always do [self willMoveToSuperview:nil].

Related

UIViewController subviews existance invariant

At what point in the UIViewController lifecycle is the subview property of self.view guaranteed to be populated with all the correct views? Note: I don't care about if they're laid out or not, just that they exist in the subview array. WWDC videos say that loadView, viewDidLoad, and init all don't come with that guarantee but viewWillLayoutSubviews is also late in the game.
The task I'm trying to perform in this instance is localization. In a common view controller class, I want to loop through all the subviews, see if they have a custom attribute set that identifies what localized string key is attached to that view, and then recurse through all that views subviews until the bottom of the view hierarchy is reached. Layout isn't important in this instance, just that the subviews are populated in the view controller.
At what point in the UIViewController lifecycle is the subview property of self.view guaranteed to be populated with all the correct views
The earliest point implemented by most apps is viewDidLoad. At that point you are guaranteed that self.view exists along with all the subviews from the storyboard, and that any outlets hooked up to this view controller from the storyboard have been populated.
I don't care about if they're laid out or not, just that they exist in the subview array
Exactly so. self.view and its nib-loaded subviews exist at this point, but their layout has not yet taken place and their frame is not necessarily correct. You don't care, so viewDidLoad is fine for your purposes.

How to have the same subView in different ViewControllers in iOS?

I have 1 subView that should be visible in two different View Controllers in my app (it is a main button in my app with a badge that shows new messages and other relevant information to the user).
What is the best way to achieve this?
Do I have to duplicate the subView in both View Controllers?
UIView's addSubview
addSubview:
Adds a view to the end of the receiver’s list of subviews.
(void)addSubview:(UIView *)view
Parameters
view
The view to be added. After being added, this view appears on top of any other subviews.
Discussion
This method establishes a strong reference to view and sets its next responder to the receiver, which is its new superview.
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.
So don't try to do that .
You should simply have two instances of the view that look the same and have them in the same location.
You might want to have central creation method (that also updates all buttons) and target for this button.
Create Your SubView`s CustomView And respectively add the view to your view controllers ,then save the information with NSUserdefualts in one viewController and get it from another ViewController.

iOS: destroy subviews in viewDidDisappear?

I have a UINavigationController with a UIViewController pushed onto it. In my viewControllers view, I have two subview UIScrollViews. I recreate these scrollviews every time in the viewWillAppear method in my viewController and add them as subviews to my viewcontroller's view. I am using ARC, should I be destroying the two scroll views in viewDidDisappear method? What is better practice for memory usage.
EDIT: I did some investigation, and without removing the scroll views and setting them to nil in the viewDidDisappear method, they are not released. The viewcontroller's view gets an increasing number of subviews as viewWillAppear is called. As I mentioned in the comments, I never deallocate my main viewcontroller. It always stays on the navigation controller's stack. Why must I manually release the scroll views?
It may not be necessary to do anything at all. If your UIViewController is removed from the screen and is then being deallocated, it will automatically deallocate its view hierarchy (including subviews you have added). An easy way to check for this is to override the dealloc methods of the classes you're interested in (using an ObjC Category method added to the class), and log a message to indicate they've been removed.

ios what is opposite of layoutSubviews

I have a view that exits in its own class, with its own xib.
This view is initialized and added as a subview to my viewController view.
When the view is initialized, the method layoutSubviews is called where i customize some stuff in the view.
BUT which method is called when the view is removed from the superview (if any)?
For example, for a ViewController, viewWill/DidDisappear is called. Is there a similar method to a UIView (opposite to the layoutSubviews)?
Thanks in advance
---EDIT---
I just found a method that is called both on adding and removing a subView:
- (void)willMoveToSuperview:(UIView *)newSuperview
AND if newSuperview == 0, you can customize the removing of the subview.
Am i right or is it a tacky way to handle the situation?
BUT which method is called when the view is removed from the superview (if any)?
-removeFromSuperview is called, so you can override that if you need to do some housekeeping when the view is removed. Just remember to call super's version, too.
-layoutSubviews isn't necessarily only called when the view is added to a superview -- it's called whenever layout is needed. For example, it might be called when the orientation changes, or when the superview lays itself out again, or when the view's frame changes. There's really not an inverse of -layoutSubviews because none is needed. (What would it be called? -messupSubviews? ;-))

How can a UIView subclass get notified when it has been added as subview to another view?

I need some kind of hook or template method to override which gets called when a view is added as subview to another view, but couldn't find it in the documentation. It must be something that gets called automatically by UIKit. The reason is that my view must start some animations as soon as there is a superview, but stop animating as soon as there is no superview anymore.
I can't override -setSuperview: as Xcode is not indicating that such class exists - and I can't call super.
You want to override - (void)didMoveToSuperview.
didMoveToSuperview
Tells the view that its superview changed.
The default implementation of this method does nothing. Subclasses can override it to perform
additional actions whenever the superview changes.

Resources