3

次のコードのようなものを使用して、UIControl を拡張するカスタム ビューに追加された UITextFields を見つけてから、それらに対して rejectFirstResponder を呼び出してキーボードを閉じたいと思いますが、XCode コンパイラはこれを許可せず、"予期しないインターフェイス名 UITextField。式が必要です。" ここで私の目的を達成するための最良の方法は何ですか?

@interface MyCustomView : UIControl
@end

@implementation MyCustomView

   - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
   {
        /* Dismiss the keyboard associated with any UITextFields in this view */
        for (id subview in self.subviews) {
            if ([subview isKindOfClass: UITextField] ||
                [subview isMemberOfClass: UITextField]) {
                [subview resignFirstResponder];
            }
        }
    }

@end
4

3 に答える 3

3

UITextField クラスを取得するには、次の手順を実行する必要があります。

if ([subview isKindOfClass:[UITextField class]]
于 2012-12-18T02:37:47.497 に答える
1

if ([subview isKindOfClass:[UITextField class])クラスに当てはまり、UITextFieldサブクラスです。

if ([subview class] == [UITextField class])UITextFieldクラスのみに当てはまります。

必要に応じて、いずれかを使用できます。

于 2012-12-18T02:54:19.987 に答える