0

iOSアプリにメニューを表示するドロワーコントローラーがあります。このメニューは、各画面で使用できるメニュー ボタン (UIButton) を押すことで切り替えられます。

ここに画像の説明を入力

モックでわかるように、メニュー ボタンには、新しいコンテンツが利用可能であることを示す赤い点を付けることができます。

ここに画像の説明を入力

このドットの「グローバル」プロパティを使用してカスタム UIControl を作成することを考えました。それは正しい方法ですか?

class MenuButton : UIButton {
  static var showNotificationDot : Bool = false
}
4

1 に答える 1

1

たとえば、サブクラス UIButton を作成し、オブザーバーを追加できます。

class MyButton: UIButton {

    static let notificationKey = NSNotification.Name(rawValue: "MyButtonNotificationKey")

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.subcribeForChangingState()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    fileprivate func subcribeForChangingState() {
        NotificationCenter.default.addObserver(forName: MyButton.notificationKey, object: nil, queue: nil) { notificaton in
            if let state = notificaton.object as? Bool {
                self.changeState(active: state)
            }
        }
    }

    fileprivate func changeState(active: Bool) {
        //change ui of all instances
        print(active)
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

そして、次のように任意の場所から UI を変更します。

NotificationCenter.default.post(name: MyButton.notificationKey, object: true)
于 2018-04-28T12:07:23.810 に答える