2015-06-04 99 views
0

我有一個UISegmentedControl和兩個ViewControllers。每個頁面上的按鈕都可以瀏覽這兩個視圖,但當我將Segmentcontrol設置爲某個特定的視圖並返回時,它會重置爲原始視圖。UISegmented控制切換視圖重置

繼承人我想要做的,頁面1上的用戶單擊設置,然後UISegmentedControl選擇配色方案並返回。 (但我怎麼從視圖#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,位於視圖#1。所以我無法訪問它。加上段控制重置,當我回去查看#2。

+0

使用委託並通過以下方式手動設置:'self.segmentControl.selectedSegmentIndex' – 0yeoj

回答

1

視圖#2可能會在您回到視圖#1時釋放,假設您彈出或解散回去。當你再次查看#2時,它是一個新的實例,所以當然分段控件將顯示默認選擇。要更改第一個控制器中的標題屬性,應該使用控制器#2中定義的委託協議。使用它將顏色和字體屬性傳遞迴控制器#1,並讓該控制器設置自己的標題特徵。使用委託模式將數據發送回先前的控制器是Apple通常使用的範例,您可以在文檔中閱讀它。