0

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 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.

1 Answer 1

3

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] 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help! I haven't done the Singleton suggestion yet, but that's exactly what I'm looking for. What I'm getting now is that it loads its own .xib, so I can just call toast.view, but I still have to type [self.view addSubview:toast.view]; What exactly are the steps I need to take to have the toastView class load its view as a subview of a different class? It seems like I can't do it unless I call [self.view addSubview:] from that class?
If I understand your question correctly, the loadToastInView: method in the Toast class should handle that. It would look like this: - (void) loadToastInView: (UIView *) view { [view addSubview:toastView]; }
that seems really simple now. Thanks so much!
The singleton idea was amazing. I should have thought of that! It works like a charm now and I don't have to do basically anything to implement my toast in the future! Thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.