我有一个UISegmentedControl和两个ViewControllers。每个页面上的一个按钮可以浏览两个视图,但是当我将Segmentcontrol设置为某个视图并返回时,它将重置为原始视图。
继续我想要做的事情,用户在第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,在View#1上。所以我无法访问它。当我回到视图#2时,加上段控制重置。
答案 0 :(得分:1)
视图#2可能会在您返回查看#1时被取消分配,假设您弹出或关闭以返回。当您再次查看#2时,它是一个新实例,因此分段控件当然会显示默认选择。要更改第一个控制器中的标题属性,应使用控制器#2中定义的委托协议。使用它将颜色和字体属性传递回控制器#1,并让该控制器设置自己的标题特征。使用委托模式将数据发送回以前的控制器是Apple通常使用的范例,您可以在文档中阅读它。