I have scenario in that, On click of Tab I am calling viewWillAppear method and but after that when I press Back button It will directly back without any animation.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
switch(mytabbar.selectedIndex)
{
case 0:
[viewController viewWillAppear:YES];
break;
}
}
All thing working fine but I don't get Back button animation is only issue
You are not allowed to call view controller life cycle methods - like viewWillAppear and viewDidLoad. They automatically called throughout the life cycle of a view as and when needed and applicable.
You need to tell us what exactly you want to implement , then we can help you?
On click of back:
- (IBAction)onBackButtonClicked:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
Related
I would like to know when we are setting setSelectedIndex for uitabbbarviewcontroller,
Which delegate method will be called.
In my app, i have list of songs to purchase.when user taps buy button for any song, i will show the downloading tab.for that i am setting [self.tabBarController setSelectedIndex:3];there i am showing the song details and progressview that how much is downaloded etc.
This is fine upto this.in the meanwhile of downaloding poem the user can go and tap buy another poem.
here i want to reload the tableview.but i dont know in which delegate method i should call reload data for tableview.
I tried viewwillappear and viewdidappear. but these are not called.
So please tell me which delegate method will be called.
Thanks a lot
override UITabBarController setSelectedIndex:
-(void)setSelectedIndex:(NSUInteger)selectedIndex
{
//must call super function.
[super setSelectedIndex:selectedIndex];
[self myMethod];
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
[self myMethod];
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
is called only when user taps it, when you programmatically set this, it is not called.
You can fire a custom delegate method or notification as soon as you do it programmatically and do what ever you need to do...
I am using tab bar controller in my project.in each class's viewWillAppear i am parsing some data it takes some time to load. i put activity indicator before parsing the data in viewWillAppear but it is not working..and also itried below code also that too not worked what to do?
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController: (UIViewController *)viewController
{
[NSThread detachNewThreadSelector:#selector(threadStartAnimating) toTarget:self withObject:nil];
}
Finally i got the solution of my issue... i called the parsing class in view will appear method that makes me the problem..instead of calling the parsing class in view will appear method i created one user defined method(GetData). In that method i called the parsing class. in view will appear i called the user defined method(GetData) by following code.
[self performSelector:#selector(GetData) withObject:self afterDelay:1];
If I normally load up a UITabBarController and switch between views, it calls the viewWillAppear of each view controller.
But it is not so.
I want to switch to another tab as soon as another tab is touched.
If I specify a tab to load up - for example [self.tabBarController setSelectedIndex:0] in the viewWillAppear of one of the tabs (say tab 4)... It goes immediately back to tab 0.
But after that.... it does not call the viewWillAppear on any of the tabs when I switch between them.
For example, if I again go to tab 4, it does not come back to tab 0. I expect it to by a never ending cycle as I expect tab 0 to load up as soon as tab 4 is touch.
But it runs JUST ONCE !!
Why ??
Note: Question has been edited.
I think I found a solution. It works every time you click on your tab and it calls viewWillAppear on both tabs.
You can do this in your AppDelegate (or somwhere else in UITabBarController's delegate):
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
Sample code:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if([self.tabBarController.viewControllers indexOfObject:viewController] == 1) {
[self.tabBarController setSelectedIndex:0];
}
}
setting the selectedIndex won't change the selectedViewController. You will have to change selectedViewController itself. Look at the documentation for more details.
Hi
I am trying to add and remove tab bar elements dynamically. There are two arrays. One is shown first with an added tabbaritem with name "More" and other array is added to the tabbar when user presses More. User can come back to first array by pressing Less tabbaritem in second array. Problem is that when i frequently press More and Less tabbaritems in sequence More, Less, More, Less, More, Less - The app crashes after last Less. Array seems ok to me and so is tabbar controller. I am not able to figure out the problem.
Below is the code of tab bar delegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(#"selected view controller is :%#",viewController);
if(viewController.view.tag == -1){
[self.tabBarController setViewControllers:self.level2TabBarItems animated:YES];
[self.tabBarController setSelectedIndex:0];
}else if(viewController.view.tag == -2){
[self.tabBarController setViewControllers:self.level1TabBarItems animated:YES];
[self.tabBarController setSelectedIndex:0];
}
}
Can anyone please let me know where I am doing wrong?
Best Regards
I had similar problem. I guess that you construct new instance of VC in your array, so frequently switching more/less causes calling method from the old instance (is not replaced yet at that moment).
Unfortunatelly setViewControllers method (as documentation say) automatically remove old view controllers calling dealloc and it seems that there is no other way to reuse them.
In your case you can try to disable selecting tabs until tabBarController:didSelectViewController: execute implementing (I didn't test it):
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
self.selectLock = YES;
// your code
self.selectLock = NO;
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
return !self.selectLock;
}
Make comment NSLog here. This is not proper format to print this.
May be your array's are empty. Try to set a breakpoint and you will find the solution which line is causing the crash.
I think Both if and else if are not satisfied with this condition
Just Check your Tag with this NSLog(#"%d",viewController.view.tag);
I have a UINavigationController with a UITableView as my main menu. User clicks on a cell and a new view is pushed on the stack. In one case I push another UITableView that needs a toolbar. So on that 2nd tableView's init I setup the self.toolbarItems property with the correct items. But then I need to call [self.navigationController setToolbarHidden:NO animated:YES]; So it makes sense to call this in the viewDidAppear or viewWillAppear method. But I put it in those methods and find out (Also via NSLog) that they never get called. The same goes for hiding it in viewWillDisappear or viewDidDisappear. Why don't these methods get called? Where should I be doing this hiding/showing of the toolbar then?
I have noticed behavior where if a parent controller (like UINavigationController or UITabBarController) never get's viewWill/DidAppear called on it, it won't call it on the child controllers either. So make sure that in the code where you create the parent controller, you call viewWillAppear, show it, then call viewDidAppear. Then it should make those calls on it's child controllers as is appropriate.
Double check the parent controller is having those methods called, and call them yourself if they are not.
Yes Its true
you can do this by first write this code in
- (void)viewDidLoad {
self.navigationController.delegate = self;
}
And then write the code which you want to write in viewWillAppear
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([viewController isKindOfClass:[self class]]) {
//write your code here
}
}
Although you solved your problem, in case someone comes along in the future another problem could have been that you forgot the animated: argument to either method - that is to say, the format of the method needs to look like:
- (void) viewWillAppear:(BOOL)animated
i noticed the same issue in iOS7. When i'm using both tab bar (2 buttons A, B) and navigation controller.
A has two views. One with tableview and second displays data according to the selection from the table view.
B has is the only view.
Button which is refer to another separate view D, placed in both tab bar views (A & B) and in both views of A.
Problem arises when i click the button from tab item B, viewWillAppear and viewDidLoad not called.
So i solved this issue by presentModalViewController:animated: and to come back i used dismissModalViewControllerAnimated:, just when i go to view D from tab item B.