一种可能的解决方案是创建一个应用程序委托的公共属性来保存您的背景图像。
@property (strong) UIImage *globalBackground;
在您的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中,您应该将其初始化为某个默认图像或上次保存的图像。
self.globalBackground = [UIImage imageNamed:@"defaultBackground"];
假设您的 appDelegate 头文件是 MyAppDelegate.h。您应该在所有使用此默认背景图像机制的类中导入它。或者直接将其添加到您的 Prefix.pch 文件中。
然后在您的设置视图控制器中设置:
//your background setting handler
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.globalBackground = ... //however you get the selected image
//and here you set the background image of settings view controller
然后在每个视图控件viewWillAppear: 中以类似的方式设置其背景:
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
//then you use appDelegate.globalBackground the same way you are doing it now
如果您有很多视图控制器,您可能需要考虑创建一个实现此机制的UIViewController 的子类(可能称为UIViewControllerWithBackground)并使您的所有视图控制器成为此类的子类。
注意 A:通常会(而不是 appDelegate 的属性)使用包含所有设置的单例类。这可能被认为是一种不好的(快速而肮脏的)做法。 More...
注意 B:要保存最后选择的背景的名称,您可以使用 NSUserDefaults。这超出了这个问题的范围,并且有很多示例代码可用......