2
我想要做的是爲每個UIButton
創建一個函數,並在函數中使用開關來確定其行爲。我將按鈕上的標籤設置爲使用開關,但實際上並沒有那麼遠。如何在使用ARC時將UIButton保留在內存中?
for var(int i = 0; i < numResults; i++)
{
UIButton* button = [[UIButton] alloc] initWithFrame:CGRectMake(0,(i*55)+10,320,50)];
[buttton setTag:i];
[button addTarget:self action:@selector(buttonHandler) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
...
-(void) buttonHandler:(id)sender
{
//Handle the button press
}
當我點擊任何按鈕的應用程序拋出一個錯誤:
-[WatchViewController buttonHandler]: unrecognized selector sent to instance 0x6845430
我認爲,這是因爲按鈕變量被創建,然後因爲有它的參考,自動釋放通過ARC,所以當函數被調用時它不再存在。不幸的是,我不知道如何保留對內存中每個按鈕的引用。
如果我錯了(或者我寫的東西是不好的練習),隨時告訴我,它會幫助我學習!
非常感謝你,我沒有意識到這是必需的,但現在你告訴我這很有道理。很明顯,我正在完全錯誤地吠叫樹! – mrh89