TableViewのセルを長押しされた時にアラートを開くような処理について。
まず、TableViewに対して長押しをした場合のデリゲートメソッドは以下です。
このメソッド内にアラートの処理を追加するのですが、iOS7とiOS8では表示の方法が違い、
・iOS7 → UIAlertView
・iOS8 → UIAlertController
を使います。
今回は、アラートで選択したセルにある文字列を
・ペーストボードにコピー
・Safariで検索
をする機能を付けたコードを書きます。
iOS7では iOS8では

といった感じの表示になります。
以下からソースコードです。
------------------------------------------
------------------------------------------
実際の動作は、以下のアプリで実装していますので確認してみてください。

巻数メモ
まず、TableViewに対して長押しをした場合のデリゲートメソッドは以下です。
@IBAction func tableCellLongPressed(sender: UILongPressGestureRecognizer) {
このメソッド内にアラートの処理を追加するのですが、iOS7とiOS8では表示の方法が違い、
・iOS7 → UIAlertView
・iOS8 → UIAlertController
を使います。
今回は、アラートで選択したセルにある文字列を
・ペーストボードにコピー
・Safariで検索
をする機能を付けたコードを書きます。
iOS7では iOS8では

といった感じの表示になります。
以下からソースコードです。
------------------------------------------
// tableViewのデータが入った配列データ
var myItems: NSMutableArray = []
// セルを長押しされた時の処理
@IBAction func tableCellLongPressed(sender: UILongPressGestureRecognizer) {
// 押された位置でcellのPathを取得
let point = sender.locationInView(tableView)
let indexPath = tableView.indexPathForRowAtPoint(point)
if indexPath == nil {
// 長押し位置に対する行数が取得できなければ何もしない
} else if sender.state == UIGestureRecognizerState.Began {
// 長押しされた場合の処理
var selectItems = myItems[indexPath!.row] as String
if objc_getClass("UIAlertController") != nil {
// iOS8の場合はUIAlertControllerを使う
let alertController = UIAlertController()
let firstAction = UIAlertAction(title: "コピー", style: .Default) {
// 選択行の文字列をコピーする
action in self.copyPasteBoard(selectItems)
}
let secondAction = UIAlertAction (title: "Safariで検索", style: .Default) {
// 選択行の文字列をSafariで検索する
action in self.searchSafari (selectItems)
}
let cancelAction = UIAlertAction (title: "キャンセル", style: .Cancel) {
action in self.alertCancel()
}
alertController.addAction (firstAction)
alertController.addAction (secondAction)
alertController.addAction (cancelAction)
//iPad用に位置を指定する
alertController.popoverPresentationController?.sourceView = tableView.superview
alertController.popoverPresentationController?.sourceRect = CGRect(x: (tableView.superview!.frame.width/2), y: tableView.superview!.frame.height, width: 0, height: 0)
alertController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up
presentViewController (alertController, animated: true, completion: nil)
} else {
// iOS7以前の場合はUIAlertViewを使う
var alertView = UIAlertView (title: selectItems, message: "", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "コピー", "Safariで検索")
alertView.show()
}
}
}
// alertView選択時の処理(iOS7以前用)
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
if buttonIndex == alertView.cancelButtonIndex {
// キャンセルされた
} else if buttonIndex == 1 {
// コピーが選択された
copyPasteBoard (alertView.title)
} else if buttonIndex == 2 {
// Safariで検索が選択された
searchSafari (alertView.title)
}
}
// [アラート]ペーストボードに文字列をコピーする
func copyPasteBoard(text: String) {
var pasteBoard = UIPasteboard.generalPasteboard ()
pasteBoard.string = text
}
// [アラート]Safariで文字列を検索する
func searchSafari(text: String) {
let encodeText = text.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet ())
var searchText = "https://www.google.co.jp/#q=" + encodeText!
let url = NSURL(string: searchText)
if UIApplication.sharedApplication ().canOpenURL (url!){
UIApplication.sharedApplication ().openURL (url!)
}
}
// [アラート]キャンセルボタン
func alertCancel() {
// キャンセル選択時は何もしない
}
------------------------------------------
実際の動作は、以下のアプリで実装していますので確認してみてください。

巻数メモ