
前回でテーブルが完成したので、メニューの内容をそれぞれ実装していきたいと思います!
簡単なところからやりたいので、まずはよくあるユーザーに評価をお願いするやつを実装したいと思います!
こんな感じのやつですね!
動作環境は下記の通りです!
Xcode Version 12.5 Swift version 5.4
まずはセルごとに処理をわけたいので、セルを選択した時のメソッドを修正します。
編集するファイルはMenuViewController.swiftです。
//セルを選択した時の設定
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Selected! \(self.items[indexPath.row])")
switch indexPath.row {
case 0:
print("アラーム設定画面へ")
case 1:
print("ウォークスルー画面へ")
case 2:
print("シェア")
case 3:
print("評価")
case 4:
print("データリセット")
default:
break
}
}
このメソッドでは選択したセルのインデックス番号が「indexPath.row」で取得できるので、switch文で条件分岐しました。
これでタップしたメニューによって処理をわける事ができました!
では評価してもらう機能を実装しましょう!
一見難しそうですが、StoreKitをインポートしてコードをちょっと書くだけで実装できます!
まずは下記の通りインポートして下さい!
import StoreKit
次にcase3のところに下記コード追加しましょう!
//セルを選択した時の設定
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Selected! \(self.items[indexPath.row])")
switch indexPath.row {
case 0:
print("アラーム設定画面へ")
case 1:
print("ウォークスルー画面へ")
case 2:
print("シェア")
case 3:
print("評価")
//レビューダイアログを表示
if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
SKStoreReviewController.requestReview(in: scene)
}
case 4:
print("データリセット")
default:
break
}
}
これだけでOKです!一度ビルドして確認してみて下さい!
英語で表示された場合はシュミレーターの言語を日本語に変更してみて下さい!
今回はユーザーに評価をお願いする機能を実装しました!
次回は、友達にアプリを教えたい時に簡単にシェアできる機能を実装したいと思います!
import UIKit
import RealmSwift
import StoreKit
class MenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
//Commonクラスのインスタンス生成
let common = Common()
//Cancelボタン
var cancelBtn: UIBarButtonItem!
//テーブルビューの変数
var tableView: UITableView?
//セル用の配列
let items = ["貯金忘れ防止アラーム", "使い方", "シェア", "評価", "データリセット"]
let icons = ["menuIcon-1", "menuIcon-2", "menuIcon-3", "menuIcon-4", "menuIcon-5"]
override func viewDidLoad() {
super.viewDidLoad()
//ナビゲーションコントローラーまわり
//タイトル
self.title = "メニュー"
//背景色
self.navigationController?.navigationBar.barTintColor = UIColor(hex: self.common.main , alpha: 1)
//セーフエリアとの境目の線を消す
self.navigationController?.navigationBar.shadowImage = UIImage()
//フォントの設定
self.navigationController?.navigationBar.titleTextAttributes
= [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont(name: "HiraMaruProN-W4", size: 20)!]
//Cancelボタンを追加
cancelBtn = UIBarButtonItem(title: "Cancel", style: .done, target: self, action: #selector(self.cancelBtnTapped))
cancelBtn.tintColor = UIColor(hex: self.common.white , alpha: 1)
self.navigationItem.leftBarButtonItem = cancelBtn
//テーブルビューの設定
self.tableView = {
let tableView = UITableView(frame: self.view.bounds, style: .plain)
tableView.autoresizingMask = [
.flexibleWidth,
.flexibleHeight
]
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
tableView.register(MenuTableViewCell.self, forCellReuseIdentifier: "Cell")
return tableView
}()
}
//Cancelボタンのメソッド
@objc func cancelBtnTapped() {
//前の画面に戻る
dismiss(animated: true, completion: nil)
}
//テーブルビューまわり
//テーブルビューのセクションの数を設定
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
//テーブルビューのセルの数を設定
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//配列itemsの数になるように設定
return self.items.count
}
//セルの設定
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//カスタムをセルを使えるように設定
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! MenuTableViewCell
//ラベルのテキストとアイコン画像のファイル名を設定
cell.setCell(item: self.items[indexPath.row], iconName: icons[indexPath.row])
//セルの右側に<を付ける
cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
//セル選択時に背景色を変更しない
cell.selectionStyle = UITableViewCell.SelectionStyle.none
return cell
}
//セルを選択した時の設定
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Selected! \(self.items[indexPath.row])")
switch indexPath.row {
case 0:
print("アラーム設定画面へ")
case 1:
print("ウォークスルー画面へ")
case 2:
print("シェア")
case 3:
print("評価")
//レビューダイアログを表示
if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
SKStoreReviewController.requestReview(in: scene)
}
case 4:
print("データリセット")
default:
break
}
}
}
コメントを残す