私はUIButtonのグリッドを持っています。 「編集」ボタンを押すと、これらの各ボタンの上に削除ボタンが表示されます。このボタンを押すと、ボタン(および関連するデータ)が削除されます。 Appleのホーム画面のようなものです。ボタンを押し続けると、コーナーのXで動くようになります。削除関数を使用してカスタムUIButtonクラスを作成します。
この投稿によると:Subclass UIButton to add a property私はそれぞれのボタンにプロパティを追加するために関連参照を使用できます。私は私のカスタムUIButtonのプロパティとしてUIButtonを追加しようとしましたが、表示されるように見えず、これが正しい方法ではないと感じているようです。ここに私のカスタムボタンがメインです:私は、プログラムのボタンを追加する場所
#import "UIButton+Property.h"
#import <objc/runtime.h>
@implementation UIButton(Property)
static char UIB_DELETEBUTTON_KEY;
@dynamic deleteButton;
- (void)setDeleteButton:(UIButton *)deleteButton {
deleteButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
deleteButton.frame = CGRectMake(100, 100, 50, 50);
objc_setAssociatedObject(self, &UIB_DELETEBUTTON_KEY, deleteButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIButton *)deleteButton {
return (UIButton *)objc_getAssociatedObject(self, &UIB_DELETEBUTTON_KEY);
}
@end
そして、ここです:
//Create a custom button for each custom book doc
for (int i = 0; i < [customBookDocs count]; ++i) {
BookDoc *customBookDoc = [customBookDocs objectAtIndex:i];
NSString *bookTitle = customBookDoc.book.title;
//create a button for each book
CGRect frame = CGRectMake(xCoord, yCoord, 200, 200);
UIButton *bookButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
bookButton.bookDoc = customBookDoc;
[bookButton setFrame:frame];
[bookButton setTitle:bookTitle forState:UIControlStateNormal];
[bookButton addTarget:self action:@selector(bookButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
xCoord += 250;
[self.view addSubview:bookButton];
[self.view addSubview:bookButton.deleteButton];
}
これを行うには、より簡単に、より賢明な方法はありますか?または私は正しい軌道にいるのですか?
...他の誰かがそのことについて言うことは多くを持っているかもしれないが、私はあなたがここにオブジェクトの関連付けを使用する必要があるだろう、なぜわからない:
削除ボタンは実際には設定しません。あなたの連想メソッドは、あなたが設定した削除ボタンをオーバーライドするだけです。 – Joe