gpt4 book ai didi

ios - UISwitch 无法正常工作

转载 作者:行者123 更新时间:2023-11-29 00:55:41 24 4
gpt4 key购买 nike

我正在尝试实现一件简单的事情 - 通过切换 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 获取数据,了解用户的性别,并根据他或她的性别设置 interestedIn 值。

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)

}


})



}}

最佳答案

正如@ILideTau 上面提到的,您不应该直接使用 UISwitch 状态,而是创建一个 Bool 变量,它将保存您的开关状态,并在您在 didSet { } 闭包中更改其状态时更新 UI:

@IBOutlet weak var interestedInSwitch: UISwitch!

var interstedInState: Bool = false {
didSet {
interestedInSwitch.on = interstedInState
}
}
func updateInterstedInState(newValue: Bool) {
interstedInState = newValue
}

关于ios - UISwitch 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37633039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com