UIView - How to get notified when the view is loaded?
Depending on what kind of actions you need to perform, there are several techniques:
-(id)initWithFrame:(CGRect)frame
- UIView's designated
initializer; always sent to a UIView to initialize it, unless the
view is loaded from a nib;-(id)initWithCoder:(NSCoder *)coder
- always sent to initialize a UIView whenever the view is loaded from a nib;-(void)awakeFromNib
- sent after all the objects in the nib are initialized and connected; applicable only if you load the object from a nib; you must call super;-(void)willMoveToSuperview:(UIView *)newSuperview
- sent immediately before the view is added as a subview to another view;newSuperview
may be nil when you remove the view from its superview;-(void)willMoveToWindow:(UIWindow *)newWindow
- sent immediately before the view (or its superview) is added to a window;newWindow
may be nil when you remove the view from a window;-(void)didMoveToSuperview
- sent immediately after the view is inserted into a view hierarchy;-(void)didMoveToWindow
- sent immediately after the view gets its window property set. -
Basically, you can choose to perform your actions during initialization (1 & 2), after loading from a nib (3), before insertion into a view hierarchy (4 & 5) and after that (6 & 7).
UIView - How to get notified when the view is loaded?
Depending on what kind of actions you need to perform, there are several techniques:
-(id)initWithFrame:(CGRect)frame
- UIView's designated
initializer; always sent to a UIView to initialize it, unless the
view is loaded from a nib;-(id)initWithCoder:(NSCoder *)coder
- always sent to initialize a UIView whenever the view is loaded from a nib;-(void)awakeFromNib
- sent after all the objects in the nib are initialized and connected; applicable only if you load the object from a nib; you must call super;-(void)willMoveToSuperview:(UIView *)newSuperview
- sent immediately before the view is added as a subview to another view;newSuperview
may be nil when you remove the view from its superview;-(void)willMoveToWindow:(UIWindow *)newWindow
- sent immediately before the view (or its superview) is added to a window;newWindow
may be nil when you remove the view from a window;-(void)didMoveToSuperview
- sent immediately after the view is inserted into a view hierarchy;-(void)didMoveToWindow
- sent immediately after the view gets its window property set. -
Basically, you can choose to perform your actions during initialization (1 & 2), after loading from a nib (3), before insertion into a view hierarchy (4 & 5) and after that (6 & 7).
iphone UIView - Get notified when view has loaded?
There is no delegate method but you can override,
- (void)didAddSubview:(UIView *)subview{
}
Detect when the View has been (re)shown
viewDidAppear
actually happens every time that view "appears", rather than just once on app load. For example, in an application that uses a UITabBarController
, every time you press a tab, the view will switch to a specific view controller and the view controller's viewDidAppear
method will get called.
To detect when the view appears from the background you need to register for notifications for applicationDidBecomeActive
.
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
Exist way notify when custom UIView will be presented or removeFromSuperview()?
I'm not totally sure what you are asking. I think you're asking for a way to tell when your custom view is added as a subview of another view.
The easiest way to do that is to make your view a custom subclass of UIView, and implement didMoveToSuperview()
or willMove(toSuperview:)
. Those methods get called when your view is added as a child view of another view.
If you really want to use the notification center you could have your custom view class broadcast a notification when it gets added to a superview.
Related Topics
Passing Parameters to a Method Called by Nstimer in Swift
Libraries Not Found When Using Cocoapods with iOS Logic Tests
Showing Pushviewcontroller Animation Look Like Presentmodalviewcontroller
Auto-Layout: What Creates Constraints Named Uiview-Encapsulated-Layout-Width & Height
Wrapping Text in a Uitextview Around a Uiimage Without Coretext
Centering Subview's X in Autolayout Throws "Not Prepared for the Constraint"
Urlsession.Datatask with Request Block Not Called in Background
Expand Uilabel Inside Uitableview with "More" Button Like Instagram
Ios: Detect If the Device Is iPhone X Family (Frameless)
How to Render a Uiview with Transparent Background on an Scnplane in Arkit
Moving the Cursor to the Beginning of Uitextfield
Presenting Camera Permission Dialog in iOS 8
Why Push Notifications Is Not Working on Testflight
Hiding Uitabbar When Pushing a Uiview
How to Detect the Orientation of the Device on iOS
Xcode - Mpnowplayinginfocenter Info Is Not Displayed on iOS 8