[英]UIButton unresponsive when created programmatically and added to uiimageview
[英]Removing UIImageView added programmatically
我知道有很多與此相關的主題,我讀了它們,但無法使其正常工作。 這是我的代碼
@property (strong, nonatomic) UIImageView *advertiseView;
- (void)startShowingImage
{
_advertiseView = [[UIImageView alloc] initWithFrame:CGRectMake(80,320,150,97)];
_advertiseView.image = [UIImage imageNamed:@"blocksAd.png"];
[self.view addSubview:_advertiseView];
NSLog(@"startShowing");
}
- (void)stopShowingImage
{
NSLog(@"stopShowing");
[_advertiseView removeFromSuperview];
}
我把NSLogs
用來檢查這些方法是否被調用,但是第一次顯示后的圖像永遠不會消失。 有什么建議嗎?
如果一定要調用NSLog(@"stopShowing")
,那么我認為您可能遇到的唯一問題將導致removeFromSuperview
無法正常工作,因為您沒有在主線程上進行更改(UI更新)。
嘗試以下方法:
[_advertiseView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
檢查在主線程中是否同時調用了addSubview和removeFromSuperView
檢查startShowingImage僅被調用一次,否則您將擁有永遠無法刪除的孤立UIImageViews
- (void)startShowingImage
{
if (self.advertiseView) {
self.advertiseView = [[UIImageView alloc] initWithFrame:CGRectMake(80,320,150,97)];
self.advertiseView.image = [UIImage imageNamed:@"blocksAd.png"];
[self.view addSubview:self.advertiseView];
NSLog(@"startShowing");
}
}
使用屬性語法,除非您特別需要避免出現問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.