使用 popViewControllerAnimated 时按钮色调会改变颜色 [英] Button tint changes color when using popViewControllerAnimated

查看:22
本文介绍了使用 popViewControllerAnimated 时按钮色调会改变颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

View #1 中,我在情节提要中设置了两个控件;一个是导航栏中的 UIBarButtonItem,另一个是 UISegmentedController.前者是自定义绘制的图像,两个项目的色调都设置为紫色(在情节提要属性检查器中设置的色调).

In View #1 I have two controls that are set in the storyboard; one is a UIBarButtonItem in the nav bar, the other is a UISegmentedController. The former is a custom-drawn image, and both items have their tint set to a purple color (tint set in the storyboard attributes inspector).

通过各种操作,用户可以通过View #1进入View #2.如果在 View #2 中不满足特定条件,用户将显示一条错误消息,并重定向回 View #1 点击确定"后.

Through various actions, the user can get to View #2 via View #1. If a certain condition is not met when in View #2, the user is displayed an error message, and is redirected back to View #1 upon clicking "OK".

if(i == [self.itemsAvailable count]){
    UIAlertView *alertView = [[UIAlertView alloc] 
                    initWithTitle:@"Oh no!" 
                    message:@"Warning message text!" 
                    delegate:self 
                    cancelButtonTitle:@"OK" 
                    otherButtonTitles:nil, nil];

    [alertView show];

    break;
}

请注意,警报是在 while 循环内触发的,因此是 break;.然后下面的函数返回到 View #1

Note that the alert is triggered within a while loop, hence the break;. The following function then returns to View #1

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == [alertView cancelButtonIndex]) {
        // Jump back one screen
        [self.navigationController popViewControllerAnimated:YES];
    }
}

问题是,当我使用 popViewControllerAnimated:YES 函数返回 View #1 时,前面提到的两个控件(UIBarButtonItemUISegmentedController) 的色调显示为灰色而不是所需的紫色.

The thing is, when I return to View #1 using the popViewControllerAnimated:YES function, the two controls mentioned previously (the UIBarButtonItem and the UISegmentedController) have their tints showing as grey instead of the desired purple.

选择不同的 UISegmentedController 值会带回适当的色调,但我需要为 UIBarButtonItem 保留 View #1 返回到正确的紫色色调.

Selecting a different UISegmentedController value brings back the appropriate tint color, but I need to leave View #1 for the UIBarButtonItem to return to the proper purple tint.

色调颜色为什么会发生变化,我该如何解决这个问题,以便它们在返回到 View #1 时自动具有适当的色调颜色?

How come the tint colors are changing, and how can I remedy this issue so that they automatically have the appropriate tint colors upon popping back to View #1?

View #1 具有以下 viewWillAppear 函数

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    // Unhide the nav bar (hidden on home screen)
    [self.navigationController.navigationBar setHidden:NO];

    [self setUserDefaults]; // Load the appropriate values and set in UISegmentedController
}

哪里

- (void) setUserDefaults {

    // Set the defaults from the settings
    int selectedValue;

    selectedValue = (int)[self.userDefaults integerForKey:@"SomeValue"];        

    [self.defaultsSegment setSelectedSegmentIndex:selectedValue];
}

修改了一些变量名,省略了一些代码,但这是重要的部分.我不认为该函数与不正确的色调颜色有任何关系,因为 UIBarButtonItem 从未通过代码修改,并且显示与 UISegmentedController 相同的错误.

Some variable names have been modified and some code has been omitted, but this is the important parts. I do not believe that the function has anything to do with the incorrect tint color, because the UIBarButtonItem is never modified through code and is showing the same error as the UISegmentedController.

将行 [self loadView]; 添加到 viewWillAppear 方法修复了 UISegmentedController 色调颜色,但我还不知道如何修复 UIBarButtonItem 色调问题.使用 [self.navigationController loadView]; 行会导致一堆问题.

Adding the line [self loadView]; to the viewWillAppear method fixes the UISegmentedController tint color, but I cannot yet figure out how to fix the UIBarButtonItem tint problem. Using the line [self.navigationController loadView]; causes a whole mess of issues.

UIAlertView 在本身从 viewWillAppear 调用的方法中调用.如果我将方法调用移动到 viewDidAppear 然后 UIBarButtonItem 保留其正确的色调颜色.但是在视图加载完成后,视图中的图形突然出现 - 这很不雅观.

The UIAlertView is called within a method that itself is called from viewWillAppear. If I move the method call to viewDidAppear then the UIBarButtonItem retains its proper tint color. But then the graphics within the view suddenly appear AFTER the view has finished loading - which is unsightly.

推荐答案

问题的原因是 popViewControllerAnimated 在视图加载完成之前被调用.

The reason for the issues is that the popViewControllerAnimated was called before the view had finished loading.

事情是这样的……

viewWillAppear 方法中调用了另一种方法,该方法检查条件并决定是否弹出一个视图.整个事情发生在视图加载完成之前,这就是问题所在.

Within the viewWillAppear method was a call to another method that checked a condition and decided whether to pop up one view or not. The whole thing happened before the view had even finished loading, and that is where the problem came in.

尝试将其放在 View #2 中,然后从 View #1 导航到它:

- (void)viewWillAppear:(BOOL)animated{
{
    [super viewWillAppear:animated];
    [self.navigationController popViewControllerAnimated:YES];
}

代码可以很好地编译,但是当您导航到 View #2 时,您将获得以下日志:

The code will compile just fine, but when you navigate to View #2 you will get the following log:

嵌套的流行动画会导致导航栏损坏"

基本上这就是我在流行后色调颜色变化时遇到的问题.popViewControllerAnimated 方法在代码中足够深,它没有被 Xcode 捕获,但到目前为止还没有造成一系列问题.

And essentially that is the problem that I was experiencing with the tint colors changing after the pop. The method popViewControllerAnimated was deep enough in the code that it was not caught by Xcode, but not so far that it didn't create a bunch of problems.

我采取的解决方法如下:

The workaround that I did was the following:

  1. 创建一个 BOOL 变量 @property (nonatomic) BOOL didFail;
  2. 首先在 willViewAppear
  3. 中将 BOOL 变量设置为 FALSE
  4. 调用测试条件的方法(在willViewAppear内)
  5. 如果条件失败,将 BOOL 变量设置为 TRUE
  6. viewDidAppear 内检查 BOOL 变量是否为真或不是.如果条件是您要查找的内容,则弹出一个视图.
  1. Create a BOOL variable @property (nonatomic) BOOL didFail;
  2. Set the BOOL variable to FALSE first thing in willViewAppear
  3. Call the method (within willViewAppear) that tests the condition
  4. If condition fails, set BOOL variable to TRUE
  5. Within viewDidAppear check to see if the BOOL variable is true or not. Pop up one view if condition is what you are looking for.

代码:

.h 文件

@property (nonatomic) BOOL didFail;

.m 文件

- (void)viewWillAppear:(BOOL)animated{
{
    [super viewWillAppear:animated];
    self.didFail = FALSE;

    [self methodCall];
}

- (void) methodCall
{
    if(condition is met){
        self.didFail = TRUE;
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if(self.didFail == TRUE){
        //alert message
        [self.navigationController popViewControllerAnimated:YES];
    }
}

然后就可以了.在警报出现之前有一个轻微的停顿,但可以忽略不计.现在,在流行音乐之后,一切都完全正常!

And that'll about do it. There is a slight pause before the alert shows up, but it is negligible. And now after the pop everything is exactly as it should be!

这篇关于使用 popViewControllerAnimated 时按钮色调会改变颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆