重新加载视图后删除子视图/ UILabel

时间:2013-11-19 19:47:28

标签: ios json reachability

只要没有互联网连接(使用Reachablity),我就会在我的视图中添加UILabel。

UILabel *pullLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 290, 30)];
pullLabel.text = @"Pull down to refresh...";
pullLabel.textAlignment = NSTextAlignmentCenter;
self.view addSubview:pullLabel];

我希望在视图刷新后删除标签(使用uirefreshcontrol)。 我尝试添加removeFromSuperview并隐藏标签,但我仍然无法让它工作。

if ([self.refreshControl isRefreshing]) {
   [self.pullLabel removeFromSuperview];
   [self.refreshControl endRefreshing];
}

我的viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    Reachability *reach = [Reachability reachabilityWithHostname:@"www.soleresource.com"];

    reach.reachableBlock = ^(Reachability *reachability)
    {
        [self.view setNeedsDisplay];

        [[self navigationItem] setPrompt: nil]; // Hide navigation prompt
        [self.pullLabel removeFromSuperview];
    };

    reach.unreachableBlock = ^(Reachability *reachability)
    {
        self.navigationItem.prompt = @"No internet connection available";

        UILabel *pullLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 290, 30)];
        pullLabel.text = @"Pull down to refresh...";
        pullLabel.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:pullLabel];
    };

    [reach startNotifier];

    self.upcomingReleases = [[NSMutableArray alloc] init];

    [self makeReleasesRequests];

    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(makeReleasesRequests) forControlEvents:UIControlEventValueChanged];
    [self.collectionView addSubview:_refreshControl];
    self.collectionView.alwaysBounceVertical = YES;
}

感谢。

1 个答案:

答案 0 :(得分:0)

你需要将pullLabel放入self.pullLabel。当您调用removeFrom Superview时,self.pullLabel为nil。

self.pullLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 50, 290, 30)];
self.pullLabel.text = @"Pull down to refresh...";
self.pullLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.pullLabel];