2015-06-03 32 views
3

所以我一直有這個問題一段時間,如果任何人都可以把我帶到正確的道路,我將不勝感激。基本上我正在製作一個應用程序,但有一個設置按鈕。每當我啓用SegmentedControl或UISWITCH時,我會回到默認狀態。另外我想要一個分段控件來改變顏色。我已經設置了顏色,但我想要更改的所有信息都在視圖控制器#1上。基本上我怎麼做一個設置頁面來保持更改。繼承我的代碼到目前爲止。通過ViewControllers傳遞數據 - 進行設置頁面

- (IBAction)colorController:(id)sender { 

if (Controller.selectedSegmentIndex == 0) { 

    //App title text color 
    appTitle.textColor = [UIColor colorWithRed:1.00 green:1.00 blue:0.00 alpha:1.0]; 

    //Background color when selected 
    Controller.tintColor = [UIColor colorWithRed:1.00 green:1.00 blue:0.00 alpha:1.0]; 

    //The font of the selected 
    NSDictionary *fontColor = [NSDictionary dictionaryWithObjectsAndKeys: 
           [UIColor blackColor],NSForegroundColorAttributeName, 
           nil]; 
    [Controller setTitleTextAttributes:fontColor forState:UIControlStateSelected]; 



} 
if (Controller.selectedSegmentIndex == 1) { 

    //App title text color 
    appTitle.textColor = [UIColor colorWithRed:0.00 green:0.66 blue:1.00 alpha:1.0]; 

    //Background color when selected 
    Controller.tintColor = [UIColor colorWithRed:0.00 green:0.66 blue:1.00 alpha:1.0]; 

    //The font of the selected 
    NSDictionary *fontColor = [NSDictionary dictionaryWithObjectsAndKeys: 
           [UIColor whiteColor],NSForegroundColorAttributeName, 
           nil]; 
    [Controller setTitleTextAttributes:fontColor forState:UIControlStateSelected]; 

} 
if (Controller.selectedSegmentIndex == 2) { 

    //App title text color 
    appTitle.textColor = [UIColor colorWithRed:0.98 green:0.22 blue:0.22 alpha:1.0]; 

    //Background color when selected 
    Controller.tintColor = [UIColor colorWithRed:0.98 green:0.22 blue:0.22 alpha:1.0]; 


    //The font of the selected 
    NSDictionary *fontColor = [NSDictionary dictionaryWithObjectsAndKeys: 
           [UIColor whiteColor],NSForegroundColorAttributeName, 
           nil]; 
    [Controller setTitleTextAttributes:fontColor forState:UIControlStateSelected]; 
} 
if (Controller.selectedSegmentIndex == 3) { 

    //App title text color 
    appTitle.textColor = [UIColor colorWithRed:0.15 green:0.82 blue:0.44 alpha:1.0]; 

    //Background color when selected 
    Controller.tintColor = [UIColor colorWithRed:0.15 green:0.82 blue:0.44 alpha:1.0]; 

    //The font of the selected 
    NSDictionary *fontColor = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]; 
    [Controller setTitleTextAttributes:fontColor forState:UIControlStateSelected]; 

} 

}現在

,你可以看到「appTitle」是第一個視圖控制器上,因此它會自動不工作。我怎樣才能解決這個問題。請將我鏈接到某處/以一種不復雜的方式展示我。 (我也會有很多標籤)

回答

1

爲此,我通常使用一個在文件級聲明的結構體(不嵌套在任何類中)。因此,應該可以在整個應用程序中訪問。設置更改所選段時的值;加載視圖控制器時獲取該值。下面是斯威夫特的例子:

struct setStruct { 
    var selSeg: Int = 0 
} 
var settings = setStruct() 

在設置視圖控制器的viewDidLoad:

Controller.selectedSegmentIndex = settings.selSeg 

在IBAction爲爲分段控制:

settings.selSeg = Controller.selectedSegmentIndex 
0

如果你想要做一個「設置「頁面,我建議你閱讀NSUserDefaults。你可以做的是在AppDelegate.mdidFinishLaunchingWithOptions中創建默認值。

下面是一個例子:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // this is a method I created to check if there are default values set already 
    [self checkUserDefaults]; 
    return YES; 
} 

#pragma mark - Initial defaults 

- (void)checkUserDefaults { 

    NSUserDefaults *defaults= [NSUserDefaults standardUserDefaults]; 
// If there's a default value for a key, then don't do anything 
    if([[[defaults dictionaryRepresentation] allKeys] containsObject:@"defaultTime"]){ 
     NSLog(@"User Defaults are ALREADY set"); 
    } else { 
// Otherwise, create a default value for it 

     // ** Set initial userDefaults ** 
     [[NSUserDefaults standardUserDefaults]setInteger:15 forKey:@"defaultTime"]; 

     NSLog(@"User Defaults have been initially set"); 
     } 
} 

在界面生成器,你可以創建一個TableViewController故事板,使tableView內容靜態細胞。您可以在Interface Builder的屬性檢查器選項卡上爲單元格創建部分。然後,您可以拖動標籤,開關等,並將它們連接到您需要創建的PreferencesTableViewController類。

基本上,你創建一個與此發射之間仍然存在一個默認值:

[[NSUserDefaults standardUserDefaults]setInteger:15 forKey:@"defaultTime"] 

,你這樣稱呼它:

[[NSUserDefaults standardUserDefaults]valueForKey:@"defaultTime"] 

您可以爲很多事情設置默認值,爲你將在Apple的文檔中看到

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/UserDefaults/AccessingPreferenceValues/AccessingPreferenceValues.html

這裏是一個處理UIColor的帖子,這似乎是你主要關注的內容。

Saving UIColor to and loading from NSUserDefaults