view events not being called in UITabBarController - iphone

i've got a little problem with my custom UITabBarController class. My UICustomTabBarController is a subclass of UITabBarController. In my didSelectItem event I implemented the following code:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
[self showActivityIndicator];
}
I my showActivityIndicator method I add a activity indicator to my current view. It works just fine.
Now i would like to remove the activity indicator when the current view will disappear.
i found the following events:
-(void)viewDidDisappear:(BOOL)animated {
NSLog(#"hello");
}
-(void)viewWillDisappear:(BOOL)animated {
NSLog(#"hello");
}
-(void)viewWillAppear:(BOOL)animated {
NSLog(#"hello");
}
-(void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
NSLog(#"hello");
}
Unfortunately none of them are working. They are not being called. Am I doing anything wrong?
Thanks for your help!

Is the delegate property for UITabBar is set in the .h file ? ?
i.e. <UITabBarDelegate,UITabBarControllerDelegate>

Related

Get the Identifier of the current tab - iphone

Probably a simple question, but I'm having a hellofa time finding a solution to it.
I need to find the identifier of the current tab in a tabbarcontroller and use it in a conditional to run a method.
how do I find this?
if (self.tabbarcontroller.identifier == #"My identifier") {
// do some method
} else {
// do the default method
}
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UITabBarController *tabBarController = (UITabBarController*) window.rootViewController;
UIViewController *selectedVC = tabBarController.selectedViewController;
if ([selectedVC.identifier isEqualToString:#"anIdentifier"])
{
// Do something
} else {
// Do something else
}
You can set the identifier of a ViewController in your storyboard
Check out the following code. Also make sure that the delegate for the UITabBar is correct pointing to the view controller, in this case FirstViewController.
**FirstViewController.h****
#interface FirstViewController : UIViewController<UITabBarDelegate>
**FirstViewController.m:**
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSLog(#"%#",[item tag]);
}

Not receiving UINavigationBarDelegate callbacks

Is there a way to troubleshoot not receiving the NavBarDelegate callbacks? I tried in a test project to just do:
[self.navigationController.navigationBar setDelegate:self];
in viewDidLoad and I did receive the callbacks for:
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
NSLog(#"%s", __FUNCTION__);
return YES;
}
- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item {
NSLog(#"%s", __FUNCTION__);
}
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
NSLog(#"%s", __FUNCTION__);
return YES;
}
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item {
NSLog(#"%s", __FUNCTION__);
}
I did check that my ViewController conforms to this protocol in the interface .
In viewWillAppear:, I check if my class conforms to the protocol with:
if ([self conformsToProtocol:#protocol(UINavigationBarDelegate)]) {
NSLog(#"yes I conform");
}
And I do get the NSLog message saying my class conforms, but I do not get the callbacks. As it works in a test project, and it doesn't work here, I'm trying to figure out other ways to troubleshoot this. Any thoughts? Thanks.
I am leaving this for future anyone who will encounter this issue:
You need to place these <UINavigationBarDelegate> protocol methods inside your Navigation Controller's class, not inside a view controller's class you are working on.
In my case I just have a class NavigationController (subclass of UINavigationController), singleton instance of which: sharedNavigationController works for my entire app. I have set it to conform <UINavigationBarDelegate> protocol and declared these methods in it. All worked fine since then.

duplicate declaration of method dismissviewdidfinish

Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two actionsheet buttons and for modalviewcontroller, can i have multiple dismissviewdidfinish method for each
-(void)dismissViewDidFinish:(ModalViewController *)controller
{
[self dismissModalViewControllerAnimated:YES];
}
-(void)dismissViewDidFinish:(Devanagari *)controller1;
{
[self dismissViewControllerAnimated:completion];
}
-(void)dismissViewDidFinish:(English *)controller2;
{
[self dismissViewControllerAnimated:YES];
}
Cause if i add these three methods on mainviewcontroller i get red warning message duplicate declaration of method dismissviewdidfinish.
Any ideas how to solve this kind of situation.
You cannot have the same name for more than 1 method. Use a single dismissViewDidFinish:(UIViewController *)viewController method and then check to see which viewController finished:
- (void)dismissViewDidFinish:(UIViewController *)viewController {
//check to see what kind of class viewController is
//or use tags by setting the viewcontroller.view.tag when creating it
}

trying to update a UILabel on a parent view controller when dismissing the modal view

I am trying to update a UILabel in a parent View after someone makes a change in a modal view. So, after they click "save" ... the newly entered value would change what text is displayed on the parent view controller.
But, I can't seem to get that UILabel to refresh the newly entered value.
Any ideas on what I can try? I've tried a few things, but being the view is already loaded, nothing is getting "refreshed".
Thanks!
There are many ways to do this. One way is to use NSNotificationCenter to be able to do calls between different classes. So in the parent view you will have a function responsible for the update (lets call it updateLabel) and you will do the following:
- (void) updateLabel
{
yourLabel.text = #"what you need";
}
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateLabel) name:#"DoUpdateLabel" object:nil];
}
Now in other view simply post a notification in the save button:
[[NSNotificationCenter defaultCenter] postNotificationName:#"DoUpdateLabel" object:nil userInfo:nil];
EDIT:
I have to mention 2 things here:
In this scenario it is always preferable to have Shared Data Modal where you save your data in so you can access this data in any view in your program. In other words it is a good practice to separate the data from classes.
Remember to resomve the NSNotificationCenter that you used in the main view by adding [[NSNotificationCenter defaultCenter] removeObserver:self];
To elaborate on my comment. This is how I would implement a delegation method to update the label.
In the header of the parent view controller:
#import "ModalViewController.h"
#interface ViewController : UIViewController <ModalViewControllerDelegate>
/* This presents the modal view controller */
- (IBAction)buttonModalPressed:(id)sender;
#end
And in the implementation:
/* Modal view controller did save */
- (void)modalViewControllerDidSave:(ModalViewController *)viewController withText:(NSString *)text
{
NSLog(#"Update label: %#", text);
}
/* Prepare for segue */
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"modalSegue"])
{
ModalViewController *mvc = (ModalViewController *) segue.destinationViewController;
mvc.delegate = self;
}
}
/* Present modal view */
- (IBAction)buttonModalPressed:(id)sender
{
[self performSegueWithIdentifier:#"modalSegue" sender:self];
}
Here you see the delegation method in the top.
The header of the modal view controller would contain the delegation protocol like this:
#protocol ModalViewControllerDelegate;
#interface ModalViewController : UIViewController
#property (nonatomic, weak) id <ModalViewControllerDelegate> delegate;
- (IBAction)buttonSavePressed:(id)sender;
#end
#protocol ModalViewControllerDelegate <NSObject>
- (void)modalViewControllerDidSave:(ModalViewController *)viewController withText:(NSString *)text;
#end
The implementation of the modal view controller would contain a method similar to this one:
/* Save button was pressed */
- (IBAction)buttonSavePressed:(id)sender
{
if ([self.delegate respondsToSelector:#selector(modalViewControllerDidSave:withText:)])
[self.delegate modalViewControllerDidSave:self withText:#"Some text"];
[self dismissModalViewControllerAnimated:YES];
}
When the save button is pressed, the delegate is notified and the text in your text view is sent through the delegation method.
in SWIFT:
ParentViewController :
func updateLabel() {
yourLabel.text! = "what you need"
}
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.updateLabel), name: "DoUpdateLabel", object: nil)
}
In OtherView:
#IBAction func closePopUp(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName("DoUpdateLabel", object: nil, userInfo: nil)
}

ViewController not responding to didRotateFromInterfaceOrientation

My view controller is not responding to didRotateFromInterfaceOrientation, despite that I have added following in my code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[self.popOver dismissPopoverAnimated:NO];
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
... My Custom Code...
}
}
Am I doing something wrong here?
If you can't inherit from UIViewController (which is unfortunate), you can use this:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
Then register to start receiving UIDeviceOrientationDidChangeNotification notifications.
If your UIViewController is a child in some root view then IB does not add it as a child controller to the root controller by default. The easiest way to address this is to modify your root controller:
- (void)viewDidLoad
{
[super viewDidLoad];
[self addChildViewController:(UIViewController*) self.yourChildController];
}
This should do the trick. Now your child controller will be receiving both:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
and
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
messages.
I think the real answer here (more accurately the answer to the linked question) is that you need to call
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
in your subclass implementation of the didRotateFromInterfaceOrientation method. For example:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
// Then your code...
}
This is not mentioned in the apple documentation but caused some serious and unexplained problems for me when omitted...