我正在开发一个消息应用程序atm,在ChatView屏幕上,你可以看到用户之间传递的消息线程,我想给予我的用户能够访问他们正在通信的用户的个人资料。
为了进入ChatView类,用户从用户列表中选择另一个用户来发送消息。一旦选择了那个用户,就会在用户初始化的地方转到ChatView。从那个视图中,用户可以选择显示聊天对象姓名的导航标题来查看他们的个人资料。为了将用户数据传递到下一个视图控制器,我做了以下操作:
// line inside another function setting up the navigation title
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 30)
button.titleLabel?.textColor = .white
button.setTitle(self.otherUser.name, for: .normal)
button.addTarget(self, action: #selector(showUserProfile), for: .touchUpInside)
navigationItem.titleView = button
@objc func showUserProfile() {
let viewUserProfile = ViewProfileViewController()
viewUserProfile.user = self.otherUser
navigationController?.pushViewController(viewUserProfile, animated: true)
}
然而,当我尝试这样做时,我得到了这个错误:(11db) Unexpectedly found nil while implicitly unwrapping an Optional value
,在用户配置文件页面上,应用程序关闭。我在这里做错了什么?
1条答案
按热度按时间svmlkihl1#
你的
ViewProfileViewController
是如何定义它的视图的?如果它使用了一个故事板或者nibfile,那么用默认的初始化器加载它是不起作用的。(因为它不加载它的视图,所以它所有的IBOulet都是空的。)从故事板加载视图控制器的常用方法是使用字符串标识符调用故事板的instantiate()方法。
调用
ViewProfileViewController()
的唯一方法是视图控制器在其loadView()
方法中构建视图层次结构。编辑
如果视图控制器是在情节提要中定义的,则需要:
1.使用
func instantiateViewController(withIdentifier identifier: String) -> UIViewController
示例化视图控制器1.呈现视图控制器(模态地,通过推入视图控制器堆栈,或任何适当的方式)
1.实现UIViewController方法
prepare(for:sender:)
。在该方法中,将任何需要传递到目标视图控制器的数据传递给目标视图控制器。