通过 UITabBarController 移动到选定的索引后,UIView 为 nil
UIView is nil after moving to the selected index via UITabBarController
我在音乐播放应用程序中使用 UITabBarController
。我的 tabBarController
中有 2 个索引,索引 0 (tableViewController
) 和索引 1 (nowPlayingViewController
)。索引 0 包含一个显示歌曲名称的 UITableViewController
,索引 1 包含一个应该显示正在播放页面的 UIViewController
,包括当前播放歌曲和艺术家的标签。
当用户点击 tableViewController
上的单元格(歌曲名称)时,我会做两件事,首先我将所选歌曲的标题提供给第三个 class,ControllerClass
然后我将标签栏移到索引 1,这是正在播放的页面...
//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
}
到目前为止一切顺利。 nowPlayingViewController
是 ControllerClass
的代表。当 ControllerClass
收到新歌名时,它会调用一个委托方法,该方法应该在 nowPlayingViewController
上设置 UILabel 的文本。这是 ControllerClass
中的方法,它使用新标题作为字符串进行回调。
func newNowPlayingSongTitle(songTitle: String){
delegate?.configureLabels(songTitle)
}
这也很好用,一切都很好。我在 nowPlayingViewController
成功收到回调,我知道这是因为我能够在收到回调时打印歌曲标题,就像这样...
func configureLabels(songTitle: String){
print("song title is \(songTitle)")
//successfully prints the correct song title
}
但是,我的问题是...我不仅需要打印出新歌名,还需要将 nowPlayingViewController
上的 UILabel 文本 属性 设置为等于新歌我在回调中收到的标题。但是,当我尝试这个时...
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"
}
应用程序因 unexpectedly found nil while unwrapping...
而崩溃。显然 UILabel 是 nil,尽管我知道我已经正确设置了它,因为我可以从其他地方设置它的文本 属性。
这个我也试过了,
func configureLabels(songTitle: String){
let view = UIView(frame: CGRectMake(100, 100, 100, 100))
self.view.addSubview(view)
}
查看整个视图本身是否为 nil,它再次崩溃并出现相同的错误,所以确实 self.view
为 nil。
这是怎么回事?我可以从其他地方在此视图中设置标签和图像,但是在 UILabel 上方的插图中,整个视图本身为零。如何解决这个问题?任何建议都会很好,谢谢。
注意:我试过 self.loadView() 但这也没有帮助
我的问题是我的委托被设置为 nowPlayingViewController 的另一个实例,而不是当前在 tabBarController 中的实例。要解决这个问题,而不是
let np = nowPlayingViewController()
ControllerClass.delegate = np
我做到了
let np = self.tabBarController?.viewControllers![1] as! nowPlayingViewController
然后 ControllerClass.delegate = np
UITabBarController 必须这样做的原因是因为嵌入在 tabBarController 中的视图被加载一次然后保持加载状态,因此我需要在 nowPlayingViewController 上设置 UILabels 以获得 nowPlayingViewController 的正确实例,这是嵌入在 tabBarController 中的那个,因为那个实例是被显示的那个。当然希望其他人觉得这很有用!
我在音乐播放应用程序中使用 UITabBarController
。我的 tabBarController
中有 2 个索引,索引 0 (tableViewController
) 和索引 1 (nowPlayingViewController
)。索引 0 包含一个显示歌曲名称的 UITableViewController
,索引 1 包含一个应该显示正在播放页面的 UIViewController
,包括当前播放歌曲和艺术家的标签。
当用户点击 tableViewController
上的单元格(歌曲名称)时,我会做两件事,首先我将所选歌曲的标题提供给第三个 class,ControllerClass
然后我将标签栏移到索引 1,这是正在播放的页面...
//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
}
到目前为止一切顺利。 nowPlayingViewController
是 ControllerClass
的代表。当 ControllerClass
收到新歌名时,它会调用一个委托方法,该方法应该在 nowPlayingViewController
上设置 UILabel 的文本。这是 ControllerClass
中的方法,它使用新标题作为字符串进行回调。
func newNowPlayingSongTitle(songTitle: String){
delegate?.configureLabels(songTitle)
}
这也很好用,一切都很好。我在 nowPlayingViewController
成功收到回调,我知道这是因为我能够在收到回调时打印歌曲标题,就像这样...
func configureLabels(songTitle: String){
print("song title is \(songTitle)")
//successfully prints the correct song title
}
但是,我的问题是...我不仅需要打印出新歌名,还需要将 nowPlayingViewController
上的 UILabel 文本 属性 设置为等于新歌我在回调中收到的标题。但是,当我尝试这个时...
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"
}
应用程序因 unexpectedly found nil while unwrapping...
而崩溃。显然 UILabel 是 nil,尽管我知道我已经正确设置了它,因为我可以从其他地方设置它的文本 属性。
这个我也试过了,
func configureLabels(songTitle: String){
let view = UIView(frame: CGRectMake(100, 100, 100, 100))
self.view.addSubview(view)
}
查看整个视图本身是否为 nil,它再次崩溃并出现相同的错误,所以确实 self.view
为 nil。
这是怎么回事?我可以从其他地方在此视图中设置标签和图像,但是在 UILabel 上方的插图中,整个视图本身为零。如何解决这个问题?任何建议都会很好,谢谢。
注意:我试过 self.loadView() 但这也没有帮助
我的问题是我的委托被设置为 nowPlayingViewController 的另一个实例,而不是当前在 tabBarController 中的实例。要解决这个问题,而不是
let np = nowPlayingViewController()
ControllerClass.delegate = np
我做到了
let np = self.tabBarController?.viewControllers![1] as! nowPlayingViewController
然后 ControllerClass.delegate = np
UITabBarController 必须这样做的原因是因为嵌入在 tabBarController 中的视图被加载一次然后保持加载状态,因此我需要在 nowPlayingViewController 上设置 UILabels 以获得 nowPlayingViewController 的正确实例,这是嵌入在 tabBarController 中的那个,因为那个实例是被显示的那个。当然希望其他人觉得这很有用!