Notifications in iOS

Posting a Notification First, create a global key before the class definition of the class that is posting the notification Post the notification

Add Share Button to iOS 9 App

@IBAction func export(sender: UIBarButtonItem) { if let data = prepareCSVData() { let filename = getDocumentsDirectory().stringByAppendingPathComponent(“data.csv”) data.writeToFile(filename, atomically: true) let url = NSURL(fileURLWithPath: filename) let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil) if let popoverController = activityViewController.popoverPresentationController { popoverController.barButtonItem = sender } presentViewController(activityViewController, animated: true, completion: nil) } else { NSLog(“Unable to prepare data”) } } The… Continue reading Add Share Button to iOS 9 App

Send Mail with Attachment in iOS

if MFMailComposeViewController.canSendMail() { let data = prepareCSVData() let mailController = MFMailComposeViewController() mailController.mailComposeDelegate = self mailController.setSubject(“Data Export”) mailController.setMessageBody(“Attached is a CSV file containing your data.”, isHTML: false) // Add attachment if let data = data { mailController.addAttachmentData(data, mimeType: “text/comma-separated-values”, fileName: “headaches.csv”) presentViewController(mailController, animated: true, completion: nil) } else { NSLog(“No data”) } }  

Xcode 7 Archive Button Grayed Out

One reason the Archive button may be grayed out in Xcode is that you need to be connected to a device, and choose that device. See here: http://stackoverflow.com/questions/16050640/xcode-product-archive-disabled/18791703#18791703

Published
Categorized as hints Tagged

Scroll to the Bottom of a Collection View

private func scrollToBottom() { let lastSectionIndex = (collectionView?.numberOfSections())! – 1 let lastItemIndex = (collectionView?.numberOfItemsInSection(lastSectionIndex))! – 1 let indexPath = NSIndexPath(forItem: lastItemIndex, inSection: lastSectionIndex) collectionView!.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Bottom, animated: false) }  

Add a UITableView to an Existing ViewController in Xcode

Quick and dirty checklist for adding a table view to an existing view controller: Embed existing view controller in a NavigationController Drag a UITableView object into the view controller in the storyboard Make the UITableView the same width and height as the view controller and set the constraints to all four sides with no margins… Continue reading Add a UITableView to an Existing ViewController in Xcode

Create a Confirmation Alert in iOS with Swift

If you want to present an alert with an option to either take an action or cancel: @IBAction func clearImage(sender: UIBarButtonItem) { let alert = UIAlertController(title: “Clear Canvas”, message: “Are you sure you want to clear the canvas?”, preferredStyle: .Alert) let clearAction = UIAlertAction(title: “Clear”, style: .Destructive) { (alert: UIAlertAction!) -> Void in self.canvas.image =… Continue reading Create a Confirmation Alert in iOS with Swift

Create Navigation Bar Programmatically in Swift

In your UIPopoverPresentationControllerDelegate: func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? { let presentedViewController = controller.presentedViewController let navigationController = UINavigationController(rootViewController: presentedViewController) let dismissButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: “dismissPopover:”) presentedViewController.navigationItem.rightBarButtonItem = dismissButton return navigationController } func dismissPopover(sender: AnyObject) { self.dismissViewControllerAnimated(true, completion: nil) }

Setting Up Delegates in iOS

This is copied from the iOS Apprentice tutorial on raywenderlich.com but I wanted to put it where I could easily access it. These are the basic steps for setting up a delegate pattern between two objects, where object A is the delegate for object B, and object B will send messages back to A. Define a delegate protocol for… Continue reading Setting Up Delegates in iOS

Published
Categorized as tutorials Tagged