How to Create Outlets and Actions in Xcode Using Swift - Swift Tips 6

Xcode with the Assistant Editor (Venn Diagram button) let's you connect code and UI (user interface) files together.

In order for you to make your UI interactive, you need to create connections from the UI to your related code files. These connections give Xcode names that you can use in your code file to modify how something looks programmatically or to respond to user input (button tabs, sliders, switches, etc). There are two primary connections you will create in Xcode: outlets and actions.

Outlets

Outlets enable you to give a UI element a name that you can use in your code. You can create a connection from a label to your code file using an outlet.

Outlets will look like:

@IBOutlet weak var textLabel: UILabel!

You can modify properties associated with your UI element, like text and textColor for UILabel objects.

Actions

Actions let you trigger things that can happen in response to user input. When a user moves a slider, enters a date, or types their name you can run a chunk of code to respond to it.

Actions will look like:

@IBAction func buttonPressed(sender: AnyObject) {

    print("hi developer")

    textLabel.text = "you pressed me "
}

Creating Outlets and Actions

  1. Open the Assistant Editor
  2. Right-click and drag from your UI element (i.e. label) to the code file
  3. Xcode auto inserts code for you to create the name and connection

Variables and Properties

You can also create variables and properties in your Swift code files. For ViewController.swift files it is easier to initialize variables and properties when you declare them, or in your viewDidLoad() method.

Swift Tips Source Code

Grab the source code for this Swift Tip and many more at: http://SuperEasyApps.com/SwiftTips

3 Common Crashes for Beginner iPhone Developers in Xcode using Swift

3 common crashes for beginner iphone developers in xcode using swift

As you work with your first iPhone app you will inevitably run into different crashes. The three most common crashes are what every beginner encounters. If you know what to look for, you can avoid these in the future.

Two of the crashes are real crashes and one is because you clicked and mistakenly added a breakpoint, which is used for finding bugs.

Here are the three crashes and how to fix them:

  1. Breakpoint Crash

    • Problem: Thread 1: breakpoint 1.1
    • Why: You clicked in the gutter left of your code and added a blue flag.
    • Solution: Delete or disable the breakpoint (blue flag) from the code gutter.
  2. Storyboard IBOutlet Crash

    • Problem: Thread 1: signal SIGABRT
    • Console:

      Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x7fbd74a090d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key spellingMistakee.'

    • Why: You deleted an Outlet/Action connection in code or renamed it (i.e. spelling mistake).

    • Solution: Right-click on the UI element and remove the old Outlet/Action connection mentioned as the key in the top of the Console output.
  3. Nil Optional Crash

    • Problem: Thread 1: EXC_BAD_INSTRUCTION
    • Console:

      fatal error: unexpectedly found nil while unwrapping an Optional value

    • Why: You removed a UIView IBOutlet connection and then used the IBOutlet variable in code. The variable has no value until you reconnect it to the UI element.

    • Solution: Right-click and drag from the UI element to the IBOutlet property that Xcode stopped at with the EXC_BAD_INSTRUCTION.

Logitech c930 vs Logitech c920 web camera review - Amazing web cameras for online teaching on Teachable

If you teach online courses and want a camera with an incredible focus distance for detailed work, you need to get the c930 from Logitech.

Focal distance is insane compared to my Canon 6D DSLR (30x the price!) with the c930. And it auto focuses super fast.

It's a super wide angle lens and it gives you more creative freedom to get close to your work with a configurable mini-tripod mount from Joby.

Watch the c930 in action

Logitech c930 super close auto focus and wide angle lens.

Both cameras are 1080p and look stellar with auto focus and can focus within ~2 cm. Super FUN!

And if you teach online, make sure you use Teachable.com to host all your videos + payment with Paypal + Credit Cards!  

It's so easy, so much better than Skillshare or Udemy!

Bug alert: sometimes I've noticed the c930 will bug out and skip frames... close the app using it and reopen the app on Mac and it seems to get normal again