0

I've been searching for hours now and can't find the solution to my problem: I am sending a pushnotification to my app with this php script: http://pastebin.com/9axHdM0t. With it I send additional Information: 'patient' => 'test'. Now I wanted to extract this Information and set a Label to it. I am doing this with this function in the AppDelegate:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
    //extracting the information
    let info = userInfo["aps"] as! Dictionary<String, AnyObject>
    let pat = info["patient"] as! String
    //print(pat) works fine
    //sending the variable to the viewcontroller and triggering the function wich sets the text of the Label
    vc.patientx = pat
    vc.settext(vc.patientx)     
} 

In my Viewcontroller, I'm doing this:

@IBOutlet var patientxy: UITextField!
var patientx:String!
func settext(name: String)
{
    if patientx != nil {
        //print(patientx) works fine.
    patientxy.text = patientx
    AudioServicesPlaySystemSound(1304)
    AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
    }

}

Printing the variable works fine and then setting the label to the var makes the code crash:

fatal error: unexpectedly found nil while unwrapping an Optional value.

What is the problem? This is my AppDelegate: http://pastebin.com/mXC0wL5E and my Viewcontroller: http://pastebin.com/45dTVLZ3.

I've tried many suggested solutions (many from Stack Overflow) like check if the Label is still connected or update the label in a different way, but I can't find a working solution.

Brian
  • 14,610
  • 7
  • 35
  • 43
JDev
  • 124
  • 1
  • 9
  • 1
    Is your `patientxy` nil? – Putz1103 Feb 01 '16 at 18:55
  • 1
    is your outlet connected? – Will M. Feb 01 '16 at 18:56
  • 2
    You should avoid implicitly unwrapped optionals as much as you can, there's no reason `patientx` should be one, you could declare it as a regular optional (`?`). You should also try debugging yourself instead of looking online for solutions, have you tried setting breakpoints to check the values of the variables, or even using `print` statements to print the variables to the console? – EmilioPelaez Feb 01 '16 at 18:56
  • 1
    What line is the error occurring on? – Craig Siemens Feb 01 '16 at 18:56
  • @WillM. read befor comment. I wrote that i've connected the outlet... – JDev Feb 01 '16 at 19:03
  • 1
    Neither of your pastebins show how settext is called. The outlet is only non-nil after viewDidLoad. You can't access outlets until then. – Lou Franco Feb 01 '16 at 19:04
  • @LouFranco yes sorry i updated the code. settext is called with `vc.settext(vc.patientx)` in the AppDelegate and i will try to change the label after the viewDidLoad thanks – JDev Feb 01 '16 at 19:08
  • 2
    You aren't showing your `ViewController` anywhere, so it's view is never loaded, so the textfield is nil. It isn't very clear what you are trying to do from the code you posted, but assuming you have some other instance of `ViewController` that you are creating in your storyboard, you need to access that instance and call the methods on it. The instance you are creating in your app delegate is a separate instance. – dan Feb 01 '16 at 19:17
  • @dan So if you look at my viewcontroller: http://pastebin.com/45dTVLZ3 or my AppDelegate:http://pastebin.com/mXC0wL5E . where should i call the method? i am calling the method in the AppDelegate with `vc.patientx = pat vc.settext(vc.patientx)` – JDev Feb 01 '16 at 19:28
  • 1
    Where you are calling it now is fine, but the `vc` you are calling it on is not the view controller that you have on screen. Every time you call `ViewController()` it creates a new view controller, it doesn't get a reference to the one you have onscreen. You might be able to get a reference to the one onscreen by doing `window.rootViewController` but that would depend on how your storyboard is set up. – dan Feb 01 '16 at 19:33
  • @dan that gives me the error: Instance member 'window' cannot be used on type 'AppDelegate' do i have to set var vc = window.rootViewController in the ViewController and then reference the var in the AppDelegate? – JDev Feb 01 '16 at 19:47
  • 1
    Where are you putting that code? Put a `print(window?.rootViewController)` in your `didFinishLaunchingWithOptions` method and see what it prints – dan Feb 01 '16 at 19:51
  • @dan i put the code just where i had the `var vc = ViewController()`in the appdelegate. Your code prints `Optional()` – JDev Feb 01 '16 at 19:56
  • 1
    @dan So i set `var vc = KardioApp.ViewController` and it gives me an error: `Instance member 'patientx' cannot be used on type Viewcontroller` by the way should i move this to the chat? – JDev Feb 01 '16 at 20:03
  • 2
    Do `var vc = window?.rootViewController as! ViewController` right before you do `vc.patientx = pat` and remove your `vc` property – dan Feb 01 '16 at 20:07
  • @dan Thank you very very very much!!! It worked!!! I wasted many hours on this... -_- You saved me ! Thanks – JDev Feb 01 '16 at 20:24

0 Answers0