UIRefreshControl не работает, когда вызывается проргамматически

У меня есть UIRefreshControl в моем ViewController и метод refreshView обрабатывать событие "тянуть, чтобы обновить". Он отлично работает, когда на самом деле тянет, но когда я звоню [refresh beginRefreshing] в коде, он просто показывает анимацию обновления, но не вызывает refreshView метод.

Вот код для инициализации управления обновлением в viewDidload:

UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
[refresh addTarget:self
            action:@selector(refreshView:)
  forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;

3 ответа

Решение

Основываясь на моем понимании документации: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIRefreshControl_class/Reference/Reference.html

Сообщает элементу управления, что операция обновления была запущена программно.... Вызовите этот метод, когда внешний источник события вызывает программное обновление вашей таблицы.

Я думаю, что он не используется для запуска операции обновления, скорее, он просто обновляет состояние элемента управления обновлением, в котором он в данный момент обновляет элемент управления обновлением. Цель будет состоять в том, чтобы пользователь не тянул табличное представление и снова не запускал операцию обновления, пока она еще обновляется.

Поэтому вы должны вызвать метод refreshView: самостоятельно.

Просто вызовите beginRefreshing асинхронно.

- (void)viewDidLoad {
   [super viewDidLoad];
   // Do any additional setup after loading the view.

   dispatch_async(dispatch_get_main_queue(), ^{
       [refreshControl beginRefreshing];
   });
   //...
}

Попробуй это

ViewDidLoad

//initialise the refresh controller
refreshControl = [[UIRefreshControl alloc] init];
//set the title for pull request
refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"pull to Refresh"];
//call he refresh function
[refreshControl addTarget:self action:@selector(refreshMyTableView)
         forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;

метод

-(void)refreshMyTableView{

    //set the title while refreshing
    refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing the TableView"];
    //set the date and time of refreshing
    NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
    [formattedDate setDateFormat:@"MMM d, h:mm a"];
    NSString *lastupdated = [NSString stringWithFormat:@"Last Updated on %@",[formattedDate stringFromDate:[NSDate date]]];
    refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:lastupdated];
    //end the refreshing

}

остановить это

[refreshControl endRefreshing];
Другие вопросы по тегам