リターンキーを押したときに定義済みのアクションを持つUITextFieldサブクラスを作成したいが、それ以外の点ではUITextFieldと違いはない。編集:私はいつものようにデリゲートが動作するようにしたい - これを行う方法の1つは、textFieldshouldReturn:
サブクラスと実際のデリゲートに他のメッセージを送信するデリゲートを作成することですが、私は可能な限り1つのオブジェクト。デリゲートを使用せずにUITextFieldが最初のレスポンダである間にリターンキーを押す方法がありますか?
答えて
これは簡単です:
UITextField
サブクラスでは、のターゲットアクションメソッドを追加しますイベント
これで、リターンキーが押されるたびにセレクタが呼び出されます。
サブクラスでtextFieldShouldReturnメソッドをオーバーライドできます。そしてそのクラスは、通常のUItextFieldが行うすべてのことを行います。
UPDATE
@interface MyTextField : UITextField {
}
@end
@implementation MyTextField
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectInset(bounds, 10, 0);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return CGRectInset(bounds, 10, 0);
}
- (void) drawRect:(CGRect)rect{
[super drawRect:rect];
}
-(BOOL)textFieldShouldReturn:(UITextField*)textField {
//write you code here to capture the return key.
//Make sure to change textfield's class from interface builder if you are using interfce builder or initialize by code.
}
@end
私はその代理人ではなく、UITextFieldをサブクラス化したいと思います。 –
サブクラスを作成するときには、リターンキーイベントをキャプチャするメソッドをオーバーライドするだけで済みます。デリゲートをサブクラス化することはできません。 –
@Loyal Tingleyは、コードスニペットに関する私の更新答えを見てください。 –
ソリューションは、あなたのUITextFieldのサブクラスは、あなたのテキストフィールドとその「通常」のデリゲート間で挿入するカスタムプライベートデリゲートを持っていることです。
あなたのプライベートデリゲートは、initWithFrame:とinitWithCoder:メソッドで作成し、デリゲートとして使用します。 UITextFieldのデリゲートプロパティはコンテンツを保持しないため、強い/保持プロパティが必要です。
@interface MyTextFieldPrivateDelegate: NSObject<UITextFieldDelegate>
@end
@interface MyTextField()
@property (nonatomic, strong) MyTextFieldPrivateDelegate *privateDelegate;
@end
@implementation MyTextField
@synthesize privateDelegate;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.privateDelegate = [[MyTextFieldPrivateDelegate alloc] init];
self.delegate = self.privateDelegate;
}
return self;
}
間にプライベートデリゲートを挿入するには、setDelegate:をオーバーライドします。
@interface MyTextFieldPrivateDelegate: NSObject<UITextFieldDelegate>
@property (nonatomic, weak) id<UITextFieldDelegate> delegate;
@end
@implementation MyTextFieldPrivateDelegate
@synthesize delegate;
@end
@implementation MyTextField
- (void)setDelegate:(id<UITextFieldDelegate>)delegate
{
if (delegate == self.privateDelegate) {
[super setDelegate:delegate];
} else {
self.privateDelegate.delegate = delegate;
}
}
@end
あなたのプライベートデリゲートは、カスタムtextFieldShouldReturnを実装します:実装、および他のすべてのUITextFieldDelegate方法
@implementation MyTextFieldPrivateDelegate
- (BOOL)textFieldShouldReturn:(UITextField*)textField {
// ...
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([self.delegate respondsToSelector:@selector(textFieldShouldBeginEditing:)]) {
return [self.delegate textFieldShouldBeginEditing:textField];
}
return YES;
}
// etc.
@end
なぜdownvotes?この「挿入されたプライベートデリゲート」手法は、非常に便利で堅牢です。 –
私はグエンダルは正しい軌道に乗ったと思うので、私は彼女の答えを取り、それを拡大しました。あなたのプロジェクトに以下のコードを追加し、Interface BuilderでクラスをUITextFieldからARTextField(AR = Automatic Resigner)に変更するだけで、目的の動作が得られます。私はまだuserDelegateプロパティの宣言が強いか、弱いべきであるかどうかを判断しようとしている
...
ARTextField.hファイル:
//
// ARTextField.h
//
// Created by Terry Grossman on 3/21/14.
//
#import <UIKit/UIKit.h>
@interface ARTextField : UITextField <UITextFieldDelegate>
@property (nonatomic, strong) NSObject<UITextFieldDelegate> *userDelegate;
@end
ARTextField.mファイル:
//
// ARTextField.m
//
// Created by Terry Grossman on 3/21/14.
//
#import "ARTextField.h"
@implementation ARTextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[super setDelegate:self];
}
return self;
}
-(id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[super setDelegate:self];
}
return self;
}
-(void) awakeFromNib
{
[super awakeFromNib];
[super setDelegate:self];
}
// if user wants a delegate, we stash it.
- (void)setDelegate:(id<UITextFieldDelegate>)delegate
{
if (delegate != self)
{
self.userDelegate = delegate;
}
}
- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
[textField resignFirstResponder];
if (self.userDelegate != nil && [self.userDelegate respondsToSelector:@selector(textFieldShouldReturn:)])
{
return [self.userDelegate textFieldShouldReturn:textField];
}
return NO;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (self.userDelegate != nil && [self.userDelegate respondsToSelector:@selector(textFieldShouldBeginEditing:)]) {
return [self.delegate textFieldShouldBeginEditing:textField];
}
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (self.userDelegate != nil && [self.userDelegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)])
{
return [self.userDelegate textField:textField shouldChangeCharactersInRange:range replacementString:string];
}
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (self.userDelegate != nil && [self.userDelegate respondsToSelector:@selector(textFieldShouldEndEditing:)]) {
return [self.userDelegate textFieldShouldEndEditing:textField];
}
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (self.userDelegate != nil && [self.userDelegate respondsToSelector:@selector(textFieldShouldClear:)]) {
return [self.userDelegate textFieldShouldClear:textField];
}
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (self.userDelegate != nil && [self.userDelegate respondsToSelector:@selector(textFieldDidEndEditing:)]) {
[self.userDelegate textFieldDidEndEditing:textField];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if (self.userDelegate != nil && [self.userDelegate respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
[self.userDelegate textFieldDidBeginEditing:textField];
}
}
@end
- 1. アニメーション中にUITextFieldを最初のレスポンダにすることはできません
- 2. UITextField:最初のレスポンダではないイベントを受信します
- 3. UITextFieldが最初のレスポンダのステータスを辞退しないようにする
- 4. どのメソッドでUITextFieldのデリゲートを設定する必要がありますか?
- 5. CSS3 Transitions:JQueryを使用せずにオンクリックオプションがありますか?
- 6. パッケージをインストールせずにPostSharpを使用する方法はありますか
- 7. UIAlertView内のUITextFieldを最初のレスポンダにすると、キーボードが表示されないのはなぜですか?
- 8. コンストラクタを使用せずに構造体のメンバーを初期化する方法はありますか?
- 9. MediaPlayerを最初から構築せずにカスタムVideoViewプログレスバーを作成する方法はありますか?
- 10. テキストフィールドを最初のレスポンダにする
- 11. シンボリックリンクを最初に削除せずに編集する方法はありますか?
- 12. 他の言語を使用せずにhtml間で値を渡す方法はありますか?
- 13. 最初のページをクロールする前にクッキーが何であるかを知らずに、スクレイピーでクッキーを渡す方法はありますか?
- 14. Resharperにファイルのすべてを修正させる方法はありますか(最初のオプションを使用)
- 15. UITextFieldがアクセサリビューでUITableViewCellに追加された場合、最初のレスポンダになることができない
- 16. ProGuard最適化を使用せずにロギングコールを削除する方法はありますか?
- 17. ユーザーが確認せずにDiscoverable時間をプログラムで設定する方法はありますか?
- 18. jQuery UIを使用せずにjQuery UIウィジェットファクトリを使用するより良い方法はありますか?
- 19. パラメータチューニングとフィーチャエンジニアリング、いずれか最初に行う必要がありますか?
- 20. Xcode 4にmainwindow.xibがありません。TabBarControllerにNavigationControllerを使用する方法が分かりません
- 21. UITextViewが最初のレスポンダになったときを知る方法
- 22. 私は最初のレスポンダiphone xcodeで戻るボタンを使用できますか?
- 23. すべてのUITextFieldのデリゲートを設定する必要はありますか?
- 24. OracleObjectMappingAttributeを使用せずにODP.NETを使用してOracle UDTからマップする方法はありますか。
- 25. WindowsのコマンドラインやIDEを使用せずにJDKツールを使用する方法はありますか?
- 26. 最初にインスタンス化せずにPHPでオブジェクトを使用する
- 27. UITextFieldでテキストを区切る方法はありますか?
- 28. デリゲート用のセッターはインターフェイスにあるはずですか?
- 29. 最初の文字を選択する方法はありますか? :最初の文字が
- 30. イベントのデリゲートを取得する方法はありますか
受諾済み!あなたのセレクタを 'returnPressed:'に編集したいのですが、6文字の制限があると思います。私が 'didPressReturn:'に変更したらどうしたらいいですか? :) –