I don’t think you’re really supposed to do this since the option is grayed out in Xcode when you select Editor -> Embed in Navigation Controller, but a hacky way to get around it is to control-drag from a free-standing Navigation Controller to the Tab Bar Controller and select “root view controller.”
Add Tab to Tab Bar Controller
Control-click from the Tab Bar Controller to the controller you want to be the new tab and select “view controllers.”
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”) } }
Prevent “Array index out of range” Error in Swift
If you don’t know if the array index you are calling exists, here is a nice way to just return nil instead of an error. I grabbed this from StackOverflow but unfortunately lost the link. I am putting it here because I use it once in a while and get tired of searching for it.… Continue reading Prevent “Array index out of range” Error in Swift
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
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) }
NSDateFormatter Styles
Here is a great guide for how to format the string for the dateFormat method (like “yyyy-MM-dd”): http://www.codingexplorer.com/swiftly-getting-human-readable-date-nsdateformatter/
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