iphone - 在 iPhone 应用程序中跨多个屏幕进行通用覆盖的最佳方法?

标签 iphone ios uiviewcontroller

我正在创建一个应用程序,其主屏幕可以包含三个场景之一。我们称它们为 A、B 和 C。

这些场景的功能和用户界面都大不相同。为了便于讨论,假设每个场景都是一种独特的逻辑难题,并且它们之间没有共同的 UI 元素、逻辑或功能。

因此,我希望每个场景都在其自己的 ViewController 中实现。场景将以模态方式显示在“主菜单”上,因此主菜单会调用类似 [self presentModalViewController:ScenarioX animated:YES] 的内容以显示场景。

问题是:每个场景都会有一个特定的 UI,它们在它们之间是通用的 - 例如,屏幕顶部状态栏中的倒计时时钟。那个通用的 UI 将有自己的逻辑,尽管是最小的。显然,它必须运行倒计时时钟,可能会在时钟走完时显示“时间到了”消息,并响应关闭模态 ViewController 并返回主菜单的“后退”按钮。

在代码重用、内存管理等方面实现这一点的最佳方法是什么?

我会欣赏一定程度的技术特异性,而不仅仅是一种“方法”。例如,如果正确的答案是将状态栏/时钟创建为它自己的 UIView 子类,然后以编程方式将其添加到场景的 View 中,那很好:这是否意味着场景的 ViewController 必须运行时钟、后退按钮和等等?

或者是否可以创建一个管理状态栏、时钟等的 StatusClock ViewController - 并将其添加为场景 View 的 subview ?

最佳答案

为此,只要我正确解释了您的问题,您的意思就是您有 3 个共享一些次要通用功能的对象。我要做的是将通用功能抽象为一个基类,然后让其他 3 个类继承自该基类。

所以你有 ScenarioBase,它具有像定时器属性这样的通用功能,一个 startTimer 函数,一个 stopTimer 函数,一个倒计时函数,一个 startingTime 变量,然后可能是一个 completedScenario 的委托(delegate)协议(protocol)(这在你的模态视图 Controller 上调用 dismiss) .

@protocol CompletedScenarioDelegate : NSObject
-(void)scenarioIsComplete;
@end

@interface ScenarioBase : UIViewController <CompletedScenarioDelegate> {
     NSTimer *timer;

     int startingTime;
}

@property (nonatomic, retain) NSTimer *timer;
@property (nonatomic, readwrite) int startingTime;

-(void)startTimer;
-(void)stopTimer;
-(void)countdown;

@end

现在您有 3 个要实现的场景。

类定义可能看起来像

@interface Scenario1 : ScenarioBase {
     UIImageView *puzzleBackground; //I don't really know what you want as members
                                   //Lets say this is a custom background for the puzzle
}

@end

然后,您可以为您的自定义拼图实现代码,在它的初始化中设置它的 startingTime,调用它继承的 startTimer 函数,然后就可以了。您可以在 scenarioIsComplete 的父类中实现默认功能,可能类似于

-(void)scenarioIsComplete {
     UIViewController *vc = [(ScenarioAppDelegate)([[UIApplication sharedApplication] delegate]) viewController];

     [vc dismissModalViewController];

}

这将处理所有子场景的解雇。

关于iphone - 在 iPhone 应用程序中跨多个屏幕进行通用覆盖的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6667514/

相关文章:

iPhone库: file is not of required architecture

ios - UITextView 结合了文本和图像

ios - 混合 Storyboard中的单元和以编程方式创建的单元

ios - Flutter Podfile 未正确创建

iphone - 我是否在滥用 UIViewController 子类化?

ios - 重新定义目标操作方法以便 `UIButtons` 将消息中继到我的 `UIViewController` 中的方法的最安全方法是什么?

iphone - 我们如何在小 UIImageview 上设置大 UIImage

iphone - UIImage 元数据

ios - Swift 2.3 到 Swift 3.1 迁移 - StatusBar 下的 EZSwipeViewController

swift - 如何从其他 UIViewController 中的 containerView 访问 UIViewController?