My UIRefreshControl is stuck after switching tabs, It happenes only on the first tab. Inside TabBar I have 2 tabs with UIViewControler inside which I have UITableView.
I have tried all solutions suggested HERE and none worked for me.
Below is what I am am doing.
- (void)viewDidLoad {
[super viewDidLoad];
data = [[NSMutableArray alloc] initWithArray:[[DataCache sharedCache] getData]];
[self addNavBar];
[self addDataTable];
//for refreshing the table data
UITableViewController *tableViewController = [[UITableViewController alloc] init];
tableViewController.tableView = dataTable;
refreshControl = [[UIRefreshControl alloc] init];
refreshControl.backgroundColor = [UIColor purpleColor];
refreshControl.tintColor = [UIColor whiteColor];
[refreshControl addTarget:self
action:#selector(refresh)
forControlEvents:UIControlEventValueChanged];
[dataTable addSubview:refreshControl];
tableViewController.refreshControl = refreshControl;
}
- (void)refresh {
[self loadSentData];
data = [[NSMutableArray alloc] initWithArray:[[DataCache sharedCache] getSentData]];
[self performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
}
- (void)reloadData{
[dataTable reloadData];
[refreshControl endRefreshing];
}
Since this question has been viewed hundreds of time. Adding my solution here might help someone else.
In my case, it was due to refreshControl. you need separate refreshControl for both the views. I ended up creating refreshControlFirstView and refreshControlSecondView and that resolved the issues.
Related
In my app, I use the old GKImage library for adding pictures and being able to custom crop them easily. This works pretty well, except when it comes to iPad. On iPhone, everything works great. The main issue is this:
When I use an iPad, none of the barButtonItems are clickable.
Here is my code for this view:
- (void)_actionCancel{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)_actionUse{
NSLog(#"PRESSED");
_croppedImage = [self.imageCropView croppedImage];
[self.delegate imageCropController:self didFinishWithCroppedImage:_croppedImage];
}
- (void)_setupNavigationBar{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(_actionCancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Use"
style:UIBarButtonItemStylePlain
target:self
action:#selector(_actionUse)];
}
- (void)_setupCropView{
self.imageCropView = [[GKImageCropView alloc] initWithFrame:self.view.bounds];
[self.imageCropView setImageToCrop:sourceImage];
[self.imageCropView setResizableCropArea:self.resizeableCropArea];
[self.imageCropView setCropSize:cropSize];
[self.view addSubview:self.imageCropView];
}
#pragma mark -
#pragma Super Class Methods
- (id)init{
self = [super init];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad{
[super viewDidLoad];
self.title = #"Choose Photo";
[self _setupNavigationBar];
[self _setupCropView];
[self.navigationController setNavigationBarHidden:NO];
}
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
self.imageCropView.frame = self.view.bounds;
}
The code used to present this controller is:
self.imagePicker = [[GKImagePicker alloc] init];
self.imagePicker.cropSize = CGSizeMake(280, 280);
self.imagePicker.delegate = self;
self.imagePicker.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.imagePicker.imagePickerController animated:YES completion:nil];
Is it an issue with presenting the view controller?
I have added a UIRefreshControl to my collection view:
self.refreshControl = [[UIRefreshControl alloc]init];
[self.refreshControl addTarget:self action:#selector(refreshCV:) forControlEvents:UIControlEventValueChanged];
[self.collectionView addSubview:self.refreshControl];
- (void)refreshCV:(UIRefreshControl *)refresh{
NSLog(#"Refresh");
[refresh endRefreshing];
}
But it is not working always. In other words, sometimes when pulling down it is doesn't call the refresh function. How to solve this?
Try this answer.
You have to set alwaysBounceVertical property of collection view.
If you are not using UITableViewController class, then do as below
UITableViewController *tableViewController = [[UITableViewController alloc] init];
tableViewController.tableView = self.myTableView;
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:#selector(refreshTV:) forControlEvents:UIControlEventValueChanged];
tableViewController.refreshControl = self.refreshControl;
- (void)refreshTV:(UIRefreshControl *)refresh{
NSLog(#"Refresh");
[refresh endRefreshing];
}
You add refreshCV as the target function for refresh control while you actually have refreshTV below. Please check to make sure which function you want indeed.
Here is an example of a UIRefreshControl.
UIRefreshControl *control = [[UIRefreshControl alloc] initWithFrame:CGRectZero];
control.tintColor = someColor;
[control sizeToFit];
[tableView addSubview:control];
#pragma mark UIRefreshControl
- (void) setUpRefreshControl {
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(refresh:) forControlEvents:UIControlEventValueChanged];
//add UIRefreshControl to Collection view
[self.collectionView addSubview:refreshControl];
}
- (void)refresh:(id)sender
{
UIRefreshControl *refreshControl = (UIRefreshControl *)sender;
// End the refreshing
if (refreshControl) {
[refreshControl endRefreshing];
}
}
When I add a UIPopoverController in the ViewDidLoad method, it displays an empty popover view. The same thing happens when I add a UITapGestureRecognizer; it doesn't trigger the attached method. If I put the code in my ViewDidAppear it does work, but in some cases ViewDidAppear is never called, depending on where in my storyboard the view is loaded.
Here's my code:
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] init];
popoverView.backgroundColor = [UIColor whiteColor];
popoverContent.view = popoverView;
pickerPopup = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
pickerPopup.delegate = (id)self;
[pickerPopup setPopoverContentSize:CGSizeMake(320, 240) animated:NO];
And the UITapGestureRecognizer:
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openPicker:)];
[_form_from addGestureRecognizer:tap];
I solved it for now using a delay in the ViewDidLoad method like so
[self performSelector:#selector(custom_init) withObject:nil afterDelay:0.1];
Still don't get why the UITapGestureRecognizer doesn't trigger. It does work if I attach it to the view, but not if I attach it to a textfield in a static cell.
I have a UIViewController that has a UITableView as a subView. I am trying to add a pull-to-refresh on the table.
I have the UIRefresh showing but it never calls the selector. I am not getting notified that the pull action happened.
I cannot use the TableView controller as i am using this View as my Generic View So that i can use for multiple View controllers
- (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];
}
}
Am I doing everything right here. Its not working for me. Please Help me in this.
The problem is in your viewDidLoad. No need to instantiate UITableViewController for refresh control. Just change viewDidLoad to this
- (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];
}
You have to initiate the UIRefreshControl in your viewDidLoad like this:
// Pull To Refresh Code
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
Here is your selector handleRefresh:
-(void) handleRefresh:(UIRefreshControl *) refreshControl{
[self performSelector:#selector(yourMethodHere)];
}
Also try to check that the reference to your selector is working or not. Click on your selector wile holding command button.
You just need a scorllview,and set refreshcontrol to it will do the trick.
Whatever controller you are in.
I am using this Cocoa Control Side Menu link.
In this Menu, when you start the app you have a Navigation Bar for the first screen, but when you open the menu and press any element of TableView, the ViewController change without Navigation Bar. I want to have a Navigation Bar for each of the elements of the TableView Menu and a left button on the Navigation bar that could open and close the Menu.
I start the application with this code:
// appDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
YPMenuViewController *menuVC = [[YPMenuViewController alloc] init];
YPProfileViewController *contentVC = [[YPProfileViewController alloc] init];
UINavigationController *contentNavigationController = [[UINavigationController alloc] initWithRootViewController:contentVC];
YPSideMenuOptions *options = [[YPSideMenuOptions alloc] init];
YPSideMenuController *sideMenuController = [[YPSideMenuController alloc] initWithMenuViewController:menuVC
contentViewController:contentNavigationController
options:options];
self.window.rootViewController = sideMenuController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
After this, the app starts with Navigation bar.
But then, in the TableView If Select the view controllers like this, I have the Navigation Bar but without button to open an close
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0:
{
YPProfileViewController *contentVC = [[YPProfileViewController alloc] init];
UINavigationController *contentNavigationController = [[UINavigationController alloc] initWithRootViewController:contentVC];
UIButton *iwantView = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
[iwantView setBackgroundColor:[UIColor blackColor]];
[iwantView addTarget:self action:#selector(openMenu) forControlEvents:UIControlEventTouchUpInside];
contentNavigationController.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:iwantView];
[[self sideMenuController] changeContentViewController:contentNavigationController closeMenu:YES];
break;
}
case 1:
{
YPProjectListViewController *contentVC = [[YPProjectListViewController alloc] init];
UINavigationController *contentNavigationController = [[UINavigationController alloc] initWithRootViewController:contentVC];
[[self sideMenuController] changeContentViewController:contentNavigationController closeMenu:YES];
break;
}
default:{
YPProfileViewController *contentVC = [[YPProfileViewController alloc] init];
[[self sideMenuController] changeContentViewController:contentVC closeMenu:YES];
break;
}
}
}
You are adding the button before the view controller is loaded, you should add it after loading the view in your content view controller.
#implementation YPProfileViewController
---------------
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIButton *iwantView = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
[iwantView setBackgroundColor:[UIColor blackColor]];
[iwantView addTarget:self action:#selector(openMenu) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:iwantView];
}
Anyway I just added a method to add a menu button on the navigation bar more easily.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self addLeftMenuButtonWithImage:[UIImage imageNamed:#"menu_icon"]];
}