Contact Us

Use the form on the right to contact us.

You can edit the text in this area, and change where the contact form on the right submits to, by entering edit mode using the modes on the bottom right. 

Oak Ridge, TN, 37830
United States


Filtering by Tag: Swift

Code a Navigation Button

Wade Cantley

For some situations a navigation button just isn't going to work in the storyboard.  For this, I have found that I need to add a button programatically.  Here are the steps.

1) Create a Receiving function that the button will trigger. This is the equivalent of an IBAction except without the "IBAction" part.

2) Put the button code in the ViewDidLoad() method.

Step 1)  For this example, this function is supposed to kick of the refresh of onboarding.  I took the body out to indicate that it has nothing to do with the button part.  

func onBoardingRefresh(sender: UIButton) {
        //Do something here.

Step 2)  Now we create the button, and put it into a barButtonItem.  Note that I already have an image named "InfoButton" in the system.  This name is whatever name is on the image you want to use.

override func viewDidLoad() {

// Setup the button object and give it the properties it needs
var onboardingButton: UIButton = UIButton()
            onboardingButton.setImage(UIImage(named: "InfoButton"), forState: .Normal)
            onboardingButton.frame = CGRectMake(0, 0, 22, 22)
            onboardingButton.addTarget(self, action: "onBoardingRefresh:", forControlEvents: UIControlEvents.TouchUpInside)
	//Setup the ButtonBarItem class and add the button to it and indicate where.
            var rightItem:UIBarButtonItem = UIBarButtonItem()
            rightItem.customView = onboardingButton
            self.navigationItem.rightBarButtonItem = rightItem


How to get currently displayed image name from UIImageView

Wade Cantley

I want to get the image name which is currently displayed at UIImageView but can't find a means to get the image name.

As a work around, for images that I need to reference at a later time, I use the restoration ID to store the image name.

I used restoration ID in this way so that I could connect multiple buttons to the same @IBAction and identify them based on the image name stored in the restoration ID and run logic about what I want to display or hide.

There might be better ways but this worked in a pinch.

I put the image name in as the restoration ID.

Here is where I designate the file for the image..

And I just copy that and put it in as the restoration ID.

(note: that is not what this was intended to be used for as it is really meant for customizing state reference but if that is not relevant to the purpose of your view, then it should work fine.)

Referenced in code when the button is selected.

//Connected to several onboarding buttons.
@IBAction func onBoardingButton(sender: UIButton) {


ID printed out.

You can also tag your images and keep the reference to those images via the tag.

nd the reference is just as easy.

@IBAction func onBoardingButton(sender: UIButton) {


hile it doesn't seem like we can discern what file was used to fill the imageview (that I know of and based on a little looking around myself) attaching hard references to a view (image, button, etc..) allows me to make the connection code side and figure out which image (or in my case button) is being used.

How To put CLLocationCoordinate2d into CLLocation for Swift

Wade Cantley

I have a method I want to call however when I get back the center of the map, it is in CLLocationCoordinate2D type.

How do I put the results of CLLocationCoordinate2D into CLLocation?

When mapView changes region, get the Lat and Lon from CLLocationCoordinate2D and create a CLLocation variable with the lat and lon passed in.

func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool){

    var centre = mapView.centerCoordinate as CLLocationCoordinate2D

    var getLat: CLLocationDegrees = centre.latitude
    var getLon: CLLocationDegrees = centre.longitude

    var getMovedMapCenter: CLLocation =  CLLocation(latitude: getLat, longitude: getLon)

    self.lastLocation = getMovedMapCenter


How To Add Text to an Image

Wade Cantley

func textToImage(drawText: NSString, inImage: UIImage, atPoint:CGPoint)->UIImage{

    // Setup the font specific variables
    var textColor: UIColor = UIColor.whiteColor()
    var textFont: UIFont = UIFont(name: "Helvetica Bold", size: 12)!

    //Setup the image context using the passed image.

    //Setups up the font attributes that will be later used to dictate how the text should be drawn
    let textFontAttributes = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: textColor,

    //Put the image into a rectangle as large as the original image.
    inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height))

    // Creating a point within the space that is as bit as the image.
    var rect: CGRect = CGRectMake(atPoint.x, atPoint.y, inImage.size.width, inImage.size.height)

    //Now Draw the text into an image.
    drawText.drawInRect(rect, withAttributes: textFontAttributes)

    // Create a new image out of the images we have created
    var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()

    // End the context now that we have the image we need

    //And pass it back up to the caller.
    return newImage


To call it you just pass in an image.

textToImage("000", inImage: UIImage(named:"thisImage.png")!, atPoint: CGPointMake(20, 20))

The following links helped me get this straight.

Swift - Drawing text with drawInRect:withAttributes:

How to write text on image in objective-c iPhone?

The original goal was to create a dynamic image that I could use in an AnnotaionView such as putting a price at a given location on a map and this worked out great for it. Hope this helps someone trying to do the same thing.

Coredata CRD

Wade Cantley

Be sure that project uses core data so that many of the delegate functions are pulled into AppDelegate.

1) Import the framework
2) setup the entity reference
3) Read with a predicate (search param)
4) Insert
5) Delete
6) Save the changes

1) Import the framework

import CoreData

2) setup the entity reference

This is used under every circumstance of coredata.

Here we are setup to interface with the entity (table) called “Users” which contains two text fields “username” and “password"

// Setup this variable as the appDelegate
        var appDel = (UIApplication.sharedApplication().delegate as AppDelegate)
        // Setup the managed object context
        var context = appDel.managedObjectContext

3) Read with a predicate (search param)

let myEntity: NSString = "Users"
var request = NSFetchRequest(entityName: myEntity)
request.returnsObjectsAsFaults = false
// Setup the predicate for the request which is what we are searching by.
// in this example we are searching the field “username” for records that have “chris” in it.
// Using a predicate is optional.  You could read all if you wanted to.
request.predicate = NSPredicate(format: "username = %@", “chris")

// Now execute, and put the results into a variable
var results = context.executeFetchRequest(request, error: nil)

// This gets the first object in the set of results which is at position 0
var res = results[0] as NSManagedObject
// Now we display it

4) Insert

// Setup the reference to the database object to be managed
let myEntity: NSString = "Users"
        var newUser = NSEntityDescription.insertNewObjectForEntityForName(myEntity, inManagedObjectContext: context) as NSManagedObject

// Change the fields in the target database object
newUser.setValue(“chris", forKey: "username")
newUser.setValue(“mypassword", forKey: "password")
// Save (without error catching)

5) Delete

This starts like a read, but eventually we loop through the records, find the ones that have the value we are looking

for and then delete the record.

// setup request
let myEntity: NSString = "Users"
var request = NSFetchRequest(entityName: myEntity)
request.returnsObjectsAsFaults = false

// In this example we are searching for all records that have “chris” in them.
request.predicate = NSPredicate(format: "username = %@", “chris")

// execute the fetch and put it into a variable.
var results = context.executeFetchRequest(request, error: nil)

// Loop over the results and delete each object that came back.
for res in results{
     context.deleteObject(res as NSManagedObject)

// Save (without error catching)