我想在我的目标视图控制器的配置文件按钮上显示图像,但是当我尝试从源视图控制器分配图像时,它正在崩溃,因为我的“proficPicButton”为零。
let cell = tableView.cellForRow(at: indexPath) as! FacebookTableViewCell
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let objFriendsDetailViewController = storyBoard.instantiateViewController(withIdentifier: "FriendsDetailViewController") as! FriendsDetailViewController
objFriendsDetailViewController.friendsName = cell.friendsName.text! //Can Assign Data to the property "friendsName"
objFriendsDetailViewController.remindersDataDict = ["birthDay":cell.friendsBirthDay.text!] //Can Assign Data to the property "remindersDataDict"
let image = cell.profilePicButton.image(for: .normal)
print(objFriendsDetailViewController)
print(objFriendsDetailViewController.remindersTableView) //It says nil
print(objFriendsDetailViewController.profilePicButton) //It says nil
objFriendsDetailViewController.profilePicButton.setImage(image, for: .normal) //Can't Assign Image to the property "profilePicButton"
self.navigationController?.pushViewController(objFriendsDetailViewController, animated: true)
如何将源视图控制器中的图像设置为目标视图控制器中的按钮?
答案 0 :(得分:0)
触发push
时,目标视图控制器的UI组件未设置。 UI组件的实际位置是设置模式,是视图控制器的viewDidLoad()
。因此,您尝试在UI实际从未设置转换为设置状态之前设置图像。并且您的Button
是隐式展开的可选项,它告诉您,如果在初始化之前访问 崩溃,或者在值为nil 时访问
现在,这不是实现您尝试的方式。实际的方法是:
viewDidLoad()
)时,您可以使用该属性更新相应的视图答案 1 :(得分:0)
let image = cell.profilePicButton.image(for: .normal)
print(objFriendsDetailViewController)
print(objFriendsDetailViewController.remindersTableView) //It says nil
print(objFriendsDetailViewController.profilePicButton) //It says nil
//These are nil because Controller from storyboard does come into memory yet
objFriendsDetailViewController.imageFromPreviousButton = image
self.navigationController?.pushViewController(objFriendsDetailViewController, animated: true)
相反,你可以做到
Class FriendsDetailViewController: UIViewController {
var imageFromPreviousButton = UIImage()
@IBOutlet var profilePicButton: UIButton!
Override func viewDidLoad() {
profilePicButton.setImage(imageFromPreviousButton, for: .normal)
}
}