按鈕的 target-action 通常綁定到視圖控制器或持有該視圖的對象,而非視圖本身。視圖(UIView)不處理業務邏輯。
UIApplication → UIWindow → hitTest:withEvent: 找到最內層視圖(按鈕)。UIControlEventTouchUpInside,調用註冊的 target-action。Superview → ViewController → Window → Application。// 在 ViewController 中配置
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
// 或在自定義 View 初始化時,若 View 持有弱引用的 delegate
[button addTarget:self.delegate action:@selector(customView:didTapButton:) forControlEvents:UIControlEventTouchUpInside];self(視圖)註冊 target,視圖被釋放時 target 成為懸空指針。weak 引用 delegate 防止循環保留。