Swift 3-重新加载导航控制器按钮图像



i具有带有嵌入式收集视图控制器的导航控制器视图。我用编程方式添加了一个正确的条按钮,其中添加了图像如下:

    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(。

每次点击之后,是否都有可以刷新/重新加载导航控制器选项卡栏,并使其持久使用,以及bar按钮的图像?

编辑:以下是全按钮处理程序。

   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))
    }

您问题的答案在于两个LIST_CoreDataBaseObjects.PatientsreturnTrueIfInFavoritespatientRecord变量中。

任何LIST_CoreDataBaseObjects.Patients都没有在您的功能handleFavoritePress的代码上更新

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

因为根据您在评论部分中的解释,如果我正确理解的话,当您 tap 按钮时,图像会更改,然后导航到其他视图,然后当您返回时,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!的值(我在代码中没有看到与此变量有关的任何设置(

最新更新