0

是否可以UIRefreshControl在启动应用程序时自动启动?我试过打电话self.refreshControl beginRefreshing];,但它不工作。

这是我的 viewDidLoad 中的一些代码:

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(refreshNowShowingMoviesList:) forControlEvents:UIControlEventValueChanged];
    [self setRefreshControl:refreshControl];
    [refreshControl beginRefreshing];

编辑

这是我的 refreshNowShowingMoviesList 方法:

- (void)refreshNowShowingMoviesList:(id)sender
{
    // Create URL
    NSURL *url = [NSURL URLWithString:kNowShowingMoviesURL"];

    // Initialize URL request
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];

    // Set value on header
    [urlRequest setValue:kHeaderFieldValue forHTTPHeaderField:kHeaderField];

    // JSON request operation
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:urlRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSMutableArray *results = [(NSDictionary *)JSON objectForKey:@"results"];

        if ([results count]) {
            _objects = results;

            // Reload table view
            [self.tableView reloadData];

            // End refreshing
            [(UIRefreshControl *)sender endRefreshing];
        }

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        // End refreshing
        [(UIRefreshControl *)sender endRefreshing];

        // Show error alert
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:kSorryTitle message:[error localizedDescription] delegate:self cancelButtonTitle:nil otherButtonTitles:kOk, nil];
            [errorAlert show];
        });
    }];

    // Start operation
    [operation start];
}

带有错误消息:

'NSInvalidArgumentException', reason: '-[MasterViewController endRefreshing]: unrecognized selector sent to instance 0xa172450'
4

1 回答 1

1

您应该调用refreshNowShowingMoviesList:nil(注意nil参数)。然后它不会崩溃,因为你可以在 nil 对象上调用任何东西。但似乎你在打电话refreshNowShowingMoviesList:self。这导致endRefreshingcall on MasterViewController,这是不可用的。

甚至更容易/更好,在调用之前检查类:

if([sender respondsToSelector:@selector(endRefreshing)]) {
    [(UIRefreshControl *)sender endRefreshing];
}

或者只使用属性而不是 sender 参数。嗯。像self.tableView.refreshControl...

于 2013-05-22T13:02:58.823 回答