The problem I am experiencing is that when i try to pop a view controller using the default back swipeGesture in iOS7 viewDidDisappear of the present ViewController does not always get called after viewWillDisappear. I am using the UINavigationController as rootViewController.
App remain struck and does not receive any user inputs after this scenario. Sometimes app gets crashed, when i look at the log: it shows "Can't add self as subview' and in crash log, it showss EXC_BAD_ACCESS. How to fix this, but when i use back button in navigation bar app works normally.
- (void)viewWillDisappear:(BOOL)animated
{
// [self.navigationController.navigationBar setAlpha:1.0f];
[self createBarButtonITems];
self.navigationItem.title = #"Back";
}
- (void)viewDidDisappear:(BOOL)animated
{
[self zoomOutTableWithoutAnimation];
}
-(void)zoomOutTableWithoutAnimation
{
backgroundView.frame = CGRectMake(0,0,320,480);
backgroundView.transform=CGAffineTransformMakeScale(1, 1);
sideMenuTableView.transform=CGAffineTransformMakeScale(0.5,0.5);
sideMenuTableView.frame = CGRectMake(0,150,self.view.frame.size.width/2, self.view.frame.size.height);
sideMenuTableView.hidden = YES;
}
As you mentioned swipe back gesture, this is probably due to the interactive pop back.
As it is mentioned in WWDC 2013, session Custom Transitions Using View Controllers, you cannot assume a viewWillDisappear will be followed by viewDidDisappear. The same goes to viewWillAppear and viewDidAppear.
I'm not sure why you want to call
[self createBarButtonITems]
in viewWillDisappear, did you mean viewWillAppear?
Anyway, it seems to me that [self createBarButtonITems] did some side effect.
Try the following code in viewWillDisappear to undo the side effect:
- (void)viewWillDisappear
{
[self doSomethingHasSideEffect];
id <UIViewControllerTransitionCoordinator> coordinator;
coordinator = [self transitionCoordinator];
if(coordinator && [coordinator initiallyInteractive])
{
[coordinator notifyWhenInteractionEndsUsingBlock:
^(id <UIViewControllerTransitionCoordinatorContext> ctx)
{
if(ctx.isCancelled)
{
[self undoAnySideEffect]
}
}];
}
}
From your code what I can understand is you need a back button with title #"Back" instead of the previous view controllers title
just add this code tho your view controller, in which you trying to do the above stuff view did load method
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:(IS_IOS_7 ? #"" : #"Back") style:UIBarButtonItemStylePlain target:Nil action:nil];
self..navigationItem.backBarButtonItem = backButton;
Add [super viewWillDisappear:animated];
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self createBarButtonITems];
self.navigationItem.title = #"Back";
}
Related
I have a UIButton in my app that when pressed it shows the next view controller. Sometimes the UI locks up and the app freezes for a moment due to background processes. When this happens the user might tap the button multiple times because nothing happened immediately on the first tap, and when this occurs the UINavigationController pushes the ViewController again a bunch of times on top of itself, so that you have to go back several times to get back to home. Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
self.pushVCButton.multipleTouchEnabled = NO;
}
- (IBAction)pushVCButtonPressed:(id)sender {
self.pushVCButton.enabled = NO;
ViewController *viewController = [[ViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
self.pushVCButton.enabled = YES;
}
How do I get this to never push multiple instances of viewController?
You should really try to make the background process run not on UI thread, but if you can not try setting the button enabled to yes only when view did disappear or listen for completion of push animation:
- (IBAction)pushVCButtonPressed:(id)sender {
self.pushVCButton.enabled = NO;
ViewController *viewController = [[ViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
// Hack: wait for this view to disappear to enable the button
//self.pushVCButton.enabled = YES;
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated]
self.pushVCButton.enabled = YES;
}
Also make sure that the action is not called twice.
I have a trio of functions and a property that I use to control my popovers as follows:
-(void)dismissPopoverIfPresentAnimated:(BOOL)animated
{
if (self.currentPopover)
{
[self.currentPopover dismissPopoverAnimated:animated];
self.currentPopover = nil;
}
}
-(void)presentViewController:(UIViewController *)viewController inView:(UIView *)view fromRect:(CGRect)rect suppressArrow:(BOOL)suppressArrow
{
//Did the user just tap on a button to bring up the same controller that's already displayed?
//If so, just dismiss the current controller.
BOOL closeOnly = NO;
if (self.currentPopover)
{
UIViewController *currentController = [self.currentPopover.contentViewController isKindOfClass:UINavigationController.class] ? ((UINavigationController *)self.currentPopover.contentViewController).topViewController : self.currentPopover.contentViewController;
UIViewController *newController = [viewController isKindOfClass:UINavigationController.class] ? ((UINavigationController *)viewController).topViewController : viewController;
if ([currentController isKindOfClass:newController.class])
closeOnly = YES;
[self dismissPopoverIfPresentAnimated:NO];
}
if (!closeOnly)
{
self.currentPopover = [[UIPopoverController alloc] initWithContentViewController:viewController];
self.currentPopover.backgroundColor = [UIColor whiteColor];
[self.currentPopover presentPopoverFromRect:rect inView:view permittedArrowDirections:(suppressArrow ? 0 : UIPopoverArrowDirectionAny) animated:YES];
}
}
(instancetype) initWithContentViewController:(UIViewController )viewController
{
self = [super initWithContentViewController:[[UIViewController alloc] init]];
if (self)
{
UIViewController contentViewController = super.contentViewController;
[contentViewController addChildViewController:viewController];
[viewController didMoveToParentViewController:contentViewController];
[contentViewController.view addSubview:viewController.view];
[self setPopoverContentSize:viewController.preferredContentSize animated:NO];
}
return self;
}
This runs fine in iOS 7, but in iOS 8 the problem is there is a delay between the call to presentPopoverFromRect and when the item actually shows up onscreen. So, if a user double taps a button to show a popover, the first tap will properly dismiss, then "start" the showing of the new controller. The second tap will make the dismiss call (the popover is not yet visible) and then not show the new controller (this is a design feature so that click a button will show a popover, clicking it again will hide it).
The problem is that the call to dismiss the popover doesn't actually work and the popover will show up. At that point I can't get rid of it because my property is nil and I think it is not showing.
My guess is this is an iOS 8 bug where the dismiss somehow doesn't see a visible popover and thus doesn't do anything, where instead, it should prevent it from showing up.
Oh, one last note is that the call to presentViewController is always done on the main thread.
I am using pushViewController to push a view in my application. Pressing the back button works about 95% of the time like you would expect. But if I go in and out of the view as fast as possible I run into a condition where the top bar moves as if a pop has occurred, but the view says. In this state, I am left with a back button, (in normal operation I have changed the text of this button to 'cancel'). pressing back will animate the top bar again, and then I am left with no buttons in the top bar, and I'm stuck inside the view.
Do you have any idea what might be going on here? Here are some more details:
The sub view calls these once or twice:
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
also the sub view is extending a BaseViewController. Inside this base controller all of the view methods are overloaded (they just call super). The one that might be interesting is:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self customizeNavigationBar];
}
- (void)customizeNavigationBar
{
[self.navigationController.navigationBar setTintColor:UIColorFromRGB(kNavigationBackgroundColor)];
UIBarButtonItem *backButton_ = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"ID_BUTTON_BACK", #"") style:UIBarButtonItemStyleBordered target:self action:nil];
self.navigationItem.backBarButtonItem = backButton_;
[backButton_ release];
}
Please let me know if you need more code or if I can explain things better.
--- Edit ----
I also am calling Google Analytics in view will appear. I remember this causing other issues in my app:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSError *error;
if (![[GANTracker sharedTracker] trackPageview:#"/app_new_page"
withError:&error]) { }
}
This code is being put in my actual view (not BaseViewController).
I found the problem. The issue was that I was calling setNavigationBarHidden:NO with animated:NO in viewDidLoad to show the nav bar without animation, but using pushViewContoller with animated:YES.
----- originally -----
[self.navigationController pushViewController:controller animated:YES];
and
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
The solution was to remove setNavigationBarHidden from viewDidLoad and move it into viewWillAppear, and to animate it the same way the view was animated. Since my nav bar was appearing instantly, it was possible to press back before the view controller had finished animating (pushing onto the stack), causing all these issues.
----- solution -----
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
Thanks for your help guys!
Is it possible to display a modal view over the top of a UIWebView? I have a UIViewController that loads a WebView. I then want to push a Modal View Controller over the top so that a modal view covers up the WebView temporarily...
The WebView is working fine; here's how it's loaded in the View Controller:
- (void)loadView {
// Initialize webview and add as a subview to LandscapeController's view
myWebView = [[[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
myWebView.scalesPageToFit = YES;
myWebView.autoresizesSubviews = YES;
myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
myWebView.delegate = self;
self.view = myWebView;
}
If I attempt to load a Modal View controller from within viewDidLoad, however, no modal view appears:
- (void)viewDidLoad {
[super viewDidLoad];
// Edit dcftable.html with updated figures
NSMutableString *updated_html = [self _updateHTML:#"dcftable"];
// Load altered HTML file as an NSURL request
[self.myWebView loadHTMLString:updated_html baseURL:nil];
// If user hasn't paid for dcftable, then invoke the covering modal view
if (some_condition) {
LandscapeCoverController *landscapeCoverController = [[[LandscapeCoverController alloc] init] autorelease ];
[self presentModalViewController:landscapeCoverController animated:YES];
}
}
I suspect that there's something that needs to be done with the UIWebView delegate to get it to receive the new modal view...but can't find any discussion or examples of this anywhere...again, the objective is to invoke a modal view that covers over the top of the WebView.
Thanks for any thoughts in advance!
I ran into this same problem. Moving the presentModalViewController call from viewDidLoad to viewDidAppear did the trick.
I couldn't get it to work in viewDidLoad either, but I got it to work in a different method. Here's what I did:
- (void)viewDidLoad {
...
if (some_condition) {
[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:#selector(showModal) userInfo:nil repeats:NO];
}
}
- (void)showModal {
LandscapeCoverController *landscapeCoverController = [[[LandscapeCoverController alloc] init] autorelease ];
[self presentModalViewController:landscapeCoverController animated:YES];
}
Basically, it works if it's not being called in viewDidLoad. I wish I could explain why, I'm curious myself, but this should at least fix your problem.
Hope this helps!
I have a UITableViewController that launches a UIViewController and I would like to trap whenever the back button is pressed in the child controller, which is the class that derives from 'UIViewController'. I can change the Back Button title but setting the target & action values when setting the backBarButtonItem seems to get ignored. What's a way to receiving some kind of notification that the Back button was tapped?
- (void)showDetailView
{
// How I'm creating & showing the detail controller
MyViewController *controller = [[MyViewController alloc] initWithNibName:#"MyDetailView" bundle:nil];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Pages"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(handleBack:)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[self.navigationController pushViewController:controller animated:animated];
[controller release];
}
- (void)handleBack:(id)sender
{
// not reaching here
NSLog(#"handleBack event reached");
}
You can implement the viewWillDisappear method of UIViewController. This gets called when your controller is about to go away (either because another one was pushed onto the navigation controller stack, or because the 'back' button was pressed).
To determine whether the view is disappearing because of the back button being pressed, you can use a custom flag that you set wherever you push a new controller onto the navigation controller, like shown below
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (viewPushed) {
viewPushed = NO; // Flag indicates that view disappeared because we pushed another controller onto the navigation controller, we acknowledge it here
} else {
// Here, you know that back button was pressed
}
}
And wherever you push a new view controller, you would have to remember to also set that flag...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
viewPushed = YES;
[self.navigationController pushViewController:myNewController animated:YES];
...
}
It has been a while since this was asked, but I just tried to do this myself. I used a solution similar to Zoran's, however instead of using a flag I did this:
- (void)viewWillDisappear: (BOOL)animated
{
[super viewWillDisappear: animated];
if (![[self.navigationController viewControllers] containsObject: self])
{
// the view has been removed from the navigation stack, back is probably the cause
// this will be slow with a large stack however.
}
}
I think it bypasses the issues with flags and IMO is cleaner, however not as efficient (if there are lots of items on the navigation controller).
In my opinion the best solution.
- (void)didMoveToParentViewController:(UIViewController *)parent
{
if (![parent isEqual:self.parentViewController]) {
NSLog(#"Back pressed");
}
}
But it only works with iOS5+
I use this code:
- (void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound)
{
// your view controller already out of the stack, it meens user pressed Back button
}
}
But this is not actual when user presses tab bar button and pops to root view controller at one step. For this situation use this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(viewControllerChange:)
name:#"UINavigationControllerWillShowViewControllerNotification"
object:self.navigationController];
- (void) viewControllerChange:(NSNotification*)notification {
NSDictionary* userInfo = [notification userInfo];
if ([[userInfo objectForKey:#"UINavigationControllerNextVisibleViewController"] isKindOfClass:[<YourRootControllerClass> class]])
{
// do your staff here
}
}
Don't forget then:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:#"UINavigationControllerWillShowViewControllerNotification"
object:self.navigationController];
You can make your own button and place it as the leftBarButtonItem. Then have it call your method where you can do whatever, and call [self.navigationController popViewController... yourself
{
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(handleBack:)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
[self filldata];
[super viewDidLoad];
}
just replace backBarButtonItem with leftBarButtonItem
Simply use viewDidDisappear instead. It will be perfectly called in any scenario.
We are basing your lifecycle management on viewDidAppear and viewDidDisappear. If you know Android: the both are comparable to onResume and onPause methods. But there is a difference when it comes to locking the screen or pressing the homebutton on iOS.