我试图实现一个简单的事情 - 通过切换UISwitch来切换字符串值。
但是,我可以得到,出了什么问题
func switchIsChanged(interestedIn: UISwitch) {
if interestedIn.on == true {
print("UISwitch is ON")
} else {
print("UISwitch is OFF")
}
}
if self.Gender.text == "male" {
switchIsChanged(self.interestedIn)
}
如有必要,我可以显示整个代码。我只是从Facebook获取数据,了解用户的性别,并根据他或她的性别设置感兴趣的值。
import UIKit
import FBSDKShareKit
import FirebaseDatabase
import FirebaseAuth
import Firebase
class Settings: UIViewController {
@IBOutlet weak var UserImage: UIImageView!
@IBOutlet weak var UserName: UILabel!
@IBOutlet weak var UserSurname: UILabel!
@IBOutlet weak var Gender: UILabel!
@IBOutlet weak var interestedIn: UISwitch!
@IBOutlet weak var GenderofInsterest: UILabel!
var pictureURL : String?
var interest = ""
override func viewDidLoad() {
super.viewDidLoad()
let paramets = ["fields": "email, first_name, last_name, picture.type(large), gender"]
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: paramets)
graphRequest.startWithCompletionHandler({
(connection, result, error) -> Void in
if error != nil {
print (error)
}
if let first_name = result["first_name"] as? String {
self.UserName.text = first_name
}
if let last_name = result["last_name"] as? String {
self.UserSurname.text = last_name
}
if let picture = result["picture"] as? NSDictionary, data = picture["data"] as? NSDictionary, pictureUrl = data["url"] as? String {
self.pictureURL = pictureUrl
let fbUrl = NSURL(string: pictureUrl)
if let picData = NSData(contentsOfURL: fbUrl!) {
self.UserImage.image = UIImage(data: picData)
}
}
if let gender = result["gender"] as? String {
self.Gender.text = gender
}
let people = ProfileClass()
people.profileName = self.UserName.text
people.profileGender = self.Gender.text
people.profileSurname = self.UserSurname.text
people.profilePhotoUrl = self.pictureURL
people.SaveUser()
func switchIsChanged(interestedIn: UISwitch) {
if interestedIn.on == true {
print("UISwitch is ON")
} else {
print("UISwitch is OFF")
}
}
if self.Gender.text == "male" {
switchIsChanged(self.interestedIn)
}
})
}}
答案 0 :(得分:1)
正如上面提到的@ILideTau,你不应该直接使用UISwitch状态,而只是创建一个Bool变量,它将保持你的开关状态,并在你更改didSet {}闭包中的状态时更新UI: / p>
@IBOutlet weak var interestedInSwitch: UISwitch!
var interstedInState: Bool = false {
didSet {
interestedInSwitch.on = interstedInState
}
}
func updateInterstedInState(newValue: Bool) {
interstedInState = newValue
}