通过ViewControllers传递数据 - 进行设置页面

时间:2015-06-03 01:31:05

标签: ios objective-c iphone xcode

所以我现在已经有这个问题一段时间了,如果有人能把我带到正确的道路上,我会非常感激。基本上我正在制作一个应用程序,但有一个设置按钮。每当我启用SegmentedControl或UISWITCH时,我都会返回到默认状态。另外,我想要一个分段控件来改变颜色。我已设置颜色,但我要更改的所有信息都在视图控制器#1上。基本上我如何创建一个设置页面来保持更改。到目前为止我的代码。

- (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"是在第一个视图控制器上,所以它自动不起作用。我怎样才能解决这个问题。请把我链接到某个地方/以一种不复杂的方式给我看。 (我也会有很多标签)

2 个答案:

答案 0 :(得分:1)

为此,我通常使用在文件级别声明的结构(不嵌套在任何类中)。因此,应该可以在整个应用程序中访问它。更改所选分段时设置值;加载视图控制器时获取值。这是Swift中的一个例子:

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

在View Controller viewDidLoad:

设置中
Controller.selectedSegmentIndex = settings.selSeg

在分段控制的IBAction中:

settings.selSeg = Controller.selectedSegmentIndex

答案 1 :(得分:0)

如果您想进行"设置"页面,我建议您阅读NSUserDefaults。您可以做的是在didFinishLaunchingWithOptions的{​​{1}}中创建默认值。

以下是一个例子:

<强> AppDelegate.m

AppDelegate.m

在Interface Builder中,您可以创建- (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内容静态单元格。您可以在Interface Builder的“属性检查器”选项卡上为单元格创建部分。然后你可以拖动标签,开关等,然后将它们连接到你需要创建的tableView课程。

基本上,您创建一个默认值,该值在启动之间保持不变:

PreferencesTableViewController

你这样称呼它:

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

您可以为许多内容设置默认值,正如您在Apple的文档中看到的那样

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

这是一篇涉及[[NSUserDefaults standardUserDefaults]valueForKey:@"defaultTime"] 的帖子,这似乎是您主要关注的内容。

Saving UIColor to and loading from NSUserDefaults