Ian Ian - 3 years ago 129
Swift Question

Navigation UI not updating with Xcode 9

I tried to update my navigationItem titleView when I finished downloading data model from server.

Something like this :

private func loadNews() {
self.newsModelManager.sendRequest(inBackground: false, preSendHandler: { (isReachable) in
if isReachable {

} else {
appDelegate.showInternetFailedAlertView()
}
}, successHandler: { (response) in
print("loadNewsModel: successHandler")
}, errorHandler: { (response) in
print("loadNewsModel: errorHandler")
}, reloginFailHandler: { (response) in
appDelegate.showReloginFailedAlertView()
}) { (isReachable) in
let array = self.newsModelManager.loadFirstFiveNews()!
DispatchQueue.main.async {
self.setupTitle_ViewWith(array: array)
}
}
}


and in seupTitle_ViewWith(array:)

func setupTitle_ViewWith(array: [NewsModel]?) {
guard array != nil else { return }
let frame = CGRect(x: 0, y: 0, width: 200, height: 40)
let newsTitle_View = NewsTitleView(newsModelArray: array!, frame: frame)
newsTitle_View.newsTitleViewDelegate = self
self.title_View = newsTitle_View
}


this all works fine, until the last line
self.title_View = newsTitle_View


I thought this would update my titleView to my CustomView,
But it's not....
What might went wrong there?

I first save model from server with realm and read at
let array = self.newsModelManager.loadFirstFiveNews()!


But, say if I have data in device already and read directly without loading first, it works totally fine...

Answer Source

Hi here is the brief summary of what solved this issue for reference for others who might face the similar kind of issue,

The codes which @Ian has shared in the question has been added in the NavigationController subclass.
The issue is caused here. Because you cannot able to update the navigation title nor the titleView from the navigation controller for little explanation refer this answer(https://stackoverflow.com/a/20923010/4510873)


So we tried updating the titleView from viewController and it solved our issue then.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download