UIRefreshControl iOS 6 xcode

Gar*_*eth 42 iphone xcode objective-c ios6 uirefreshcontrol

有没有人有一个如何将新的实现UIRefreshControl到xcode 的简短示例.我有一个UITableViewController显示推文,希望能够下拉和刷新.

dan*_*ing 68

viewDidLoad如果您有以下内容,可以在您的设置中进行设置UITableViewController:

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(refresh)
         forControlEvents:UIControlEventValueChanged]; 
self.refreshControl = refreshControl;
Run Code Online (Sandbox Code Playgroud)

然后你可以在这里做你的刷新事情:

-(void)refresh {
    // do something here to refresh.
}
Run Code Online (Sandbox Code Playgroud)

当您完成刷新后,请调用[self.refreshControl endRefreshing];以停止刷新控制,如rjgonzo所指出的那样.

  • 记得调用`[self.refreshControl endRefreshing];`来停止刷新控制.您也可以设置[title](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIRefreshControl_class/Reference/Reference.html).干杯. (17认同)

J S*_*iro 32

您也可以在Interface Builder中进行配置.虽然它目前的工作方式,它只为您节省了几行代码.

选择TableViewController场景,在At​​tributes Inspector中,您将找到"Refreshing"的下拉列表选项.将其设置为"已启用".您将在视图控制器层次结构中注意到已添加"刷新控件"(您不会看到任何可视地添加到场景本身的内容).奇怪的是,在将刷新控制连接到IBAction(值变更事件)之后,事件似乎没有被触发.我猜这是一个错误(?)但同时,将"Refreshing"设置为启用会创建UIRefreshControl对象并将其分配给视图控制器的refreshControl属性.完成后,您可以将事件处理行添加到viewDidLoad方法:

[self.refreshControl addTarget:self action:@selector(refreshView:) forControlEvents:UIControlEventValueChanged];
Run Code Online (Sandbox Code Playgroud)

在refreshView:方法中,您可以执行一些工作,然后停止刷新动画:

- (void)refreshView:(UIRefreshControl *)sender {
    // Do something...
    [sender endRefreshing];
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您从列表中提取了TableViewController输出对象,那么"刷新"项目就在那里.如果你自己构建了View,那不是你.所以,不要怀疑它是否存在 (2认同)
  • 在XCode 6上,修复了IB错误.现在在IB中,您可以控制从"刷新控件"拖动到代码以创建IBAction. (2认同)

iMe*_*elf 18

下面介绍如何下拉和刷新

在你的UITableViewController.h中添加 UIRefreshControl *refreshControl;-(void) refreshMyTableView;方法全局声明

viewDidLoadUITableViewController.m

//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;
Run Code Online (Sandbox Code Playgroud)

和刷新日期和时间刷新功能

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

}
Run Code Online (Sandbox Code Playgroud)