ios7: Pull to refresh does not show up on simulator - ios

I am pretty new to iOS programming and for a class project I was trying to implement "pull to refresh feature"
I tried from this link, and implemented same in my MoviesViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.moviesTableView.delegate = self;
self.moviesTableView.dataSource = self;
// pull to refresh
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to Refresh"];
[refresh addTarget:self action:#selector(refreshView:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
// progress bar
MBProgressHUD *HUD = [self getLoadingController];
[HUD showWhileExecuting:#selector(generateMovies) onTarget:self withObject:nil animated:YES];
self.moviesTableView.rowHeight = 100;
[self.moviesTableView registerNib:[UINib nibWithNibName:#"MovieViewCell" bundle:nil] forCellReuseIdentifier:#"MovieViewCell"];
}
and
-(void)refreshView:(UIRefreshControl *)refresh {
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Refreshing data..."];
// custom logic: get data from server
NSLog(#"refreshing view");
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM d, h:mm a"];
NSString *lastUpdated = [NSString stringWithFormat:#"Last updated on %#",[formatter stringFromDate:[NSDate date]]];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated];
[refresh endRefreshing];
}
But when I run this in simulator, I do not see it while pulling down the movies table.
What could be the issue here?

I think you're missing adding the HUD into the refresh control. Depending on your view structure, you'd need something like:
[self.refreshLoadingView addSubview:self.hud];
We'd need to see the rest of your code to debug the problem, but here's a tutorial I wrote explaining custom pull-to-refresh controls for iOS (including Swift): http://www.jackrabbitmobile.com/design/ios-custom-pull-to-refresh-control/

Related

No known class method for selector 'datePicker'

I have searched similar questions here to help me solve mine, all in vain. I want the UItextField when clicked to pull up the UIDatePicker, then the user can select a date, and then set it in the UITextfield. However I keep getting an error for 'datePicker'. not sure what else I am supposed to do.
This is the part of my app that calls the datePicker
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[pd resignFirstResponder]; //the textField that you will set the selected date
datePicker = [[UIDatePicker alloc] init]; //declared uidatepicker component
pickerViewDate = [[UIActionSheet alloc] initWithTitle:#"Select the date!"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
datePicker.datePickerMode = UIDatePickerModeDate; //set your spesific mode
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]]; //or another LocaleIdentifier instead of en_US
[dateFormatter setDateFormat:#"dd.MM.yyyy"]; //desired format
[datePicker addTarget:self action:#selector(dateChanged) forControlEvents:UIControlEventValueChanged]; //the function would be fired when user change the date in datePicker
//now preparing the toolbar which will be displayed at the top of the datePicker
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle=UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonClicked)]; //barbutton item is "DONE" and doneButtonClicked action will be fired when user clicks the button.
[barItems addObject:flexSpace]; // set the left of the bar
[pickerToolbar setItems:barItems animated:YES];
[pickerViewDate addSubview:pickerToolbar];
[pickerViewDate addSubview:datePicker];
[pickerViewDate showInView:self.view];
[pickerViewDate setBounds:CGRectMake(0,0,320, 464)]; //you can change the position
}
-(IBAction)dateChanged{
NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init];
[FormatDate setLocale: [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US"]];
[FormatDate setDateFormat:#"dd.MM.yyyy"];
pd.text = [FormatDate stringFromDate:[UIDatePicker datePicker]];
}
You're not going to be able to Google the cause of the errors in your code. You will need to figure it out, as your code is unique.
As #madmik3 points out, this line is wrong:
pd.text = [FormatDate stringFromDate:[UIDatePicker datePicker]];
That code is trying to call a UIDatePicker class method "datePicker", which doesn't make any sense.
What you probably want instead is:
pd.text = [FormatDate stringFromDate: [datePicker date]];
That code is asking your date picker object, in the variable datePicker, for it's date property (the current date set in the date picker.

Pull To Refresh crashes app if I try to scroll while refreshing

I have implemented Pull to Refresh on my app but it crashes when I try to scroll while it is still refreshing. here is the code I'm using:
FYI, I'm calling webServices while refreshing.
In ViewDidLoad:
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to Refresh"];
[refresh addTarget:self action:#selector(callWebService:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
[self callWebService:refresh];
Method for refresh:
-(void)callWebService:(UIRefreshControl *)refresh
{
static BOOL refreshInProgress = NO;
MessageWebServices *messageWebService = [[MessageWebServices alloc]init];
MessageBO *tempBO = [[MessageBO alloc]init];
if(self.flag)
{
self.navigationItem.title = #"My Posts";
tempBO.projectId = [AppConstants getProjectId];
tempBO.customerId =[AppConstants getCustomerId];
}
else
{
tempBO.projectId = [AppConstants getProjectId];
}
self.messageStore = [[NSMutableArray alloc] init];
if (!refreshInProgress)
{
refreshInProgress = YES;
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Refreshing"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.messageStore = [messageWebService getMessageList:tempBO];
dispatch_async(dispatch_get_main_queue(), ^{
[refresh beginRefreshing];
[self.tableView reloadData];
[refresh endRefreshing];
refreshInProgress = NO;
});
});
}
}
Please Help!
I have never used beginRefreshing method. You can easily add refresh control to your tableView.
#pragma mark Refresh Method
-(void)startRefreshing {
if (!self.refreshControl) {
// Initialize the refresh control.
self.refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl.backgroundColor = CRREFRESHBGCOLOR;
self.refreshControl.tintColor = [UIColor whiteColor];
[self.refreshControl addTarget:self action:#selector(callWebservice) forControlEvents:UIControlEventValueChanged];
[yourTable addSubview:self.refreshControl];//add refresh control to your table
}
}
-(void)endRefreshing {
if (self.refreshControl) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM d, h:mm a"];
NSString *title = [NSString stringWithFormat:#"Last update: %#", [formatter stringFromDate:[NSDate date]]];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor]
forKey:NSForegroundColorAttributeName];
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attrsDictionary];
self.refreshControl.attributedTitle = attributedTitle;
[self.refreshControl endRefreshing];
}
}
After calling/finishing web-service method call [self endRefreshing]; method and reload your table [yourTable reloadData].

How To Apply Refresh Control To UITableView

Is there a way to apply a refresh control to a UITableView which is set in a UIViewController?
The solution I found requires subclassing of UITableViewController, which is troublesome for me because I need to subclass a UIViewController not a UITableViewController, and I don't see any method under the documentation of UITableView that allows me to set a refresh control on it.
Any help would be very much appreciated!
use this UIRefreshControl control :
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(refresh1:) forControlEvents:UIControlEventValueChanged];
[Delivered_TBL addSubview:refreshControl];
- (void)refresh1:(UIRefreshControl *)refreshControl
{
[self Service_Call];
[refreshControl endRefreshing];
}
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.backgroundColor = [UIColor clearColor];
refreshControl.tintColor = [UIColor whiteColor];
[refreshControl addTarget:self
action:#selector(getLatestData)
forControlEvents:UIControlEventValueChanged];
[yourTableviewName addSubview:refreshControl];
-(void)getLatestData
{
// here add your reload method
[self XXXXX];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM d, h:mm a"];
NSString *title = [NSString stringWithFormat:#"Last update: %#", [formatter stringFromDate:[NSDate date]]];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor]
forKey:NSForegroundColorAttributeName];
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attrsDictionary];
refreshControl.attributedTitle = attributedTitle;
[refreshControl endRefreshing];
}

Getting date from the label onto the datepicker

I am working with the UIdatepicker. I am fetching the time from the datepicker and showing the date on the label. Once i select the date it is shown on the label and datepicker is hidden away. Now i want that when i call the picker it should show me the selection bar of the datepicker on the date of the label which i selected previously selected on the datepicker.Code i am using is shown below:-
-(void)datepickershown:(UILabel *)time animated:(BOOL)animated
{
UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:#"Select the Time" delegate:self cancelButtonTitle:nil destructiveButtonTitle:#"DONE" otherButtonTitles:Nil, nil];
pickdate = [[UIDatePicker alloc] initWithFrame:[actionsheet bounds]];
pickdate.hidden = NO;
//pickdate.date = [NSDate date];
pickdate.datePickerMode = UIDatePickerModeTime;
[pickdate addTarget:self action:#selector(changeDateInLabel:) forControlEvents:UIControlEventValueChanged];
[actionsheet addSubview:pickdate];
[actionsheet showInView:self.view];
[actionsheet setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickdate.bounds;
pickerRect.origin.y = -90;
pickdate.bounds = pickerRect;
}
- (IBAction)changeDateInLabel:(id)sender
{
NSLog(#"I Am Changing the values");
df = [[NSDateFormatter alloc] init];
df.timeStyle = NSDateFormatterShortStyle;
}
As you want the datepicker to show the date you have already set on the label.
For this you need to use following method.
- (void)setDate:(NSDate *)date animated:(BOOL)animated
Pass the date whatever you have selected and is shown in the label.
try this
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy"];
if([dateFormat dateFromString:yourDateLabel.text])
{
NSDate *date = [dateFormat dateFromString:yourDateLabel.text];
[self.datePicker setDate:date];
}
hope this will work

SVPullToRefresh not triggering the action handler when pushed

The first trigger goes ok, but after that, if I try to pull it down, it always shows its still loading.
here is my simple code example on my UITableViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
__weak StoresViewController * bSelf = self;
[self.tableView addPullToRefreshWithActionHandler:^{
[bSelf callStoresService];
}];
[self.tableView triggerPullToRefresh];
}
- (void)callStoresService {
[self.tableView.pullToRefreshView stopAnimating];
}
Adding a breakpoint to [self.tableView.pullToRefreshView stopAnimating]; I can see it never gets called again...
I have an old project with almost the same code and its working... anything I am missing here?
Since I am only developing for iOS 6, I decided to use the native refresh:
on my viewDidLoad:
// managing pullToRefresh
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull down to refresh"];
[refresh addTarget:self action:#selector(refreshView:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
[self refreshView:refresh];
[self.refreshControl beginRefreshing];
Then create a refreshView method:
-(void)refreshView:(UIRefreshControl *)refresh {
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Refreshing..."];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM HH:mm:ss"];
// do what you need
NSString *lastUpdated = [NSString stringWithFormat:#"Last update: %#", [formatter stringFromDate:[NSDate date]]];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated];
}
dont forget to call [refresh endRefreshing]; when your are done

Resources