0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Alertを起動中1回のみ表示・二度と表示しない機能

Last updated at Posted at 2020-10-28

ダイアログによくある選択肢

パターン①OK
--押下後、起動中はもう表示しない。次回起動したら、また表示する

パターン②二度と表示しない
--押下後、もう表示しない再起動しても表示しなくなる。
 アプリをアインインストール、再度インストールしたら、また表示する

それぞれを実装してみましたのでメモしておきます。

アプリ起動、ダイアログのOKの押下後、FlagをTrueにして起動中に表示させなくなるための準備

DialogHelper
class DialogHelper {
    
    static let shared = DialogHelper()
    var dialogDidSee = false
    
    func getDialogStatus()->Bool{
     
        return dialogDidSee
    }
    
    func setDialogStatus(_ flag:Bool){
        
        dialogDidSee = flag
    }
}

アンインストールするまで二度と表示させないため
アプリ終了後も消えさせないデータ、ユーザの設定のデータを保存するUser Defaultsの準備

UserDefaultsKey
enum UserDefaultsKey: String {
    
    case dialogNoMore = "dialog_neverAgain"
    
    func initalValue<T>()-> T?{
        switch self{
        case .dialogNoMore:
            return false as? T
            
        default:
            return nil
        }
    }
}

ViewControllerでこのようにAlert作成
Alertを出すべきかどうかを判断、の二つのメソッドを作ります。

ViewController
    func showDialogIfNeed(){
        
        let didSee = DialogHelper.shared.getDialogStatus()
        let neverSee = UserDefaults.standard.bool(forKey: UserDefaultsKey.dialogNoMore.rawValue)
        
        if neverSee {
            return
        }else{
            if didSee {
                return
            }else {
                createDialog()
            }
        }
    }

    func createDialog(){
        
        let alert:UIAlertController = UIAlertController(title: "Here is a message for you!", message: "", preferredStyle: .alert)
        
        let okAction:UIAlertAction = UIAlertAction(title: "OK", style: .default, handler: {
            (action:UIAlertAction!) -> Void in
            DialogHelper.shared.setDialogStatus(true)
        })
        
        let neverAgainAction:UIAlertAction = UIAlertAction(title: "Never remind me", style: .default, handler: {
            (action:UIAlertAction!) -> Void in
            UserDefaults.standard.set(true, forKey: UserDefaultsKey.dialogNoMore.rawValue)
        })
        alert.addAction(okAction)
        alert.addAction(neverAgainAction)
        
        present(alert, animated: true, completion: nil)
    }
ViewController
override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        showDialogIfNeed()
    }

動いてみて試したら、OKボタンとNeverAgainボタンがちゃんと動作しました。

ちなみにこのようなLayoutを作りました。
viewDidLayoutSubviews()を忘れずに気をつけよう。

ViewController
let label = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.brown
        label.text = "Please see the Dialog"
        label.textAlignment = .center
        view.addSubview(label)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        label.frame = view.bounds
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        showDialogIfNeed()
    }
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?