I have an Iphone application in which i have 3 tabitems with a tabbarcontroller.Inside the tabbarcontroller each viewcontroller is a navigation controller.when selecting the second tab i have a view controller.when selecting a button on that i am pushing another view controller to the self.navigation controller.and in that viewcontroller i am pushing and go like that.But the problem is when i am selecting the tabitem again that pushedviewcotrooller is shown there.but i need that rootview there again when i am selecting the tab,i tried like this in my code but not worked,`
-(void)tabBarController:(UITabBarController *)tabbBarController didSelectViewController:(UIViewController *)viewController
{
if(tabBarController.selectedIndex==0)
{
//[viewController.tabBarItem setImage:[UIImage imageNamed:#"pinboard_hvr.png"]];
}
else if (tabBarController.selectedIndex==1)
{
NSLog(#"%#",viewController);
//[viewController.tabBarItem setImage:[UIImage imageNamed:#"pinboard_hvr.png"]];
// NSArray *array = [viewController.navigationController viewControllers];
NSLog(#"%#",array);
// [self.navigationController popToViewController:[array objectAtIndex:0] animated:YES];
[viewController.navigationController popToRootViewControllerAnimated:YES];
//[appdelegate.navigationController popToRootViewControllerAnimated:YES];
}
else if (tabBarController.selectedIndex==2)
{
//[viewController.tabBarItem setImage:[UIImage imageNamed:#"pinboard_hvr.png"]];
}
}
`i have tried both with poping to root and also by taking the array of view controllers,but not worked.Can anybody help me to achieve this?
The argument you have received in delegate is itself a navigationController.
So, change statement like below,
else if (tabBarController.selectedIndex==1)
{
[((UINavigationController *)viewController) popToRootViewControllerAnimated:YES];
//[appdelegate.navigationController popToRootViewControllerAnimated:YES];
}
Me too had a similar problem which I solved by the following code.
-(void)tabBarController:(UITabBarController *)tabbBarController didSelectViewController:(UIViewController *)viewController
{
else if (tabBarController.selectedIndex==1)
{
NSArray *mycontrollers = self.tabBarController.viewControllers;
[[mycontrollers objectAtIndex:1] popToRootViewControllerAnimated:NO];
mycontrollers = nil;
}
}
Hope this helps you.
Related
Let me explain clearly.
I have a tabbarcontoller in the viewcontroller which is the main view controller of the single view application project.
I added a tabbarcontroller to the viewcontroller as the subview. In the tabbarcontroller, I added two navigation controllers like below image,
I have added three(named First, Second, Third) more viewcontrollers as new file.
If I navigate from one viewcontroller to other in a tab using below,
third = [[Third alloc] initWithNibName:#"Third" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:third animated:YES];
If I switch to next tab and come back to the previous tab, it should popto the previous view controller, how to do this?
I tried
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
not succeed,
I also tried,
[third popto];
In the third viewcontroller,
-(void)popto
{
[self.navigationController popViewControllerAnimated:YES];
}
nothing happened.
Now, I have to click the tab again to poptoviewcontroller to the first viewcontroller.
Any ideas will be highly appreciated.
You should try using
[viewController.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
instead of
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
Try the below code snippet
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
UINavigationController *navController = (UINavigationController*)viewController;
if (navController && ([[navController viewControllers] count] > 0))
{
[navController popToRootViewControllerAnimated:NO];
}
return YES;
}
Hope it may work for you.
How about overriding UITabbarController for a custom one and implement the following method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
Next loop trough all the 'viewcontrollers' of the tabbar (in this case the navigation controllers) and call on all the navigation controllers popToRootViewController.
This might work.
I have an application in which i am having a table view.when pressing a button on the cell i have to go to another view controller(ie:testviewcontroller) showing what i selected in the previous one.then when pressing one button i need to go to another view controller showing the remaing values.if he select one there then i need to repeat the above process.The problem is from here also i am carrying some values to that testviewcontroller.if i pop back to my view controller how can i carry the new values.currently i am doing like this.
TestViewController *test =[[ TestViewController alloc]initWithNibName:#"TestViewController" bundle:nil];
test.itemselected=head;
test.itemid=productid;
//NSLog(#"%#",del.navigationController);
[del.navigationController pushViewController:test animated:YES];
but i know
NSArray *array = [del.navigationController viewControllers];
[array objectAtIndex:3]is my desired view controller.
can anybody know how can i avoid this pushing of same view controller again?
for (UIViewController*vc in [self.navigationController viewControllers]) {
if ([vc isKindOfClass: [TestViewController class]]){
vc.itemselected= head ;
[[self navigationController] popToViewController:vc animated:YES];
}
}
*EDIT*
This should be
for (TestViewController*vc in [self.navigationController viewControllers])
instead of
for (UIViewController*vc in [self.navigationController viewControllers])
You can traverse loop on view controller stack.
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
NSMutableArray *ViewControllerArray=[[NSMutableArray alloc]init];
for (long i=[self.navigationController.viewControllers count]-1;i>0; i--) {
[ViewControllerArray addObject:[self.navigationController.viewControllers objectAtIndex:i]];
NSLog(#"%#",ViewControllerArray);
NSLog(#"%#",self.navigationController.viewControllers);
}
for (UIViewController *controller in self.navigationController.viewControllers) {
//Do not forget to import AnOldViewController.h
if ([controller isKindOfClass:[YourViewController class]]) {
[self.navigationController popToViewController:controller
animated:YES];
break;
}
}
}
[super viewWillDisappear:animated];
}
For your condition it is beter to pop back and reload the table with new contents..no need to push to a new instance
Use a seperate array for datasource of table
Remove the selected item from datasource array
Then push to next view when poped back reload contents
I have a view controller inside a tab bar controller. When I'm "inside" the initialized view I want to be able to press the tab bar item again and redraw the view.
My tabbarcontroller is created in the AppDelegate
#AppDelegate.m
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSString *titleV = viewController.title;
if (titleV == #"Random") {
DetailViewController *detailViewController = [[DetailViewController alloc] init];
[detailViewController reloadView];
}
}
#ViewController.m
-(void)reloadView{
[self.view setNeedsDisplay];
NSLog(#"view updated");
}
//code
- (void)viewDidLoad
{
[super viewDidLoad];
[self checkContent];
NSLog(#"viewDidLoad");
}
//code
-(void)checkContent{
if (theContent==NULL) {
contentText.numberOfLines=0;
contentText.text = randomContent;
NSLog(#"%#", contentText.text);
} else {
contentText.text = theContent;
}
}
From the log I can see that contentText.text gets updated though the visible label does not until I move to another view and then back again. I'm not sure why this isn't working. Any ideas on how to solve this are greatly appreciated.
If you need more code I'd be happy to provide it.
Cheers,
Dubbelsnurr
Instead of putting - tabBarController:didSelectViewController in appDelegate, I would sub-class my tabBarController and conform to UITabBarDelegate, and call - tabBarController:didSelectViewController from within that.
Here is a tutorial that implements a similar concept:
http://iosdevelopertips.com/user-interface/detect-taps-on-uitabbarcontroller-and-determining-class-type.html
I'm building an iPhone app, with the following structure:
I have the MainViewController which consists of 2 views (like split screen).
The first view of them, has a button. On tap, a UItableView (ResultTableViewController) appears in the second (of the above) view:
-(IBAction)buttonTapped:(id)sender {
if ([(UIButton *)sender tag] == 0) {
ResultsTableViewController *childViewController = [[ResultsTableViewController alloc] init];
childViewController.tableView.delegate = self.results;
[self.results.view addSubview:childViewController.tableView];
}
}
So I have a UItableView as a sub-view of a UIView.
The problem is that pushViewController() in didSelectRowAtIndexPath() of ResultTableViewController does not work (self.navigationController is nil).
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailsViewController *detailView = [[DetailsViewController alloc] initWithNibName:#"DetailsViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:self.detailView animated:YES];
}
I have tried many of the solutions I found, but nothing works.
In my MainWindow.xib, I have only MainViewController added, is that the problem?
Thanks in advance!
You are adding the view of the child controller to your controller's view, not pushing the child controller onto your navigation stack. Due to that, your child controller's navigation controller will be nil, since it wasn't put into the navigation controller.
Is this what you're going for?
-(IBAction)buttonTapped:(id)sender {
if ([(UIButton *)sender tag] == 0) {
ResultsTableViewController *childViewController = [[ResultsTableViewController alloc] init];
childViewController.tableView.delegate = self.results;
[self.navigationController pushViewController:childViewController animated:YES];
}
}
[self.navigationController pushViewController:self.detailView animated:YES];
above line of code will push the detailview on navigationcontroller stack. Check whethere your tableviewcontroller is on that stack ?
Ok, I found it.
I had to declare my MainViewController as UINavigationControllerDelegate and create a secondary NavigationController in it. I push the viewController in my new navigationController and that's it.
hi
In my application i am using TabBar.At first when I call loginControl,I am hiding the tab bar using [login hidesBottomBarWhenPushed=YES]; now if login is a success then I show detailViewController but I do not see the TabBar even after setting hidesBottomBarWhenPushed to NO.
what's the problem... can any one help me??
If you don't need your login view controller anymore you can pop it before push the second. This will do the work but there is strange animation in the back button. It still a solution :)
[navController popViewControllerAnimated:NO];
Edit:
Try this....
loginSuccessController *login = [[loginSuccessController alloc] initWithNibName:#"loginSuccessController" bundle:nil];
[self.navigationController popViewControllerAnimated:NO];
[self.navigationController pushViewController:login animated:YES];
can you try [self setHidesBottomBarWhenPushed:NO]; in your loginsuccesscontroller's viewwillappear or viewdidload
Use this method for hide and show tabbar :)
-(void)makeTabBarHidden:(BOOL)hide
{
UITabBarController *tabBarController = self.tabBarController;
if ( hide == tabBarController.tabBar.hidden ) {
return;
}
UIView *contentView;
if ( [[tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] ) {
contentView = [tabBarController.view.subviews objectAtIndex:1];
} else {
contentView = [tabBarController.view.subviews objectAtIndex:0];
}
if (hide) {
contentView.frame = tabBarController.view.bounds;
}
else {
contentView.frame = CGRectMake(tabBarController.view.bounds.origin.x,
tabBarController.view.bounds.origin.y,
tabBarController.view.bounds.size.width,
tabBarController.view.bounds.size.height - tabBarController.tabBar.frame.size.height);
}
tabBarController.tabBar.hidden = hide;
}