-3

here is what is happening https://i.stack.imgur.com/jK0AF.jpg

yes i've read the article about optional

but that textfield isn't nil or without a value as it does it when i press enter when theres a value in the text-bar i also tried ! and ? with same results

i tried many many things and it wont work always the same crash

can someone explain what is the reason that textfield is hard to access

i tried

var = ViewControler()
acylam
  • 18,231
  • 5
  • 36
  • 45

2 Answers2

0

You need to instantiate the viewController since it looks like it's from a storyboard/xib. If that's the case, you can check this other question: How can I load storyboard programmatically from class?

John D
  • 1,435
  • 2
  • 16
  • 24
  • i read it but this is for swift 3.0 and im using swift 4.0 and none of the nswers suit my problem – amandakrow Nov 20 '17 at 19:28
  • Swift 3.0 and swift 4.0 don't have a lot of difference when it comes to instantiating your storyboard/xib. Are you using storyboard/xib? – John D Nov 20 '17 at 19:29
  • i can't understand why the textfield isnt accessible , i dont get the whole its not loaded part, it is when i run that function in that other class – amandakrow Nov 20 '17 at 19:40
  • 1
    The items inside your ViewController is not loaded because of your storyboard. The Storyboard need to be instantiated in order for the items inside the viewController to actually exist. – John D Nov 20 '17 at 19:47
  • what is the easiest way to do in swift 4.0? – amandakrow Nov 20 '17 at 19:53
0

If you want to call the class function from the viewcontroller that contains the textfield change the class function to have one argument :

func changetextfieldincontroller(_ vc: NSViewController){
   if let vc = vc as? ViewController{ 
     vc.textField.stringValue = "New text"
   }
}

when you call it from the controller make sure to pass self as an argument.

Andras M.
  • 838
  • 7
  • 12