titans 2019-06-27
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
NSUserDefaults *launchSettings=[NSUserDefaults standardUserDefaults]; if([launchSettings boolForKey:(@"hasLaunched")]==NO) { [launchSettings setBool:(YES) forKey:(@"hasLaunched")]; [launchSettings setBool:(NO) forKey:(@"nightShiftOpen")]; }
- (void)viewWillAppear:(BOOL)animated
该方法是页面即将出现的时候被调用,可放入实时刷新的语句,例如下面的例子是简易的夜间模式的实现:
- (void)viewWillAppear:(BOOL)animated { BOOL isOpenNightShift=[_userSettings boolForKey:(@"nightShiftOpen")]; if(isOpenNightShift==YES) { self.tableView.backgroundColor=[UIColor blackColor]; self.navigationController.navigationBar.backgroundColor=[UIColor blackColor]; } else { self.tableView.backgroundColor=[UIColor whiteColor]; self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor]; } }
@property (strong,nonatomic) UISwitch *nightShiftOpen;
if(_nightShiftOpen==nil) { _nightShiftOpen=[[UISwitch alloc]init]; } [_nightShiftOpen addTarget:(self) action:(@selector(nightShiftOpenValueChanged:)) forControlEvents:(UIControlEventValueChanged)];
nightShiftOpenValueChanged:
是绑定的方法的名称,而UIControlEventValueChanged
是事件的类型,此处类型为控件值的改变。然后在对应的UIViewController中实现监听器方法,此处实现的是夜间模式开关的监听:
- (void)nightShiftOpenValueChanged:(id)sender { if([_nightShiftOpen isOn]==YES) { [_userSettings setBool:(YES) forKey:(@"nightShiftOpen")]; self.tableView.backgroundColor=[UIColor blackColor]; self.navigationController.navigationBar.backgroundColor=[UIColor blackColor]; } else { [_userSettings setBool:(NO) forKey:(@"nightShiftOpen")]; self.tableView.backgroundColor=[UIColor whiteColor]; self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor]; } [_userSettings synchronize]; }