2015-06-04 46 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通常使用的范例,您可以在文档中阅读它。