2013-05-23 5 views
8

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

Вот код для инициализации управления обновления в 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; 

ответ

14

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

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

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

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

-1

Попробуйте

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]; 
+0

Ваш код 'viewDidLoad' не отличается от моего. Метод 'refreshMyTableView' не будет вызываться на' [refresh beginRefreshing] ', все еще. –

+0

Попробуйте удалить ваш ':' в конце action: @selector (refreshView :) – IamGretar

0

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

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

    dispatch_async(dispatch_get_main_queue(), ^{ 
     [refreshControl beginRefreshing]; 
    }); 
    //... 
} 
Смежные вопросы