开发者

How to make an iOS subview add itself to a view

开发者 https://www.devze.com 2023-03-13 19:40 出处:网络
I\'ve created a \"toast\" style activity window class that I can add very simply by creating an instance of the class, then initiating it with the nib \"toastView.xib\" and adding it as a subview to t

I've created a "toast" style activity window class that I can add very simply by creating an instance of the class, then initiating it with the nib "toastView.xib" and adding it as a subview to the current view.

What I want to do is simplify this so that I only have to initiate the instance. Then, to conserve memory, I'd like it to only add 开发者_Go百科the subview when I pop up the toast. Currently, I do this with one of two methods, but for simplicity just assume it is -(void)loadWithLabel:(NSString *)labelString When this happens, I think the current View Controller must pass the toastView the current main view. Based on this view, I would like the toastView class to add itself as a subview, so I have to do less work to implement this toast window in View Controllers further down the line. If you guys have any suggestions that would be most welcome! Sorry about the verbosity, its kind of confusing :)

Thanks in advance!

Summary of the desired behavior:

My current View Controller (call it currentView) allocates and initializes an instance of toastView class (call it 'toast'). When the toast is needed, currentView sends something like [toast loadToastInView:self.view]; after which toast inserts itself into currentView.view at index 0. After a set time (or on method call), toast releases its view from it's superView, currentView until it's called upon again.


I would make a singleton and call it Toast. Then when I needed a toast I would call

[[Toast sharedInstance] loadToastInView:self.view]; 

and

[[Toast sharedInstance] removeToast]; 

The Toast singleton would have a UIView member called toastView which is added/removed when those are called and which is loaded from xib on init with

[[NSBundle mainbundle] loadNibNamed@"toastView" owner:self options:nil]
0

精彩评论

暂无评论...
验证码 换一张
取 消