工厂模式
1.
2.#import <UIKit/UIKit.h>
3.
4.@interface LXYUIFactory : NSObject
5.
6.
7.+ (UIWindow *)createWindow;
8.
9.
10.+ (UIViewController *)createViewController:(NSString *)controllerClassName;
11.
12.
13.+ (UIViewController *)createViewController:(NSString *)controllerClassName
14. withBackgroundColor:(UIColor *)bgColor;
15.
16.@end
17.
1.#import "LXYUIFactory.h"
2.
3.
4.@implementation LXYUIFactory
5.
6.+ (UIWindow *)createWindow {
7. static UIWindow *window = nil;
8. if (!window) {
9. window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
10. window.backgroundColor = [UIColor whiteColor];
11. }
12. return window;
13.}
14.
15.+ (UIViewController *)createViewController:(NSString *)controllerClassName {
16.
17. Class cls = NSClassFromString(controllerClassName);
18.
19. return cls ? [[cls alloc] init] : nil;
20.}
21.
22.+ (UIViewController *)createViewController:(NSString *)controllerClassName
23. withBackgroundColor:(UIColor *)bgColor {
24.
25. UIViewController *controller =
26. [self createViewController:controllerClassName];
27. if (controller) {
28. controller.view.backgroundColor = bgColor;
29. }
30. return controller;
31.}
32.
33.@end
34.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步