私は、弱いプロパティと強いプロパティを含む少しの混乱に直面しています。簡潔にするために、コード全体は含めません。
UIView オブジェクトを返すクラス コンビニエンス メソッドを作成し、サブクラス化の代わりに UIView カテゴリに実装しました。
@implementation UIView (CSMonthView)
+ (UIView *)monthViewFromDateArray:(NSArray *)arrayOfAllShiftsAndEvents withNibOwner:(id)owner selectedDate:(NSDate *)selectedDate withCompletionHandler:(void(^)(CSCalendarButton *selectedButton))block
{ // .. do some stuff
// Create an instance of UIView
UIView *monthView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320.0, 200.0)];
// Create UIButtons and set the passed down 'owner' value, as the target for an
// action event.
// Add UIButton as subviews to monthView....
return monthView;
}
メソッド内には monthView を指すものがないことに注意してください。
CSCalendarViewController というクラスである「所有者」の実装内で、クラス コンビニエンス メソッドを呼び出して上記の UIView を作成し、それを _monthView という UIView プロパティに割り当てます。
@interface CSCalendarViewController : UIViewController
@property (weak, nonatomic) UIView *monthView;
@end
@implementation CSCalendarViewController
__weak CSCalendarViewController *capturedSelf = self;
// Create the current month buttons and populate with values.
_monthView = [UIView monthViewFromDateArray:_arrayOfAllShiftsAndEvents withNibOwner:self selectedDate:_selectedDate withCompletionHandler:^(CSCalendarButton *selectedButton) {
capturedSelf.selectedButton = selectedButton;
[capturedSelf.selectedButton setSelected:YES];
}
今、私の混乱はこれです。プロパティ「monthView」を弱いものとして定義しましたが、「monthView」は返された UIView の値を保持しています。
先に進んで次のようなことをすると:
_monthView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 200.0)];
コンパイラーは、「割り当てられた保持オブジェクトが弱い変数に割り当てられました」という警告を (当然のことながら) 表示します。
クラス メソッドから返される UIView に「monthView」を割り当てたときに同じエラー メッセージが表示されないのはなぜですか?
ARC以前のメモリ管理に関しては、私は深く理解していません。また、明らかな何かが欠けていると思います。ありがとう。