In my app I have this hierarchy:
AppViewController (root)-->HUDViewController (as a container viewcontroller within AVC)-->NavBar (subview of UIView)-->UIButtons
When you touch some of the buttons they need to launch a UIPopoverController from AVC. I send a notification back from the NavBar class to AVC. In the selector for the notification center I have this code
...
//get the client list from the notification
[dictPopoverData setObject: [[notification userInfo] objectForKey:#"Client List"] forKey:#"Client List"];
//get the frame of the object launching the popover
popupCallerFrame = CGRectFromString([[notification userInfo] objectForKey:#"Caller Frame"]);
[self presentPopOver:CLIENT_LIST:YES:dictPopoverData];
...
then in presentPopOver I have this:
- (void) presentPopOver : (int) popoverID : (BOOL) isTable : (NSMutableDictionary*) dictPopoverData {
if (self.myPopoverController != nil) {
[self.myPopoverController dismissPopoverAnimated:YES];
self.myPopoverController = nil;
}
CGRect launchFrame;
//init the popover
if (popoverID == CLIENT_LIST) {
ClientListPopover* vcClientList = [[ClientListPopover alloc] initWithStyle:UITableViewStylePlain];
vcClientList.arrDataSource = [dictPopoverData objectForKey:#"Client List"];
self.myPopoverViewController = vcClientList;
//set the launch frame
launchFrame = popupCallerFrame;
launchFrame.origin.x = launchFrame.origin.x;
launchFrame.origin.y = launchFrame.origin.y + 100.0;
} else if (popoverID == PA_LIST) {
PAListPopover* vcPAList = [[PAListPopover alloc] initWithStyle:UITableViewStylePlain];
vcPAList.strClientNumber = [dictPopoverData objectForKey:#"Client Number"];
self.myPopoverViewController = vcPAList;
//set the launch frame
launchFrame = popupCallerFrame;
launchFrame.origin.x = launchFrame.origin.x;
launchFrame.origin.y = launchFrame.origin.y + 100.0;
}
//init and display the popover controller
if (self.myPopoverController == nil) {
//add the self.myPopoverViewController
self.myPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.myPopoverViewController];
//display the popover
[self.myPopoverController presentPopoverFromRect:launchFrame inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
[self.myPopoverController setDelegate:self];
} else {
[self.myPopoverController dismissPopoverAnimated:YES];
self.myPopoverController = nil;
}
}
So what is suppose to happen is the user clicks the client button, notification gets sent to AVC, a UITable in a popover controller is presented. The user then selects a client from the table. Again, a notification is sent to AVC, the displayed client list popover should now be dismissed and the PA list popover should show. It seems that [self.myPopoverController dismissPopoverAnimated:YES] is not being called. I've traced it and it hits that line of code but nothing happens, the first popover remains on the screen. Any ideas of what I am doing wrong?
Edit: I forgot to mention I can't seem to assign a delegate to self.myPopoverController. Which is probably why the dismiss method is not firing.
Edit: I added the call to the delegate after the popover controller is inited. That didn't seem to make a difference. If I touch outside the first popover, without touching inside, it does dismiss and I can trace the method.
Related
When I give a good swipe to my tableView and press the "Back" button before the tableView ended it's scrolling, my app crashes. I've tried the following:
- (void) closeViewController
{
[self killScroll];
[self.navigationController popToRootViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)killScroll
{
CGPoint offset = sellersTableView.contentOffset;
[sellersTableView setContentOffset:offset animated:NO];
}
That didn't work, same crash. I don't see why, the error I'm getting is the following:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
So that means that the tableView is still requesting a cell when everything is already being deallocated. Makes no sense.
Then I tried this:
- (void) closeViewController
{
[self.navigationController popToRootViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)dealloc
{
sellersTableView.dataSource = nil;
sellersTableView.delegate = nil;
sellersTableView = nil;
}
Gives me the same error. Any ideas?
Update:
My delegate methods
creation
if (textField == addSellerTextField) {
sellersTableView = [[UITableView alloc] initWithFrame:CGRectMake(addSellerTextField.frame.origin.x + addSellerTextField.frame.size.width + 10, addSellerTextField.frame.origin.y - [self heightForTableView] + 35, 200, [self heightForTableView])];
sellersTableView.delegate = self;
sellersTableView.dataSource = self;
sellersTableView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.05];
sellersTableView.separatorColor = [[UIColor grayColor] colorWithAlphaComponent:0.15];
sellersTableView.rowHeight = 44;
sellersTableView.layer.opacity = 0;
[self.companyView addSubview:sellersTableView];
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{sellersTableView.layer.opacity = 1;} completion:nil];
}
cellForRowAtIndexPath
if (tableView == sellersTableView) {
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.backgroundColor = [UIColor clearColor];
if ([sellersArray count] > 0) {
cell.textLabel.text = [sellersArray objectAtIndex:indexPath.row];
} else {
UILabel *noSellersYetLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, sellersTableView.frame.size.width, [self heightForTableView])];
noSellersYetLabel.text = #"no sellers yet";
noSellersYetLabel.textAlignment = NSTextAlignmentCenter;
noSellersYetLabel.textColor = [UIColor grayColor];
[cell addSubview:noSellersYetLabel];
sellersTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
}
removing
- (void) textFieldDidEndEditing:(UITextField *)textField
{
if (textField == addSellerTextField) {
[self updateSellers:textField];
}
}
- (void)updateSellers:(UITextField *)textField
{
[textField resignFirstResponder];
[self hideSellersTableView];
}
- (void)hideSellersTableView
{
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{sellersTableView.layer.opacity = 0;} completion:nil];
sellersTableView.dataSource = nil;
sellersTableView.delegate = nil;
[sellersTableView removeFromSuperview];
sellersTableView = nil;
}
Solution
So apparently putting the dataSource = nil and delegate = nil into textFieldDidEndEditing fixed the problem. Thanks everybody for the answers!
It's strange behaviour of UITableView. The easiest way to resolve this issue just set the dataSource and delegate property of UITAbleView to nil before you make a call of function popToRootViewControllerAnimated. Furthermore you can use more common solution and add the code that set the properties to nil into the -dealloc method. In addition you no need the -killScroll method.
After a short research I have realized what the problem is. This unusual behaviour appeared in iOS 7. The scroll view retained by its superview may send message to delegate after the delegate is released. It happens due to -removeFromSuperview implementation UIScrollView triggers -setContentOffset: and, eventually, send message to delegate.
Just add following lines at the beginning of dealloc method:
sellersTableView.delegate = nil;
sellersTableView.dataSource = nil;
No need to use hacks like your killScroll method.
Also, I can't see why you want to call both popToRootViewController and dismissViewController.
If you dismiss a view controller which is embedded in a navigation controller, navigation controller itself as well as all contained view controllers will be released.
In your case you'll have just weird animation.
setContentOffset method won't help you, try to set
sellersTableView.dataSource = nil;
somewhere in your viewWillDisappear method.
This is not a good practice of course.
Change you closeViewController like below and see if works
(void) closeViewController
{
sellersTableView.dataSource = nil;
sellersTableView.delegate = nil;
[self.navigationController popToRootViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
I don't think that setting the tableView (or it's delegate) to nil is the issue. You should be able to perform both dismissViewControllerAnimated or popToRootViewController individually without having to modify the tableView in this way.
So the issue is most likely due to calling both of these methods at the same time (and with animated = YES), and in doing so asking your viewController setup to do something unnatural.
Looks like upon tapping a "close" button you are both popping to a rootViewController of a UINavigationController, as well as dismissing a modal viewController.
In doing so, you're dismissing a modal viewController which is likely presented by the topViewController of the navigationController (so top vc is holding a reference to modal vc). AND you're trying to kill the top vc via the popToRootViewController method call. And you're doing both of these things using animated = YES, which means they take some time to complete, and you can't be sure when each finishes (ie you can't be sure when dealloc will be called).
Depending on your needs you could do one of several things.
Consider adding a delegate property to your modal vc. Dismiss the modal vc, and in the completionBlock of the modal vc tell its delegate that it's finished dismissing. At that point call popToRootViewController (because at this point you can be sure that the modal is gone and scrolling wasn't interrupted).
If it's your navController that's been presented modally, then do this in the opposite order. Notifying the delegate that the pop operation has completed, and do the modal dismissal then.
This situation happens very rarely and I don't know how to reproduce it, but it does happen sometimes and I would like to fix it.
In ViewController A, if I push ViewController Bin, sometimes(not always) when ViewController B did appear, the navigation bar is showing the navigation bar items of ViewController A, not those of ViewController B. If I click on the back button, I cannot go back to ViewController A, getting stuck in ViewController B.
A UIBarButtonItem is added to the navigation items of ViewController A, and the navigation items of ViewController A will be updated in response to some events. Is it the reason that causes this issue?
Codes for pushing ViewController B
ViewControllerB* viewControllerB = [ViewControllerB new];
[self.navigationController pushViewController:viewControllerB animated:YES];
Codes for updating the navigation items in ViewController A
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if(NumberOfCalendarOperations == 0){
[self showNavigatorBarButtons];
}
else{
[self startRefreshAnimationOnUpdateButton];
}
}
//This method is triggered through notification when the number of operations in calendarOperationQueue is changed
-(void)calendarOperationQueueStateDidChange:(NSNotification*)notification{
if(NumberOfCalendarOperations == 0){
if (self.isShowActivityIndicator) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showNavigatorBarButtons];
});
}
}
else{
if (!self.isShowActivityIndicator) {
dispatch_async(dispatch_get_main_queue(), ^{
[self startRefreshAnimationOnUpdateButton];
});
}
}
}
/**
* Show the update button on the right of navigation bar
*/
-(void)showNavigatorBarButtons{
self.isShowActivityIndicator = NO;
UIBarButtonItem *updateButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"sync.png"] style:UIBarButtonItemStylePlain target:self action:#selector(updateButtonDidPress:)];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:updateButton, nil]];}
/**
* Start refresh animation on the update button
*/
-(void)startRefreshAnimationOnUpdateButton{
if (self.navigationController.topViewController != self) {
return;
}
self.isShowActivityIndicator = YES;
UIView* updateButtonView = nil;
for (UIView *subViewInNavigationBar in self.navigationController.navigationBar.subviews){
NSString *subViewClassAsString = NSStringFromClass([subViewInNavigationBar class]);
if ([subViewClassAsString compare:#"UINavigationButton" /* TODO: be careful with this */
options:NSCaseInsensitiveSearch] == NSOrderedSame){
if ([subViewInNavigationBar isKindOfClass:[UIView class]] == YES){
if(updateButtonView == nil){
updateButtonView = subViewInNavigationBar;
}
else if(subViewInNavigationBar.center.x < updateButtonView.center.x){
updateButtonView = subViewInNavigationBar;
}
}
}
}
for (UIView *subViewsInButton in updateButtonView.subviews){
if ([subViewsInButton isKindOfClass:[UIImageView class]] == YES &&
subViewsInButton.frame.origin.x != 0.0f &&
subViewsInButton.frame.origin.y != 0.0f){
[subViewsInButton removeFromSuperview];
CGRect activityIndicatorFrame = self.updateButtonActivityIndicator.frame;
activityIndicatorFrame.origin.x = (CGRectGetWidth(updateButtonView.frame) / 2.0f) - (CGRectGetWidth(activityIndicatorFrame) / 2.0f);
activityIndicatorFrame.origin.y = (CGRectGetHeight(updateButtonView.frame) / 2.0f) - (CGRectGetHeight(activityIndicatorFrame) / 2.0f);
self.updateButtonActivityIndicator.frame = activityIndicatorFrame;
[self.updateButtonActivityIndicator startAnimating];
[updateButtonView addSubview:self.updateButtonActivityIndicator];
return;
}
}
}
Anyone has got a clue? Thank you.
Finally I found the reason of this issue. It is that I try to hide the navigation bar in viewWillDisappear. Now iOS allows me to back to the previous view by panning from the left edge of the screen. But while panning from the left edge, if I cancel this action, and pan back to the left edge, the navigation bar will enter this strange state.
iOS unfortunately doesn't have a dropdown picker like html does with the tag. I decided that I was finally going to create one for my app, and it looks and works great. My dropdown object is a subclass of UITextField. However, I changed something and now it only works some of the time.
User interaction is enabled, but I don't want the textfield to be editable. The class in which my dropdown subclass resides is UITextField delegate, and should receive delegate methods for UITextField.
I have - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ where I check to see if the textfield in question is a dropdown menu, and if it is, I call a method to instantiate a popover and disable editing, but the dropdown only appears on every other tap.
For example, i'll tap the "textfield" and my popover displays. I tap out so the popover goes away, then I tap on the "textfield" and nothing happens. I tap on the textfield once again and the popover appears. No idea why this is happening, here is what i'm doing:
.h
subclass : UIViewController<UITextFieldDelegate>
.m
dropdownTextField.delegate = self;
...
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if(textField == self.measurementSelect){
NSLog(#"IM CALLED");
[self showPopover:textField];
return NO;
}
return YES;
}
-(void)showPopover:(id)sender{
if (_measurementPicker == nil) {
_measurementPicker = [[iPadMeasurementSelect alloc] initWithStyle:UITableViewStylePlain];
_measurementPicker.delegate = self;
}
if (_measurementPopover == nil) {
_measurementPopover = [[UIPopoverController alloc] initWithContentViewController:_measurementPicker];
[_measurementPopover presentPopoverFromRect:self.measurementSelect.frame inView:self.conversionView permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
else {
[_measurementPopover dismissPopoverAnimated:YES];
_measurementPopover = nil;
}
}
Every tap gets nslogged, so I assume my popover method is the culprit of this problem. Any ideas?
Let's rewrite by teasing apart existence of the UI elements and the visible state of the popover:
// canonical lazy getters for UI elements
- (iPadMeasurementSelect *)measurementPicker {
if (!_measurementPicker) {
_measurementPicker = [[iPadMeasurementSelect alloc] initWithStyle:UITableViewStylePlain];
_measurementPicker.delegate = self;
}
return _measurementPicker;
}
- (UIPopoverController *)measurementPopover {
if (!_measurementPopover) {
_measurementPopover = [[UIPopoverController alloc] initWithContentViewController:self.measurementPicker];
}
return _measurementPopover;
}
// now the show/hide method makes sense. it can take a bool about whether to show or hide
-(void)showPopover:(BOOL)show {
if (show) {
[self.measurementPopover presentPopoverFromRect:self.measurementSelect.frame inView:self.conversionView permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
} else {
[self.measurementPopover dismissPopoverAnimated:NO];
// if you want/need to create a new one each time it is shown, nil the popover here, like this:
// self.measurementPopover = nil;
}
}
When the textField begins editing, show the popover like this:
[self showPopover:YES];
And when the delegate gets the didEndEditing message:
[self showPopover:NO];
I have a view controller that's instantiated from IB. It contains a UIButton whose action creates a UIPopoverController whose delegate updates the title of the UIButton through:
- (void) popoverSelected:(NSString*)string {
[self.sortButton setTitle:string forState:UIControlStateNormal];
[self.sortPickerPopover dismissPopoverAnimated:YES];
}
popoverSelected is a delegate method for the UIPopoverController, which contains a simple UITableView.
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selectedSort = [_sortTypes objectAtIndex:indexPath.row];
if (_delegate != nil) {
[_delegate popoverSelected:selectedSort];
}
}
The popover is instantiated by the TouchUpInside action on the self.button through:
- (IBAction)sortButtonPressed:(id)sender {
if (_sortPicker == nil) {
// Create the picker view controller
_sortPicker = [[SortPickerViewController alloc] initWithStyle:UITableViewStylePlain];
// Set this as the delegate
_sortPicker.delegate = self;
}
if (_sortPickerPopover == nil) {
// The colour picker popover is not showing. Show it
_sortPickerPopover = [[UIPopoverController alloc] initWithContentViewController:_sortPicker];
[_sortPickerPopover presentPopoverFromRect:_sortButton.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
} else {
// if it's showing, we want to hide it
[_sortPickerPopover dismissPopoverAnimated:YES];
_sortPickerPopover = nil;
}
}
This has no issues the first time the button's title is updated, but second time around I get an EXC_BAD_ACCESS when executing setTitle: in popoverSelected.
I can't see anywhere that I'm releasing the button accidentally (and the object definitely still exists at this point). The project is using ARC.
With NSZombies I've occasionally reached [__NSArrayI valueRestriction] unrecognised selector sent to instance which makes even less sense.
Are there any obvious approaches I can take to debug this further?
Instead of checking _sortPickerPopover == nil to know whether to show it, you should check [_sortPickerPopover isPopoverVisible]. Also, I would put the construction code into autoloaders.
- (UIPopoverController *)sortPickerPopover
{
if (!_sortPickerPopover) {
_sortPickerPopover = [[UIPopoverController alloc] initWithContentViewController:self.sortPicker];
}
return _sortPickerPopover;
}
- (SortPickerViewController *)sortPicker
{
if (!_sortPicker) {
_sortPicker = [[SortPickerViewController alloc] initWithStyle:UITableViewStylePlain];
// Set this as the delegate
_sortPicker.delegate = self;
}
return _sortPicker;
}
- (IBAction)sortButtonPressed:(UIButton *)sender
{
if ([self.sortPickerPopover isPopoverVisible]) {
[self.sortPickerPopover dismissPopoverAnimated:YES];
} else {
[self.sortPickerPopover presentPopoverFromRect:sender.frame
inView:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
/***
* NOTE: Delegate methods should always pass the calling object as the first
* object. Additionally, the name is not very descriptive of what is actually
* being performed and does not use should/will/did naming conventions.
* You should consider changing this method to something like:
* - (void)sortPickerViewController:(SortPickerViewController *)sortPicker
* didSelectSortMethod:(NSString *)sortMethod
**/
- (void)popoverSelected:(NSString *)string
{
[self.sortButton setTitle:string forState:UIControlStateNormal];
[self.sortPickerPopover dismissPopoverAnimated:YES];
}
Once these changes are made, the only other possible source of problems is the implementation of your SortPickerViewController. I'll look that over for you if you can post that view controller as well.
I am trying to invoke -(BOOL) textFieldShouldClear:(UITextField *)textField when UITextField's clear button is tapped. I have already set my delegate and UITextField's other delegate's methods are being called correctly, except this one. Clear Button is set to "is always visible" in nib file.
EDIT
FYI I am showing FPPopover when textfield's text is changed. if I tap on clear button without showing popover, clear button works fine. But if I try to tap it when popover is being displayed, delegate method is not called.
Code Snippet
-(BOOL) textFieldShouldClear:(UITextField *)textField
{
return YES;
}
- (IBAction)didChangeScripText:(id)sender {
NSString *text = isPortrait ? symbolTextField.text : landsymbolTextfield.text;
if(scripList.count == 0)
{
if([Logs sharedManager].scripData.count > 0)
[self extractScrips];
else
return;
}
// SAFE_ARC_RELEASE(popover);
// popover=nil;
//the controller we want to present as a popover
if(controller == nil)
controller = [[scripComboViewController alloc] initWithStyle:UITableViewStylePlain];
if(controller.scripListFiltered.count > 0)
[controller.scripListFiltered removeAllObjects];
controller.delegate = self;
if(popover == nil){
popover = [[FPPopoverController alloc] initWithViewController:controller];
popover.tint = FPPopoverDefaultTint;
}
controller.scripListFiltered = [NSMutableArray arrayWithArray:[scripList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"SELF beginswith[c] %#",text]]];
NSLog(#"array is: %#",controller.scripListFiltered);
if(controller.scripListFiltered.count == 0)
{
[popover dismissPopoverAnimated:YES];
return;
}
//decide contentsize and arrow dir based on tableview height
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
popover.contentSize = CGSizeMake(300, 500);
}
else {
popover.contentSize = CGSizeMake(200, 200);
}
//sender is the uitextfield
float height = isPortrait ? portTable.frame.size.height : landTable.frame.size.height;
if(height > 0)
popover.arrowDirection = FPPopoverArrowDirectionDown;
else
popover.arrowDirection = FPPopoverArrowDirectionUp;
if(![popover isModalInPopover])
[popover presentPopoverFromView:sender];
[controller reloadTable];
}
What is going wrong? Can anyone tell me. Thanks.
Actually problem is due to FPPopover. When it receives touch event outside its view, it dismisses itself, and no interaction with outside controls is possible at that time. So if tap clear button, it will be used to dismiss the pop up and then I am able to use clear button. Thats all.