Objective-C Detect a viewDidAppear from 'Back' button? - iphone

I currently have a UIViewController "Radio" with a button on it.
When the button is clicked, the "Radio" viewcontroller pushes another UIViewController called "details"
When the user hits "Back" and returns back to the "Radio" view controller, I need a method to detect that it did so, to prevent refreshing the View.
Does anyone know how I can do this?

In the Radio class:
- (void) viewDidAppear:(BOOL)animated {
NSLog(#"viewDidAppear");
}

Related

Unable to remove BackButton in Navigation controller using Storyboard in ios

I am new to iOS,
I am developing shopping App for IPhone using Storyboard,
Here is the snapshot of my App,
TabBarController contains 4 Tab with NavigationController
When i open Tab1(let say class1) it contains TableView, onclick of tableview it will take me to Detail page with title and back button on NavigationBar (I am adding title and back button programatically in ViewWillAppear method) after this when i hit back button i navigates to previous page properly, this is working fine..
My problem is When i open Tab1(i.e class1) and when i navigates to Detail page after selecting a row in tableview, in a Detail page, BackBtn and title will be added in NavigationBar bcoz ViewWillAppear method will be called, and when i hit Tab2 before hitting Backbtn, i am navigates to class of Tab2 and thn when i comes back to Tab1 and now i clicks on back button i am navigating to previous page of my Tab1 class (i.e class1) but on class1 back Button and title of Detail page is there on my class1 i am unable to hide it...
You can see in the 2nd image BackBtn and Title is there in Class1..
What's the problem ?
just hide that backButton in viewWillAppear: method of Class1 like bellow...
[self.navigationItem setHidesBackButton:YES animated:YES];
or
[self.navigationItem setHidesBackButton:YES];
UPDATE:
if you add custom button to the UINavigationBar then just remove that button like bellow...
self.navigationItem.leftBarButtonItem = nil;
and if you want to remove right bar button then use bellow another code like above...
self.navigationItem.rightBarButtonItem = nil;
i hope this helpful to you...
[btnBack setHidden:YES] in viewWillDisapper: method in Detail page
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
Will give you which item you are currently selecting.
You can work around if you cant fix it by popping to root view controller and pushing it again. And make sure you are not adding a subview in place of pushing a view controller.

UITabbarController with MoreNavigationController , PoptoRootViewController issue

Kindly help me with this issue .
I am using tabbarcontroller in my App,
[tabBarController setViewControllers:tabs];
tabs Contain array of viewcontrollers (6 viewcontrollers).
It automatically created more button.
ISSUE
When I open any viewcontroller from more button and then open any other controller from index 0 to 2 , and then press more button it maintain the last opened viewcontroller .
For Example:
more button tableviewcontroller
screen :
Now when i press Contacts let say
Now when user press any other tabbar like feature tab bar
Now when user go back to more tab it shows the contact's viewcontroller
But i want the app to poptorootviewcontroller when user back again to more tabbar , and simply more tableviewcontroller.
You can do this by in the ViewWillDisappear method of view controller in More tab, call method to pop this view out of MoreViewNavigationController, like this:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController popViewControllerAnimated:NO];
}
May be too late but here it is for future reference
UITabController has a tabBar property and it has a delegate which tells you when a tabitem is tapped
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
Tab bar also has another property "items" which lists the visibile tabs. Find the index of the selected tab item in items in the delegate method implementation and if the index is 4 which is more button then call tab [controller.moreNavigationController popToRootViewController]

Custom behavior when "back" button in nav bar pressed

I know that you can add a custom "back" button to a UINavigationBar, but doing so removes the existing button with the tapered left side, as described in What happens when back button is pressed in navigationBar
Is there any way to keep the existing behavior and look of the back button, but also be informed when it is pressed?
Basically, I want my app to play a sound whenever any button is touched. I can do it with UITabBarController via its delegate, but the delegate for UINavgationBarController has no such functionality.
The following are the possibilities for a View Controller to disappear.
Clicking on a button (other than backBarButtonItem).
Clicking on the backBarButtonItem
Either case viewWillDisappear: method will be called. Keep a boolean flag. Lets name it isAnyButtonClicked. And set isAnyButtonClicked = YES whenever any button is clicked. And override the viewWillDisappear: method, and play the sound if no button is clicked(ie., isAnyButtonClicked == NO). Probably your viewWillDisappear: would look like,
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (!isAnyButtonClicked) {
// Play sound
isAnyButtonClicked = NO;
}
}

iphone connecting code to the 'back' button

Is there a way to connect code to a view's 'back' button that is part of a navigation controller? That back button is automatically coded in by the nature of it being a navigation controller so I am not sure how I can connect code to it.
(I know this should be really easy but I can't seem to find it in the documentation on navigation controller.)
viewWillDisappear is where you would typically add code to execute when the back button is pressed
In reality you should not have a code that needs to be executed when Back button is pressed. What exactly you're trying to achieve?
You could check if the controller was popped in viewWillDisapper, like in this example:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
NSLog(#"Article done");
}
}

How to popViewController from button click of custom UITableViewCell

I have a button in a custom UITableViewCell. Its UITableView is controlled by a UIViewController. I would like to return to the RootView when that button is clicked.
I am trying to use
[self.superview.navigationController popViewControllerAnimated:YES];
in the target Action of the button, which is in the UITableViewCell.m file. However, it doesn't recognize "navigationController" because it is not in the stack.
How can I return to the RootView when that button is clicked?
Views have no explicit relation to controllers and views cannot be in the controller stack. Set the button target to be a UIViewController instance and in the action method call:
[self.navigationController popToRootViewControllerAnimated:YES|NO];