为什么在创建实例时我的IBOutlet连接的vars没有实例化?

问题描述 投票:0回答:2

在实例化之后,我将一些全局变量设置为等于类的参数。该类定义如下:

import Foundation
import UIKit

class LogIn : UIViewController{

    var test:Int = 1

    @IBOutlet var login_icon: UIImageView!

    @IBOutlet var username_field: UITextField!
    @IBOutlet var password_field: UITextField!
    @IBOutlet var login_button: UIButton!
    @IBOutlet var createaccount_button: UIButton! 

}

全局变量实例化为(在viewDidAppear方法中):

func showLoginView(){
    self.startup_animation.image = self.seq_6
    let login_view = LogIn()
    self.login_icon = login_view.login_icon.image //Doesnt work
    self.username_field = login_view.username_field //Doesnt work
    self.password_field = login_view.password_field //Doesnt work
    self.login_button = login_view.login_button //Doesnt work
    self.createaccount_button = login_view.createaccount_button  //Doesnt work
    self.int_testval = login_view.test //Works  
}

实例化后所有IBOutlets连接的变量都是nil值,但var test实例化得很好。为什么是这样?

谢谢!

附加上下文使用viewDidAppear函数调用showLoginView函数。全班看起来像这样:

class StartupViewController: UIViewController {

var images: [UIImage]!

var animatedImage: UIImage!

@IBOutlet var startup_animation: UIImageView!
@IBOutlet var startup_text: UIImageView!

var login_icon: UIImage!
var username_field: UITextField!
var password_field: UITextField!
var login_button: UIButton!
var createaccount_button: UIButton!


var seq_1 : UIImage!
var seq_2 : UIImage!
var seq_3 : UIImage!
var seq_4 : UIImage!
var seq_5 : UIImage!
var seq_6 : UIImage!
var seq_7 : UIImage!
var seq_8 : UIImage!

var rec_1 : UIImage!
var rec_2 : UIImage!
var rec_3 : UIImage!
var rec_4 : UIImage!
var rec_5 : UIImage!
var rec_6 : UIImage!

override func viewDidLoad() {
    super.viewDidLoad()

}

override func viewDidAppear(_ animated: Bool) {

    super.viewDidAppear(animated)

    seq_1 = UIImage(named: "Eceipt_seq1")
    seq_2 = UIImage(named: "Eceipt_seq2")
    seq_3 = UIImage(named: "Eceipt_seq3")
    seq_4 = UIImage(named: "Eceipt_seq4")
    seq_5 = UIImage(named: "Eceipt_seq5")
    seq_6 = UIImage(named: "Eceipt_seq6")
    seq_7 = UIImage(named: "Eceipt_seq7")
    seq_8 = UIImage(named: "Eceipt_seq8")

    images = [seq_1 ,seq_2, seq_3, seq_4, seq_5, seq_6, seq_7, seq_8]

    animatedImage = UIImage.animatedImage(with: images, duration: 0.8)

    startup_animation.image = animatedImage

    let transition_text = UIImage(named: "Eceipt_logo")

    UIView.transition(with: self.startup_text, duration: 3.0, options: .transitionCrossDissolve,
                      animations: {
                        self.startup_text.image = transition_text
    },
                      completion: { (finished: Bool) -> () in
                        self.showLoginView()
    })

}

func showLoginView(){
    self.startup_animation.image = self.seq_6
    let login_view = LogIn()
    self.login_icon = login_view.login_icon.image
    self.username_field = login_view.username_field
    self.password_field = login_view.password_field
    self.login_button = login_view.login_button
    self.createaccount_button = login_view.createaccount_button
}
swift xcode instantiation iboutlet
2个回答
1
投票

我认为你误解了@IBOutlet的使用。

@IBOutlet只是意味着它是xib或storyboard文件中视图或其他对象的出口。

你的观点是nil不是因为它们是出口,而是因为它们是隐式展开的选项,如果你不给它一个值,那就是nil

test不是零,因为1)它不是一个可选类型,2)它的值为1

我建议你将@IBOutlets放在UIViewControllerUIView子类中,并将storyboard / xib文件中的相应视图连接到它。这样,它们将在调用viewDidLoad方法或awakeFromNib方法时初始化。

编辑:

看起来你已经控制了+将视图拖到了LogIn类。在这种情况下,请检查插座左侧的圆圈是否已填满。如果是,那意味着它们已正确连接。你只需要等到awakeFromNib被召唤。如果不是,请尝试再次连接它们。或者,只需将所有插座直接连接到视图控制器,这样可以省去很多麻烦。

编辑2:

既然我明白你要做什么,这就是你应该做的。你似乎想要呈现LogIn VC。因此,不是将每个视图分配给self,而是使用present方法正确显示它。

let vc = UIStoryboard.main!.instantiateViewController(withIdentifier: "Login")
present(vc, animated: true, completion: nil)

请记住在故事板中为登录VC提供故事板ID“登录”。


1
投票

我认为问题是你通过做LogIn来实例化let login_view = LogIn()视图控制器。当您使用空的初始化程序实例化视图控制器时,每个IBOutlet都将为nil,因为视图控制器不是从故事板(或xib)中“获取”的。如果要使用iboutles,则必须从故事板或xib实例化视图控制器;在您的情况下(使用故事板),您应该通过执行以下操作来实例化它:

let storyboard = UIStoryboard(name: "Main", bundle: nil) // name is the filename of the storyboard that contains the vc
let vc = storyboard.instantiateViewController(withIdentifier: "LogIn") as! LogIn // you can cast to have the exact view controller type
present(vc, animated: true, completion: nil)

确保故事板中的视图控制器具有标识符“LogIn”。

© www.soinside.com 2019 - 2024. All rights reserved.