Swift 3 - Reload NavigationController按钮图像

时间:2017-06-28 17:02:42

标签: swift image refresh uibarbuttonitem navigationitem

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

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

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

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

但是,图像仅在切换回之前短暂更改。我试图将图像的颜色保存为“标志”变量,即如果它当前是灰色的,则变为银色,以便使更改持续存在,但是,图像会不断重置为加载的任何内容(或者with 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))
    }

1 个答案:

答案 0 :(得分:0)

您的问题的答案位于这两个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 */

因为根据您在评论部分中所解释的内容,如果我理解正确,那么当您点按按钮时图片会发生变化,然后您导航到其他视图,当您向后导航时,系统会触发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!的值(我没有在您的代码中看到与此变量相关的任何设置)