UIVefreshControl在UIViewController中

问题描述 投票:1回答:3

我有一个UIViewController有一个UITableView作为subView。我想在桌面上添加一个pull-to-refresh。我有UIRefresh显示但它从未调用选择器。我没有得到拉动动作的通知。我无法使用TableView控制器,因为我使用此视图作为我的通用视图因此我可以使用多个View控制器

    - (void)viewDidLoad{

    [super viewDidLoad];

   // Refresh control
     UITableViewController *tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
tableViewController.tableView = self.genericMainTableView;

// Initialise refresh control
self.refreshControl = [[UIRefreshControl alloc] init];

//Adding target to refresh Control object
[self.refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];

//Setting attribute Title
self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull To Refresh"];

//Adding as subview to tableView
tableViewController.refreshControl  = self.refreshControl;

}

- (void)refresh:(UIRefreshControl *)refreshControl {

reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reachability currentReachabilityStatus];
//Not Reachable - No Internet Connection
if (netStatus == NotReachable) {
    [[[UIAlertView alloc]initWithTitle:@"Network Error" message:@"Please Check your Network Connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]show];
    [refreshControl endRefreshing];

}
else{

    //My Function Call
    [self functionCall];

    //Reload the Table
    [self.genericMainTableView reloadData];

    //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 Refreshing
    [refreshControl endRefreshing];
}

}

我在这里干什么它不适合我。请帮帮我。

ios objective-c xcode uirefreshcontrol
3个回答
1
投票

问题出在viewDidLoad中。无需实例化UITableViewController进行刷新控制。只需将viewDidLoad更改为此即可

- (void)viewDidLoad{

[super viewDidLoad];

// Initialise refresh control
self.refreshControl = [[UIRefreshControl alloc] init];

//Adding target to refresh Control object
[self.refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
//Setting attribute Title
self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull To Refresh"];

//Adding as subview to tableView
[self.genericMainTableView addSubview:self.refreshControl];

}

0
投票

你必须在你的UIRefreshControl中启动viewDidLoad,如下所示:

// Pull To Refresh Code
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];

这是你的选择器handleRefresh:

-(void) handleRefresh:(UIRefreshControl *) refreshControl{
    [self performSelector:@selector(yourMethodHere)];
}

还要尝试检查对选择器的引用是否有效。单击选择器wile hold命令按钮。


0
投票

你只需要一个scrollview,并设置refreshcontrol就可以了。无论你在哪个控制器。

© www.soinside.com 2019 - 2024. All rights reserved.