Cocoa Touch UITabBar action on tab switch - objective-c

I have a UITabBar with a number of UITabBarItems. I've assigned a different view controller to each of these.
I want to load some data etc., when each button is clicked. Therefore I will like to know where to put that code? I tried implementing viewWillAppear and viewDidLoad in the view controller but those didn't get called.

I know that when you assign a delegate to a UINavigationController, the component UIViewController delegate methods are not called. It seems likely that the same is true of UITabBarController.
I would try implementing the UITabBarControllerDelegate protocol and implementing the tabBar:didSelectViewController: method.

Are you sure your custom controllers inherit from UIViewController?
Don't forget the signature for the viewWillAppear is viewWillAppear:animated:
I'm working off the beta 5 right now, and it works just fine (I'm also using a TabBar).

Write your code in viewdidappear function.but i didn't try

Related

When will viewWill/DidAppear/Disappear is called anyway and how exactly does it work?

I understand that viewWillAppear will be called when duh.... when the the view is about to appear.
But how does IOS know that a controller's view is about to appear?
When exactly that and how it is implemented?
For example, does the childController.view check first that window is one of it's super ancestors? Does the view has a pointer to it's controller? How exactly that works? Does everytime a view is added it check whether it's window is it's super ancestor and whether it is the view outlet of a UIViewController?
For example, if I add childcontroller.view but not to a subview of any view that's being called. Will viewWillAppear called?
Does the childController need to be the a child of a parentController so that viewWillAppear of the childController will be called when the parentController's viewWillAppear is called automatically?
The view is loaded by your controller using the - (void)loadView method. This method is implemented to load a blank view or a view from a nib/storyboard. You only need to override it if you really need to create a view hierarchy from scratch.
All of the magic happens when the value of the view property is first requested and the controller detects the value is nil. All of the life cycle method calls are handled by the UIViewController. There is nothing you need to do other than implement the methods if you need them. Remember one thing: There is no guarantee the view has been loaded until the - (void)viewDidLoad method has been called.
Everything I've learned about controllers how they work has come from the View Controller Programming Guide.

What's the proper way to reload a UIView?

While I was working with ios apps suddenly one question struct in my mind. What if I want to reload whole view? What will happen if I invoke viewDidLoad? Is that the right way to do this? I'm asking just for my knowledge only.
If the view is the main view of some controller, you can reload it by setting the controller’s view property to nil – it will be loaded again the next time somebody uses the view property. Invoking -viewDidLoad would not work, it’s just a callback method called by some UIViewController code when the view is finished loading.
You may use following simple code to reload view.
[YOUR_VIEW setNeedsDisplay];
If you want to reload the whole view, you can use reloadInputViews method or write the code in viewWillAppear , so everytime you make a new object of your viewController, the code in viewWillAppear will recall itself.

UIViewController within a UIViewController

So I have a viewControllerA, and I want to add another View managed by viewControllerB to it. There is only one UISlider activating a simple action in viewControllerB. It won't crash if I don't touch this UISlider, it will once I use UISlider. I am using ARC. I am using:
[self.view addSubView: viewControllerB.view];
to add viewControllerB to viewControllerA. Am I missing something? Thanks.
OK. It looks like a really simple situation. I just added one view controller and one action. Here is the demo project code on github: https://github.com/randomor/Demo
The reason why I want this to work is because I have another app that will create a view controller on the spot and add it to anther view. And I don't want to do it modally, because I don't want the new view controller to cover the whole screen. Thanks.
SOLUTION: So I'm now just using the latest ViewController containment API:
[self addChildViewController:viewControllerB];
It works! as long as I added this line, the event will be passed to its own controller and it stopped crashing.
i recommend you, to use the following code
in ViewControllerA.h
#import "ViewControllerB.h"
in ViewControllerA.m (where you want to push the new controller)
ViewControllerB *newController = [[ViewControllerB alloc]init];
[self presentModalViewController:newController animated:YES];
in ViewControllerB.m you will need
[self.presentingViewController dismissModalViewControllerAnimated:YES];
to make it vanish again.
concerning multiple controllers for one open screen (Apple ViewController Programming Guide):
Each custom view controller object you create is responsible for managing exactly
one screen’s worth of content. The one-to-one correspondence between a view controller
and a screen is a very important consideration in the design of your application.
You should not use multiple custom view controllers to manage different portions
of the same screen. Similarly, you should not use a single custom view controller
object to manage multiple screens worth of content.
You should try and avoid the practice of nesting UIViewControllers. While it is technically supported in iOS5, it is ill-advised, for many reasons, including the type of problem that you're having (you have a dangling pointer to a UIViewController, which is why you are crashing).
http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/
Although this question is extremely vague, I imagine that you are not keeping a reference to View Controller B, and so when view B tries to interact with it, it causes EXC_BAD_ACCESS.
What's the object that is set as the target for the slider? If it's a EXC_BAD_ADDRESS, then you may not be retaining the target, most probably the view controller for the slider.

Question regarding Tab bar controller

I am using [self.view addSubView:tabBarController.view];
But because of this viewDidAppear and viewWillAppear is not getting called so if I want to reset my view or update the contents of it, its not happening. Can u help me?
HI After some googling I have found the solution for the question. If you are using tabbar controller....then u can use :-
(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
method for invoking any function from other viewControllers.
Just you have to give
[viewController functionname];
that's it. Hope this post might help someone.
If you're linking against iOS 4.x SDK and lower, you should avoid using nested UIViewControllers.
But if it is really necessary, you should manually call viewWillAppear: (and all others) and make sure parentViewController property of your child view controller is referenced to UIViewController subclass that owns it.
P.S. If you'd like to add UITabBarController to fill all window contents, you can present it as modal view controller over another view controller or use rootViewController property of UIWindow class

Calling an instance method from another class

Consider this view setup :
I have a view controller which switches between a set of sub views. Each sub view is a UIView subclass with custom code. To switch views I use a switch statement which allocs the new view as the currentview. This works very well.
I'm now in a position where I have a view (MainMenu) with a sub view (PopUp) that contains a UITableView. The PopUp view is shown and hidden via instance methods of the MainMenu.h class. Lets call the methods showPopUp and hidePopUp.
When a user selects an item from the UITableView they then have to manually close the containing (PopUp) view by clicking the close button, which is bound to the hidePopUp method.
What should happen when a user selects an item in the UITableView is that the hidePopUp method should be triggered automatically.
How do I trigger the hidePopUp instance method via the didSelectRowAtIndexPath of the UITAbleView? Is this a job for an app delegate, or perhaps NSNotificationCenter? I've tried such things as calling
[[[UIApplication sharedApplication] delegate] closePopUp];
from the didSelectRowAtIndexPath to no avail...
Thanks in advance, it's probably something simple I'm missing. Programming with a flu is difficult!
There are a few ways to accomplish this, such as notifications or working through a singleton like the app delegate (although the use of the singleton [anti]pattern is not without controversy). Personally, I'd use delegation.
Something like:
#protocol PopUpDelegate
#optional
- (void)Popup:(YourPopUpClass *)popUp didEndWithData:(NSData *)blah;
#end
You could then implement this protocol in your MainMenu, assign it as Popup's delegate, have the Popup call the delegate's method when the close button is pushed, and close the popup from there.
Here's a great post on how to implement delegates if you choose to go this route: How do I create delegates in Objective-C?