1

我有一个导航控制器视图,带有一个嵌入式 collectionview 控制器。我以编程方式添加了一个右栏按钮,其中添加了一个图像,如下所示:

    self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "greyCircle").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector (handleGrayCircleButton))

我希望单击此按钮的用户能够将图像从灰色更改为银色。我将按钮处理程序设置为每次按下时从灰色切换到银色:

        self.navigationItem.rightBarButtonItem?.image = UIImage(named: "greyCircle")

但是,图像在切换回来之前只会短暂更改。我试图将图像的颜色保存为“标志”变量,即如果它当前为灰色,则变为银色,以使更改持续存在,但是,图像会不断重置为加载的任何内容(与 viewDidLoad 或 viewWillAppear)。

每次点击后是否有刷新/重新加载navigationController标签栏并使其持续存在的方法,以及栏按钮的图像?

#

编辑:以下是完整的按钮处理程序。

   func handleFavoritePress(){
        print("Favorites pressed")
        if(!returnTrueIfInFavorites(objectName: readString(object: self.patientRecord!, key: "name"), objectType: LIST_CoreDataBaseObjects.Patients)){
            // Add to favorites
            Favorites_Names.append(readString(object: self.patientRecord!, key: "name"))
            Favorites_Types.append(LIST_CoreDataBaseObjects.Patients)


            // Let user know
            let patientName = readString(object: self.patientRecord!, key: "name")
            let alertController = UIAlertController(title: "Alert!", message: "\(patientName) has been added to your favorites.", preferredStyle: .alert)
            let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alertController.addAction(defaultAction)
            var rootViewController = UIApplication.shared.keyWindow?.rootViewController
            if let navigationController = rootViewController as? UINavigationController {
                rootViewController = navigationController.viewControllers.first
            }
            if let tabBarController = rootViewController as? UITabBarController {
                rootViewController = tabBarController.selectedViewController
            }
            rootViewController?.present(alertController, animated: true, completion: nil)
            self.navigationItem.rightBarButtonItem?.image = UIImage(named: "savedFavorite_1")

        }else{

            self.navigationItem.rightBarButtonItem?.image = UIImage(named: "favorite_2")
            deleteFromFavorites(objectName: readString(object: patientRecord!, key: "name"), objectType: LIST_CoreDataBaseObjects.Patients)
        }
        for favorite in Favorites_Names{
            print("Favorites are currently: \(patient)")
        }

    }

及其他功能:

   override func viewWillAppear(_ animated: Bool) {

        collectionView?.backgroundView = setBackgroundImage(imageName: "whiteBackground")
        navigationItem.title = "Patient"
        if(returnTrueIfInFavorites(objectName: readString(object: patientRecord!, key: "name"), objectType: LIST_CoreDataBaseObjects.Patients)){
            setupNavBarButtonsIsFavorited()
        }else{
            setupNavBarButtonsNotFavorited()
        }
    }

    func setupNavBarButtonsNotFavorited(){
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "favorite_2").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector (handleFavoritePress))
    }
    func setupNavBarButtonsIsFavorited(){
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(image:  #imageLiteral(resourceName: "savedFavorite_1").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector (handleFavoritePress))
    }
4

1 回答 1

0

您的问题的答案在于这两个LIST_CoreDataBaseObjects.Patients或变量。returnTrueIfInFavoritespatientRecord

要么LIST_CoreDataBaseObjects.Patients没有更新你的函数handleFavoritePress代码

/* this code */
Favorites_Names.append(readString(object: self.patientRecord!, key: "name"))
Favorites_Types.append(LIST_CoreDataBaseObjects.Patients)
/* this code */

因为从你在评论部分的解释来看,如果我理解正确的话,当你点击按钮时,图像会改变,然后你导航到不同的视图,但是当你导航回来时,viewDidAppear(_:)就会viewWillAppear(_:)被触发。

在下面的代码块中,我将解释

// this function will be triggered everytime you navigate into this view (because it has appeared)
override func viewWillAppear(_ animated: Bool) {
    /* don't forget to call super.viewWillAppear(true) */
    super.viewWillAppear(true)
    /* don't forget to call super.viewWillAppear(true) */

    collectionView?.backgroundView = setBackgroundImage(imageName: "whiteBackground")

    navigationItem.title = "Patient"

    // the problem lies here
    /* the function `(returnTrueIfInFavorites(objectName: readString(object: patientRecord!, key: "name"), objectType: LIST_CoreDataBaseObjects.Patients))` is always returning one thing which is why your image returns back to its previous state */ 
    if (returnTrueIfInFavorites(objectName: readString(object: patientRecord!, key: "name"), objectType: LIST_CoreDataBaseObjects.Patients)) {
        setupNavBarButtonsIsFavorited() // only one of these gets called
    } else {
        setupNavBarButtonsNotFavorited() // only one of these gets called
    }
}

func setupNavBarButtonsNotFavorited() {
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "favorite_2").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector (handleFavoritePress))
}

func setupNavBarButtonsIsFavorited() {
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(image:  #imageLiteral(resourceName: "savedFavorite_1").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector (handleFavoritePress))
}

所有这些,或者您只是没有更新变量self.patientRecord!的值(我没有在您的代码中看到与此变量相关的任何设置)

于 2017-07-13T00:08:09.303 回答