私はiOS 5のARCを知っていますが、今はiOS 5以前のコードスタイルを開発しています。UITextFieldを持っている私のカスタムアラートビューの少し複雑なリリースの問題
私の唯一の目標は、UITextFieldで非常に便利なカスタムアラートビューを作成することです。
私は多くの機能を持つ 'BigView'ビューを持っています。そして、それは、おそらく、そのビューでディスプレイ上の多くの異なる状況に対して多くのUIAlertViewを生成する可能性があります。だから私は各アラートビューのUIAlertViewDelegateを使用する方法を知っていますが、実験的にUIButtonの 'addTarget'(実際にはUIControlのメソッド)のようにしたいと考えています。
簡単に言えば、
これは、電子メールの収集のためのボタンで発射「BigView」クラスと私の「TextAlert」インスタンスの一部です。
BigView.m
- (void)emailFeedback:(id)sender
{
TextAlert *textAlert = [[TextAlert alloc] initWithTitle:@"Enter your email address"];
[textAlert setTarget:self action:@selector(textAlertInputed:)];
// [textAlert release];
}
- (void)textAlertInputed:(NSString *)text
{
NSLog(@"text alert inputed, text: %@", text);
}
、これらは私のTextAlertファイルいっぱいです。
TextAlert.h
#import <Foundation/Foundation.h>
@interface TextAlert : NSObject <UIAlertViewDelegate>
{
UIAlertView *alertView;
UITextField *textField;
id target;
SEL action;
}
- (id)initWithTitle:(NSString *)title;
- (void)setTarget:(id)target action:(SEL)action;
@end
TextAlert.m
#import "TextAlert.h"
@implementation TextAlert
- (id)initWithTitle:(NSString *)title
{
if (self = [super init])
{
alertView = [[UIAlertView alloc] initWithTitle:title message:@"beneath" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
[alertView setTransform:myTransform];
[textField setBackgroundColor:[UIColor whiteColor]];
[alertView addSubview:textField];
[alertView show];
}
return self;
}
- (void)dealloc
{
[alertView release]; alertView = nil;
[textField release]; textField = nil;
[super dealloc];
}
- (void)setTarget:(id)_target action:(SEL)_action
{
target = _target;
action = _action;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[target performSelector:action withObject:textField.text];
}
@end
あなただけのコメント部分の完全なコードを見ることができるように、私の主な問題は、「BigView」でTextAlertインスタンスのリリースポイントです上記。もちろん、そのコメントを取り除くと、割り当て解除されたメソッドの呼び出しのためにクラッシュしました。
また、私はautoreleasedとしてtextAlertインスタンスを作成するエラーを取得します。
私の場合、唯一の解決策は、 'BigView'の 'textAlert'オブジェクトを 'BigView'のメンバーではなくローカルオブジェクトにすることです。しかし、その場合、このための便利で軽量なアプローチの私の最初の目標は満足できないと私は思う。また、 'BigView'にはすでにメンバーインスタンスが多数あるため、これ以上追加する必要はありません。
どのようなご提案ですか?または、この試みのためのコメントを歓迎します。私は が本当に私の不十分なコードに反論しているのを聞く準備ができています。すべてはあなたのリリースの問題を除いて動作する場合は、事前に
おかげで、
MK
私はUIAlertViewから継承し、NSObjectから継承してはならないと思います。initWithTitleメソッドをオーバーライド/追加してください。 – CarlJ
@meccan "UIAlertViewクラスはそのまま使用され、サブクラス化をサポートしていません。クラスはプライベートであり、変更する必要はありません。 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html – joerick
@joerick thx、私の間違いのためにsry! – CarlJ