如何从另一个更改viewController的颜色(或主题)?

时间:2014-12-19 23:56:28

标签: ios objective-c uiviewcontroller uicolor

简介
我有一个'设置' viewController和我的Xcode项目中的mainViewController。在设置控制器中,每当用户按下静态单元格时,它应该改变mainViewController的某些元素的颜色。出于某种原因,这对我来说并不适用。有人会介意告诉我为什么吗?

代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"Main"];

    self.tableView.delegate = self;
    if (indexPath.section==0) {
    _checkedCell = indexPath.row;
    [self.tableView reloadData];
    }

    if ((indexPath.row == 0) && (indexPath.section==0)) {
        NSLog(@"Let there be gold");
        UIColor *gold = [UIColor colorWithRed:226.0f/255.0f
                                          green:202.0f/255.0f
                                           blue:162.0f/255.0f
                                          alpha:1.0f];
        UIColor *goldComp = [UIColor colorWithRed:255.0f/255.0f
                                          green:255.0f/255.0f
                                           blue:255.0f/255.0f
                                          alpha:1.0f];


        //testing one label
        viewController.changeZone.tintColor = gold;
        viewController.zoneTime.textColor = goldComp;


    }
    if ((indexPath.row == 1) && (indexPath.section==0)) {
        NSLog(@"Let there be silver");
        UIColor *silver = [UIColor colorWithRed:170.0f/255.0f
                                 green:170.0f/255.0f
                                  blue:170.0f/255.0f
                                 alpha:1.0f];
        UIColor *silverComp = [UIColor colorWithRed:255.0f/255.0f
                                          green:255.0f/255.0f
                                           blue:255.0f/255.0f
                                          alpha:1.0f];
        viewController.changeZone.tintColor = argentum;
        viewController.zoneTime.textColor = argentumComp;
    }
    if ((indexPath.row == 2) && (indexPath.section==0)) {
        NSLog(@"Let there be snow");
        UIColor *snow = [UIColor colorWithRed:255.0f/255.0f
                                 green:255.0f/255.0f
                                  blue:255.0f/255.0f
                                 alpha:1.0f];
        UIColor *snowComp = [UIColor lightGrayColor];
        viewController.changeZone.tintColor = album;
        viewController.zoneTime.textColor = albumComp;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {
    if (_checkedCell == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    }
}

解释
上面的代码应该导致mainViewController中标签的颜色变化不同,具体取决于被轻敲的单元格。每当点击一个单元格时,就会出现一个复选标记。

通知
日志出现在控制台中,但mainViewController没有其他任何操作。有什么建议? mainViewController位于pageViewController中,settingsViewController位于NavigationController中,如果它可以帮助解决任何问题。谢谢!

1 个答案:

答案 0 :(得分:1)

您在主视图控制器中看不到颜色更改的原因是您正在修改不在导航堆栈中的新实例。如果您不需要维护堆栈中的任何状态,则可以替换堆栈中的先前实例。或者,另一种选择是使用外观API并修改那里的颜色。

相关问题