问题描述
我有2个视图控制器,VC1在我的应用程序中显示所有用户的基本信息,当您在某个单元格中按更多信息"按钮时,它会选择一个新的VC,该VC显示用户在您所使用的单元格中的完整个人资料先前按下.
I have 2 view controllers, VC1 displays all the users on my apps basic info, when you press a more info button in a certain cell it segues to a new VC that displays the users full profile that was in the cell that you previously pressed.
在帮助下,我可以进行所有工作,除了如何在VC2上显示结果.我正在尝试在VC2中设置属性.我也尝试过
With help I have everything working except for how to display the results on VC2. I am trying to set a property in the VC2. I also tried for
此代码未返回任何错误,但在VC2上什么也不显示.
This code is returning no errors but displays nothing on the VC2.
我尝试设置,获取willSet,所有内容都无法破解:
I've tried set, get willSet, everything and can't crack it:
var userToShowDetail: PFUser? {
willSet {
self.configureView()
}
}
func configureView() {
// Update the user interface for the detail item.
if let userToShowDetail: PFUser = self.userToShowDetail {
if let label = self.userName {
userName.text = userToShowDetail["name"] as! String?
}
}
}
override func viewDidLoad() {
我尝试了这个,但是它只返回一个空的VC:
I tried this one but it returns nothing but an empty VC:
var userToShowDetail: PFUser?
override func viewDidLoad() {
super.viewDidLoad()
if let appUsers = userToShowDetail as PFUser? {
self.userName.text = appUsers["name"] as? String
self.userInstrument.text = appUsers["instrument"] as? String
self.userAge.text = appUsers["age"] as? String
self.userProfile.file = appUsers["image"] as? PFFile
self.userProfile.layer.cornerRadius = self.userProfile.frame.size.width/2
self.userProfile.clipsToBounds = true
self.userProfile.loadInBackground()
}
我已经研究了didSet和willSet,但是我仍然茫然.任何帮助将不胜感激.有人可以给我指向教程或建议我,下一步是什么.
I have researched didSet and willSet but I am still at a loss. Any help would be greatly appreciated. What is the next step can someone point me to a tutorial or advise me please.
View Controller 1 segue:
View Controller 1 segue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "userProfileDetailsSegue" {
//get the index path for the row that was selected
let indexPath = self.resultsPageTableView.indexPathForSelectedRow!
//get the PFUser object for that particular row
let userToShow = self.appUsers[indexPath.row]
//create your new view controller
let newVc = segue.destinationViewController as! UserProfileDetailsViewController
//assign the new vc's property to your object
newVc.userToShowDetail = userToShow
}
}
推荐答案
当您不熟悉Swift时,这是一个常见错误.
-当您选择VC2并将值传递给"userToShowDetail"时.将调用"userToShowDetail"的"didSet"方法.
-不幸的是,此时VC2的视图仍然为零.因此您会收到此错误.
-解决方法很简单,请添加?"在"didSet"功能中的每个视图之后.并且不要忘记在VC2的viewDidload中调用"didSet"功能.
This is a common mistake when you are newer to swift.
- When you segue to VC2, and pass a value to "userToShowDetail".The "didSet" method of "userToShowDetail" will be called.
- Unfortunately, at this point, your VC2's views are still nil.So you will get this error.
- The solution is simple,add a "?" after each view in your "didSet" func.And don't forget to call "didSet" func in VC2's viewDidload.
func configureView() {
if let userToShowDetail: PFUser = self.userToShowDetail {
if let label == self.userName? {//add "?" to VC2's subviews
userName?.text = userToShowDetail["name"] as! String?//add "?" to VC2's subviews
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
configureView()//call mothed in didSet()
// other thing you need to do
}
这篇关于设置一个属性以更新VC swift上显示的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!