2014-02-20 28 views
-1

我搜索了互聯網和20多個有關全局變量的主題,我找不到我需要的主題。iOS中的動態全局變量應用程序

  • 將在iOS應用程序中使用它。
  • 我希望變量可以從所有視圖訪問。
  • 我想要幾個視圖能夠更改該變量。
  • 最好不要混淆segues。
+0

可變因素不會幫助你嗎? –

+1

你不喜歡OOP嗎?你不喜歡封裝嗎? – Abizern

+0

我只是不喜歡或明白objective-c – Esqarrouth

回答

3

你可以做什麼暗示,在AppDelegate中從您的視圖控制器中添加變量,然後把它拿來這樣的:

AppDelegateNeme *ap = (AppDelegateNeme *)[[UIApplication sharedApplication] delegate]; 
ap.yourVar = smth 

正如你可以在上面代碼中看到,你正在訪問UIApplication的共享實例,只需將它轉換到AppDelegate類,您就可以訪問您的「全局」變量。但我個人並不認爲這是最好的解決方案。會發生什麼是你需要在每個UIViewController中包含你的應用程序委託,並且你可能已經在你的AppDelegate中包含了這個UIViewController,所以你最終可能會看到遞歸包含或者非常混亂的代碼。

更清晰的方法是創建一個類來存儲您的全局變量,只需添加一個文件,併爲類選擇NSObject。然後,您可以創建該類的單例對象,或者您可以定義類變量和類方法(帶+的類)來存儲和取自「全局變量」中的值。

通過這種方式,您的代碼將是模式可讀的,並且您將永遠不會有任何包含問題,只要您不開始在該類中包括東西。

例子:

//GlobalClass.h 
@interface GlobalClass : NSObject 

@property (nonatomic) BOOL someBool; 

// example wit a singleton obj: 
+ (GlobalClass *)globalClass; 

// example with class methods 
+(int)GetMyVar; 
+(void)SetMyVar:(int)var; 

//GlobalClass.m 
static int MyVar; 

@synthesize someBool; 

static GlobalClass *globalClass = nil; 

+ (GlobalClass *)globalClass 
{ 
    if (globalClass == NULL) 
    { 
     // Thread safe allocation and initialization -> singletone object 
     static dispatch_once_t pred; 
     dispatch_once(&pred, ^{ globalClass = [[GlobalClass alloc] init]; }); 
    } 
    return globalClass; 
} 

+(int)GetMyVar 
{ 
    return MyVar; 
} 
+(void)SetMyVar:(int)var 
{ 
    MyVar = var; 
} 

所以從外部(從視圖 - 控制): 要創建單並設定我們的布爾屬性:

//set var: 
[[GlobalClass globalClass] setSomeBool:YES]; 
// get 
BOOL b = [[GlobalClass globalClass] someBool]; 

或使用類的方法(不需要創建singletone obj)

// set 
[GlobalClass SetMyVar:5]; 
// get 
int num = [GlobalClass GetMyVar]; 

希望這會有所幫助...

+0

使用未聲明的標識符'globalClass',,,,也許這個代碼中缺少一些東西? singletone obj和class方法有什麼區別?看起來類方法要簡單得多,除非有理由使用singletone obj,否則我更喜歡他們。 – Esqarrouth

+1

你是對的,我編輯了我的答案......是的,你可以,特別是當你只想保存一些幫助數據或全球變量,這是你想要的。 Sinletone對象與您在類中實例化的其他對象相似,唯一的區別是您只能創建一次。正如你可以從我的編輯看到它的指針存儲在一個類變量... – AntonijoDev

0

你應該在你的Application Delegate類中聲明你的'全局'變量。

這種方式可以確保您的所有viewsControllers等都能夠訪問它們。

對我來說,這可能是最優雅的解決方案,當您需要訪問這些變量之一時,您可以獲取對應用程序委託的引用。

+0

的例子嗎? extern關鍵字?另一種方式?該怎麼做的錯誤「不能改變只讀變量」? – Esqarrouth

+0

AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate]; NSString * str = [appDelegate str]; 這就是你說的方式嗎? – Esqarrouth

+0

這是一種方法,是的。你的應用程序委託中聲明瞭str的位置。 – Woodstock

0

創建一個singleton類。將所有全局變量添加爲讀/寫屬性。創建應用程序啓動時的單例實例,通常在application:didFinishLaunchingWithOptions:方法中。使用這個單例實例,您現在可以在整個應用程序中共享信息。

如果您的應用程序將在多線程環境中訪問這些屬性,您可能需要製作屬性thread safe

希望有幫助!

2

在應用程序委託米文件應用程序委託頭

extern NSString* const globalVariable1; 

NSString* const globalVariable1 = @"My Value"; 

可以在任何視圖控制器訪問該全局變量。

這只是字符串。

int const用於整數。

只是BOOL爲布爾值。

希望這有助於

+1

只讀變量不可分配 – Esqarrouth

+0

您可以刪除const來分配值。 – Bala

+0

工作,謝謝,方法這個安全嗎? – Esqarrouth

1

爲SWIFT我用這個方法:

struct MyStyles { 

static let ThemeColor = UIColor(red: 41/255, green: 156/255, blue: 253/255, alpha: 1.0) 
static let ThemeSecondColor = UIColor.whiteColor() 
static let ThemeGrayLineColor = UIColor(red: 128/255, green: 128/255, blue: 128/255, alpha: 1.0) 
static let ThemeMyCommentsColor = UIColor(red: 219/255, green: 238/255, blue: 255/255, alpha: 1.0) 
static let ThemeCommentedToMeColor = UIColor(red: 227/255, green: 232/255, blue: 235/255, alpha: 1.0) 

static let ThemeFontName = "HelveticaNeue-Light" 
static let ThemeFontNameBold = "Helvetica-Bold" 

static let ScreenWidth = UIScreen.mainScreen().bounds.size.width 
static let ScreenHeight = UIScreen.mainScreen().bounds.size.height 
static let StatusBarHeight = UIApplication.sharedApplication().statusBarFrame.size.height 

static func largeButton(#title: String, action: String, target: UIViewController) -> UIButton { 

    let button = UIButton(frame: CGRect(x: 0, y: ScreenHeight*9/10, width: ScreenWidth, height: ScreenHeight/10)) 
    button.setTitle(title, forState: UIControlState.Normal) 
    button.backgroundColor = ThemeSecondColor 
    button.tintColor = ThemeColor 
    button.titleLabel?.font = UIFont(name: ThemeFontName, size: 50) 
    button.setTitleColor(ThemeColor, forState: UIControlState.Normal) 
    button.addTarget(target, action: Selector(action), forControlEvents: UIControlEvents.TouchUpInside) 
    return button 
} 

} 

我訪問他們像這樣從其他類:這是在宣佈的appdelegate

var color = MyStyles.ThemeColor 
+1

你爲什麼用ObjC標記你的問題,併發布Swift的答案?請適當地將您的問題標記爲您正在使用的語言。 –

相關問題