Question

I added a UILabel to my view whenever there's no internet connection (using Reachablity).

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

I want to get rid of label after the view gets refreshed (with uirefreshcontrol). I tried adding removeFromSuperview and hiding the label but I still can't get it to work.

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

My 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;
}

Thanks.

Was it helpful?

Solution

You need to put pullLabel into self.pullLabel. self.pullLabel is nil when you are calling removeFrom Superview.

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top