在iOS開發(fā)過程中關(guān)于全局變量的幾個方法
1.在APPDelegate中聲明并初始化全局變量.
AppDelegate可以在整個應(yīng)用程序中調(diào)用,在其他頁面中可以使用代碼段獲取AppDelegate的全局變量:AppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];
因此可以在AppDelegate.h中定義需要全局使用的變量咬摇。
/** 設(shè)置全局變量的屬性. */
@property (nonatomic, assign)BOOL isLong;
- 通過單例模式獲取屬性
/** 給全局變量賦值. */
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
myDelegate.isLong = YES;
- 在viewController中獲取該值
AppDelegate *myDelegate = [[UIApplication sharedApplication]delegate];
myDelegate.isLong = YES;
NSLog(@"myDelegate: %d", myDelegate.isLong);
由此便可以創(chuàng)建一個全局變量, 在系統(tǒng)的任意界面調(diào)用. 這樣便可以創(chuàng)建夜間模式.