dimanche 30 octobre 2016

Using string for comparison in "if" statement not working (SWIFT)

So I am in the process of developing this app for Mac and I am using Swift. I am just learning + developing the app side by side. I have the main viewController and when you press (+) button, it opens another viewcontroller with textfield. Now i have two buttons, one says "Done" and the other says "Cancel". If you press Cancel the viewController dismisses. However, if you press "Done" then whatever you added in that textfield appears in the first ViewController.

I want to run an if statement on "Done" button. So if you haven't written anything in the textfield and you press done, it gives you an error, and if you have written something, then it allows you to press Done and it dismisses!

I created an IBOutlet for the textfield:

@IBOutlet var weblinklabel: NSTextField!

Then whatever is stored in the weblinklabel is stored in another variable:

weblinklabel.stringValue = done!

Obviously, "done" is an optional string variable, assigned as this:

var done: String? = "" 

Now in my understanding, "done" variable if empty should give the error which in this case is a label that shows itself(which is isHidden = false' in viewdidload()) function initially.

This is the "if-statement" that I am trying to use:

  if done != nil {
            self.view.window?.close()

        }
        else {
            label1.isHidden = false
        }

I believe the placement of this code isn't right. I am placing it under IBAction of the "Done" button.

Any help in this regard, will be appreciated! thanks

Aucun commentaire:

Enregistrer un commentaire