IOS - How to create a component that is present in all views?

Suppose an iOS app displays an item that, when clicked, brings up an action menu for that item (for example, UIActionSheet). A trap consists of many elements that can be clicked, such as this one, can be present in every view of the application, so the action menu and its delegates must also be processed in any situation.

For example, an item that can be clicked can be a photograph of a product (UIButton with an image background) that can be displayed on many different screens, and when you click on a user, options such as Buy, Details, etc. .d.

How would you like to design / implement this kind of component with its associated behavior, in a modular and reusable way?

It is important to keep the function in a stand-alone set of classes that can be easily tested separately and turned on / off when necessary, and you do not need to insert code into each controller where this function can be used.

Thanks!

+4
source share
2 answers

How to create a single class that represents the function you want? Then you can just call the singleton object every time.

@interface MyClickableButtonWithImage : NSObject { UIButton *aButton; } +(MyClickableButtonWithImage)getInstance; -(IBAction)buttonClicked:(id)sender; @property (nonatomic, strong) IBOutlet UIButton *aButton; @end // In your .m files, this will get the singleton object MyClickableButtonWithImage *myCBWI = [MyClickableButtonWithImage getInstance]; 
0
source

If you embed a user interface element in an application that can appear in more than one view, I would subclass your UIViewController (i.e., ABCViewController). I usually do this at the beginning of each project, just in case I need to implement something in the application, and all my views are a subclass of this view controller with no code changes, except for one word in the header file. Then execute the function in your ABCViewController to display the menu and the function to reject it. Compared to the Singleton class, it’s easier for me to manage object memory and callback in ViewController without having to deal with delegates.

+1
source

Source: https://habr.com/ru/post/1441600/


All Articles