2014-12-19 12 views
0

引言
我在我的Xcode項目中有一個'settings'viewController和一個mainViewController。在設置控制器中,無論用戶何時按下靜態單元格,都應該更改mainViewController的某些元素的顏色。由於某種原因,這不適合我。誰會介意告訴我爲什麼?如何更改viewController的顏色(或主題)?

代碼

- (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中,如果有幫助的話。謝謝!

+2

您正在創建'ViewController'的新實例,而不是引用'ViewController'的現有實例。 – rmaddy

+0

@rmaddy我如何去引用現有的viewController?我試了下面的'ViewController * viewController = [self.storyboard instantiateViewControllerWithIdentifier:@「Main」];'從另一個答案,但也沒有奏效。 mainViewController位於pageViewController中,settingsViewController位於NavigationController中,如果有幫助的話。 – cyril

回答

1

在主視圖控制器中看不到顏色更改的原因是因爲您正在修改不在導航堆棧中的新實例。如果您不需要維護任何狀態,則可以替換堆棧中的先前實例。或者,另一個選擇是使用外觀API並修改那裏的顏色。

相關問題