2013-05-23 18 views
8

Ho un UIRefreshControl nel mio ViewController e un metodo refreshView per gestire l'evento "pull to refresh". Funziona perfettamente quando effettivamente si tira, ma quando chiamo [refresh beginRefreshing] nel codice, mostra solo l'animazione di aggiornamento ma non chiama il metodo refreshView.UIRefreshControl non funziona se chiamato prorgamaticamente

Ecco il codice per l'inizializzazione di controllo di aggiornamento in 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; 

risposta

14

Sulla base della mia comprensione sulla documentazione: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIRefreshControl_class/Reference/Reference.html

Indica al controllo che un'operazione di aggiornamento è stato avviato di programmazione. ... Chiama questo metodo quando un'origine evento esterna attiva un aggiornamento programmatico della tabella.

Penso che non sia utilizzato per avviare un'operazione di aggiornamento, piuttosto è solo per l'aggiornamento dello stato di controllo di aggiornamento che è attualmente in fase di aggiornamento, in cui farà girare il controllo di aggiornamento. Lo scopo è quello di evitare che l'utente tiri la vista tabella e attiva nuovamente l'operazione di aggiornamento mentre è ancora in fase di aggiornamento.

Quindi dovresti chiamare il metodo refreshView: da solo.

-1

Prova questa

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; 

Metodo

-(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 

} 

per fermarlo

[refreshControl endRefreshing]; 
+0

Il codice 'viewDidLoad' non è diverso dal mio. Il metodo 'refreshMyTableView' non verrà richiamato su' [refresh beginRefreshing] ', ancora. –

+0

Prova a rimuovere il tuo ':' alla fine azione: @selector (refreshView :) – IamGretar

0

Basta chiamare beginRefreshing in modo asincrono.

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

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