0

NOOB question about swift 4 and building iOS applications

I am starting to learn swift for developing an iOS app I have come from the world of .net and C# so I admit I am a bit more used to a different way of development

I have not learnt all the proper terms yet so please be patient.

I am following along to this https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ConnectTheUIToCode.html#//apple_ref/doc/uid/TP40015214-CH22-SW1

Now I have followed the code to drop buttons and labels onto the storyboard

In the guide it talks about use Ctrl + Drag to create references to objects within the .swift files

Doing a ctrl + Drop seems like a pain so what I wanted to do was create the references in the swift file manually

So I dropped a new UILabel and UIButton controls on the storyboard, set Identified values

Then in the swift files I tried to add the controls and an touch event

@IBOutlet weak var labelOne: UILabel!
@IBOutlet weak var buttonOne: UIButton!

@IBAction func setLabelOneText(_ sender: Any) {
    labelOne.text = "Default Text";
}

i.e. I based this on what was placed in the swift file from the Ctrl + Drop code

However nothing worked.

Going back to the sample code, i then updated the working button tap event to also update the text of the label added manually, but it is throwing errors.

Is the only way to reference objects in a swift file to Ctrl + Drop them into the swift file? As coming from a C# world, if I dropped a control onto a WPF app, I would be able to automatically reference it in the code behind file and not have to worry about additional steps to be able to access the control.

I am using the latest version of xcode and swift 4.

Darren Guy
  • 1,123
  • 2
  • 13
  • 39

1 Answers1

2

You want to do this?

@IBOutlet weak var labelOne: UILabel!
     override func viewDidLoad() {
        super.viewDidLoad()

        let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
        labelOne.isUserInteractionEnabled = true
        labelOne.addGestureRecognizer(tap)
    }

    @objc func tapFunction(sender:UITapGestureRecognizer) {

        print("tap working")
    }
ikbal
  • 1,110
  • 12
  • 21
  • Thanks for that. I added in this code, but when I try to run it I get this error immediately when the app starts in simulator Fatal error: Unexpectedly found nil while unwrapping an Optional value on this line labelOne.isUserInteractionEnabled = true – Darren Guy Aug 25 '18 at 20:20
  • This is because `labelOne` is not connected to the label that you have added in your Storyboard. Go to your Storyboard, and select the label, then from the right panel of Xcode select (the Connection Inspector) tab, and under (Referencing Outlets) you'll find (New Referencing Outlet) and an empty dot, drag from that dot to yellow dot on your view controller and select (labelOne). – Mo Abdul-Hameed Aug 25 '18 at 20:36
  • @MoAbdul-Hameed thanks for that. What you are saying, and reading the other question you referred me to, its best to always do the Ctrl + Drag/Drop from the storyboard. thanks. – Darren Guy Aug 26 '18 at 14:38
  • @DarrenGuy If it worked, would you approve the answer? – ikbal Aug 27 '18 at 05:58