我尝试访问Viewdidload和IBAction之外的某些IBOutlet,并始终获取nil值。在Viewdidload和IBAction中,这些值都可以。我错过了宣布或初始化某些内容的部分吗?IBOutlet在ViewController方法中始终为零,但在IBAction&Viewdidload中没问题
该值在viewdidload()之后被修改,因为viewdidload被称为IBAction。
视图是在故事板中创建的,来自UINavigation控制器。
connection table between ViewController and UIView:
的登录服务器方法由userCredential代表呼吁,如下:
protocol userCredentialDelegate {
func didUpdateCredential (sender:String, credential: Bool?)
}
class userCredential: NSObject {
var delegate:userCredentialDelegate?
// self.delegate = ViewController() removed
func loginServer (name: String, pwd: String) -> Bool {
dispatch_sync(dispatch_get_main_queue())
{
self.delegate?.didUpdateCredential ("login", credential: credentialStatus)
}
}
主控制器:
class ViewController: UIViewController, userCredentialDelegate {
// set the shared instance
let user = userCredential.sharedInstance
@IBOutlet weak var incorrectCredentials: UITextField!
@IBOutlet weak var username: UITextField!
@IBOutlet weak var password: UITextField!
@IBOutlet weak var logButton: UIButton!
@IBAction func logButton(sender: UIButton) {
print (incorrectCredentials?.hidden)
if logButton.titleLabel!.text == "Log Out" {
user.logoutServer()
} else {
user.loginServer(username.text!, pwd: password.text!)
}
}
func didUpdateCredential (sender: String, credential: Bool?) {
switch sender {
case "login":
if credential! {
performSegueWithIdentifier("loginSegue", sender: self)
} else {
incorrectCredentials?.hidden = false
}
default: break
}
if let credentialResponse = credential {
loginStatus = credentialResponse
}
}
var loginStatus: Bool = false {
didSet {
if loginStatus {
incorrectCredentials?.hidden = true // always nil before, now ok
} else {
incorrectCredentials?.hidden = false // always nil before, now ok
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
user.delegate = self
incorrectCredentials.hidden = true // can work here
user.getUserInfo()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
你可以在你的笔尖上做一个截图,你在哪里连接了插座到你的控制器? IBoutlets应该很弱,所以在var之前放弱。 –
tks。已经用弱和附加图片更新了代码。 –
仍然无法正常工作? –