我有一个storyviewcontroller,它的视图中有对象.我需要更改UILabel上的文本(在storyviewcontroller中)并加载数组上的视图.我已将IBOutlet连接到storyviewcontroller中的标签.
class StoryViewController: UIViewController { @IBOutlet weak var textLabel: UILabel! @IBOutlet weak var inspiredButton: UIButton!
我创建了storyviewcontroller类的一个对象,并且能够访问它的变量.但是,在创建storyviewcontroller的对象之后,IBOutlet是零.因此,我得到一个例外,说明在打开时发现了nil
let story:StoryViewController = StoryViewController() story.textLabel.text = sampleText()
能帮帮我吧!
这是整个项目的链接
https://github.com/abhishekagarwal2301/DTC
谢谢
IBOutlets在视图加载过程中初始化,并且在您尝试访问它们时无法访问它们.相反,您必须向viewcontroller声明一个字符串变量,并在其viewDidLoad方法上将文本设置为label(在加载过程完成后)
class StoryViewController: UIViewController { @IBOutlet weak var textLabel: UILabel! @IBOutlet weak var inspiredButton: UIButton! var text:String! override func viewDidLoad() { super.viewDidLoad() textLabel.text = text } }
并从第一个控制器初始化文本变量如下
let storyboard = UIStoryboard(name: "YourStoryboardName",bundle: nil) var story = storyboard.instantiateViewControllerWithIdentifier("YourVCIdentifier") as StoryViewController story.text = sampleText() self.presentViewController(story,animated: false,completion: nil)