2015-06-03 94 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