【问题标题】:UIRefreshControl not working when called prorgammatically以编程方式调用 UIRefreshControl 不起作用
【发布时间】:2013-05-23 15:49:59
【问题描述】:

我的ViewController 中有一个UIRefreshControl 和一个方法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;

【问题讨论】:

    标签: ios pull-to-refresh uirefreshcontrol


    【解决方案1】:

    根据我对文档的理解:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIRefreshControl_class/Reference/Reference.html

    告诉控件刷新操作已启动 以编程方式。 ...当有外部事件时调用此方法 source 触发以编程方式刷新您的表格。

    我认为它不是用来启动刷新操作,而只是用于更新它当前正在刷新的刷新控件状态,它会使刷新控件旋转。目的是避免用户拉动表格视图并在它仍在刷新时再次触发刷新操作。

    所以你应该自己调用 refreshView: 方法。

    【讨论】:

      【解决方案2】:

      只需异步调用 beginRefreshing。

      - (void)viewDidLoad {
         [super viewDidLoad];
         // Do any additional setup after loading the view.
      
         dispatch_async(dispatch_get_main_queue(), ^{
             [refreshControl beginRefreshing];
         });
         //...
      }
      

      【讨论】:

        【解决方案3】:

        试试这个

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

        【讨论】:

        • 您的viewDidLoad 代码与我的没有区别。 refreshMyTableView 方法不会在 [refresh beginRefreshing] 上调用,仍然。
        • 尝试在结束动作中删除你的':':@selector(refreshView:)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-11
        • 2012-07-21
        相关资源
        最近更新 更多