2014-01-17 31 views
0

我用下面的代碼來顯示一個UIAlertView中UIAlertView中removeFromSuperview不起作用

UIAlertView * messageBox = [[UIAlertView alloc] initWithTitle: @"title" 
                 message: @"" 
                delegate: nil 
              cancelButtonTitle: @"Cancel" 
              otherButtonTitles: nil]; 



[messageBox show]; 

然後我用一個NSNotification postNotificationName觸發並希望刪除或釋放UIAlerView

-(void)notificationEntry:(NSNotification *)notification{ 

    if (messageBox) { 
     //[messageBox removeFromSuperview];//this does not work neither 
     [messageBox performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO]; 
     messageBox = nil; 

    } 
} 

通知被觸發,但是

[messageBox removeFromSuperview]; 

[messageBox performSelectorOnMainThread:@selector(removeFromSuperview); 

無作品

您的評論歡迎

+0

我想知道,爲什麼你想自動刪除警報看法?對不起,但也許有更好的方法來完成你想要完成的。 –

+0

它看起來像你正在聲明一個局部變量UIAlertView * messageBox,然後通過相同的名稱訪問伊娃。 – Bamsworld

+0

U不能從超級視圖中刪除它。你必須用** dismissWithClickedButtonIndex:animated:**調用該實例來解僱它。 – CoolMonster

回答

4
+0

這個。特別是在iOS-7中,通過嘗試觸及其視圖層次結構來消除警報視圖*強烈*不鼓勵,並且通常不起作用。將UIAlertView視爲可通過調用方法和設置屬性影響的黑盒對象,* not *作爲可像其他視圖一樣操作的View。 –