Reputation: 4044
I am using a UITabBarController
in a music playing app. I have 2 indexes in my tabBarController
, index 0 (tableViewController
) and index 1 (nowPlayingViewController
). Index 0 contains a UITableViewController
that displays song names, and index 1 contains a UIViewController
that is supposed to display the now playing page, including labels of the current playing song and artist.
When a user taps on a cell (a song name) on tableViewController
i do two things, first I give the title of the song that was selected to a third class, a ControllerClass
and then I move the tab bar over to index 1, which is the now playing page...
//this is inside tableViewController class
let _ControllerClass = ControllerClass()
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
_ControllerClass.nowPlayingSongInfo(trackTitles[indexPath.row])
self.tabBarController?.selectedIndex = 1
}
So far so good. The nowPlayingViewController
is the delegate of the ControllerClass
. When ControllerClass
receives the new song title, it calls a delegate method that is supposed to set the text of a UILabel on the nowPlayingViewController
. Here is the method inside ControllerClass
that calls back with the new title as a String.
func newNowPlayingSongTitle(songTitle: String){
delegate?.configureLabels(songTitle)
}
this also works fine, all good. I receive the callback successfully on nowPlayingViewController
which I know because I am able to print the song title when the callback is received, like so...
func configureLabels(songTitle: String){
print("song title is \(songTitle)")
//successfully prints the correct song title
}
HOWEVER, my issue is this... I need to not just print out the new song title, I need to set my UILabel's text property on my nowPlayingViewController
equal to the new song title that i receive in the callback. BUT, when i try this...
func configureLabels(songTitle: String){
print("song title is \(songTitle)")
self.songLabel.text = songTitle
//crashes on the line above error = "unexpectedly found nil while unwrapping an optional value"
}
the app crashes due to unexpectedly found nil while unwrapping...
. Apparently the UILabel is nil, even though I know i have set it up properly since I can set its text property from other places.
I also tried this,
func configureLabels(songTitle: String){
let view = UIView(frame: CGRectMake(100, 100, 100, 100))
self.view.addSubview(view)
}
to see if the entire view itself is nil, and again it crashes with the same error, so indeed self.view
is nil.
What is going on here? I am able to set the labels and images in this view from elsewhere, however in the situated illustarted above the UILabel and the entire view itself are nil. How to fix this problem? Any suggestions would be great, thank you.
Note: I have tried self.loadView() but that did not help either
Upvotes: 0
Views: 223
Reputation: 4044
My issue was that my delegate was set to a different instance of nowPlayingViewController, than the instance that was currently in the tabBarController. To fix this, instead of doing
let np = nowPlayingViewController()
ControllerClass.delegate = np
I did
let np = self.tabBarController?.viewControllers![1] as! nowPlayingViewController
then ControllerClass.delegate = np
the reason this is necessary with a UITabBarController is because the views that are embedded within the tabBarController are loaded once and then remain loaded, thus I needed to set the UILabels on the nowPlayingViewController for the proper instance of the nowPlayingViewController, which is the one that was embedded in the tabBarController, since that instance was the one being displayed. Sure hope someone else finds this useful!
Upvotes: 0