Reload uiview when pressing tab item inside initialized view - iphone

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

Related

How to load my rootview everytime when the tab is selected?

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.

Adding subViews to a viewcontroller

I am having an issue with trying to load a viewcontroller onto another viewcontroller as a subview.
what I have is a NavigationController that loads some viewControllers in as views (pop and push etc) that works perfectly. then I have decided to put a tabBar into a viewController which then looks after all of the selection stuff using a switch statement, this switch statement then calls a method inside one of the viewControllers that appears inside the navigationController.
The method inside this viewController then trys to set another viewcontroller as a subview to the viewcontroller thats inside the navgiation controller.
this is my code.
TabBarViewController.m
#import "DetailViewController.h"
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
switch (item.tag) {
case 0:
{
NSLog(#"item 1 selected");
DetailViewController *dVC = [[DetailViewController alloc] init];
[dVC tabBarSelectedAction];
}
break;
default:
break;
}
}
so this catches the selected item on the tab bar... then fires off a msg to the DetailViewController method to load the new subview onto DetailViewController.view
- (void)tabBarSelectedAction
{
ButtonOneViewController *b1VC = [[ButtonOneViewController alloc] initWithNibName:#"ButtonOneViewController" bundle:[NSBundle mainBundle]];
[self.testView addSubview:b1VC.view];
}
and this is where I am trying to load the subview onto the screen.. I think I am doing it right but for some reason its not displaying.. another thing I would like to do is animate this view from the bottom of the screen up..
any help would be hugely appreciated.
When you created your new DetailViewController you didn't make it part of the view hierarchy through a push or present type of method. Adding a subview may or may not be working but you won't see it because the object you're adding it to isn't using the screen.
Your method should probably look like this. Assuming self DetailViewController.
- (void)tabBarSelectedAction {
ButtonOneViewController *b1VC = [[ButtonOneViewController alloc] initWithNibName:#"ButtonOneViewController" bundle:[NSBundle mainBundle]];
[self presentModalViewController:b1VC animated:YES];
}
Even with that, I think your logic is a little screwed up. You allocate and initialize DetailViewController but you never present it anywhere. So how are you expecting to see a modal view in DetailViewController, if you never present it.
EDIT: Taking into consideration your comment of adding it to the UINavigationController, you would change it to look something like this..
[[self navigationController] presentModalViewController:b1VC animated:YES];
EDIT2: Also, you're initializing a class, just to call a method which is already self. Your -didSelectItem: method should look more like this.
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
switch (item.tag) {
case 0:
{
NSLog(#"item 1 selected");
[self tabBarSelectedAction];
}
break;
default:
break;
}
}

hidesBottomBarWhenPushed but when popped

I've a problem with something that seems to be very simple.
My app has a view hierarchy consisting in a UITabBarController containing UINavigationControllers. When I navigate from the root to the second level
I set the hidesBottomBarWhenPushed on true so that the tab bar is hidden
On my firstLevelController:
[secondLevelController setHidesBottomBarWhenPushed:YES];
[self.navigationController pushViewController:secondLevelController animated:YES];
After that when I push to the third level, I bring the tab bar again by doing in the secondLevelController:
[self setHidesBottomBarWhenPushed:NO];
[thirdLevelController setHidesBottomBarWhenPushed:NO];
[self.navigationController pushViewController:thirdLevelController animated:YES];
(I know, I didn't like the [self setHidesBottomBarWhenPushed:NO] either, but it didn´t work otherwise...)
So, here is the problem: when I push the back button in the third level and the second view appears, I need to hide the tabbar again but I couldn´t find the way of doing this.
Any help is appreciated
This is what works for me.
[self setHidesBottomBarWhenPushed:NO];
[thirdLevelController setHidesBottomBarWhenPushed:NO];
[self.navigationController pushViewController:thirdLevelController animated:YES];
[self setHidesBottomBarWhenPushed:YES];
The thirdlevelController shows the tabbar and secondLevelController does not show the tabbar when you pop the thirdLevelController.
On your secondViewController, do :
- (BOOL) hidesBottomBarWhenPushed {
return ([self.navigationController.viewControllers lastObject] == self);
}
This way, the tabbar will always be hidden when you are on the secondViewController, and it will appear on the other view controllers
You can hold a bool value to understand if you are coming from a popViewController
and in viewDidAppear you can detect it an hide your tab bar again.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if(backFromThirdView)
[self setHidesBottomBarWhenPushed:YES];
else
[self setHidesBottomBarWhenPushed:YES];
}
I was actually on the same problem. I always tried to hide the tabbar when selecting a row and to disable hiding after returning to the list (a tableview inside a navigationcontroller) so that the user can select the menu again. I set the tabbarcontroller hidden inside the method
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
but when I hided it inside this method, the Tabbar was still hided when returning to my list again. Now I hide the Tabbarcontroller inside the init method of a specific viewcontroller, maybe this works for somebody else too:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
[self setHidesBottomBarWhenPushed:YES];
return self;
}
now when i select a list item and this viewcontroller will be presented the tabbar is hided, after returning to the list it appears again.
You can try this
You declare in the secondLevelController
static BOOL bottomBarShouldHide = YES;
In the viewDidLoad,
if (bottomBarShouldHide) {
[secondLevelController setHidesBottomBarWhenPushed:YES];
bottomBarShouldHide = NO;
}
else {
[secondLevelController setHidesBottomBarWhenPushed:NO];
bottomBarShouldHide = YES;
}
I hope it could help you.

Switching views - iPhone development

I need help figuring out how to change out the view in my application. I have a wonderfully working view that I have finished and now I'd like to be able to switch the view to a brand new, blank white screen to display.
I have these files:
HelloAppDelegate.h,
HelloAppDelegate.m,
HelloViewController.h, and
HelloViewController.m
Then, I added a new View Controller so now I have two more files:
SecondViewController.h and
SecondViewController.m
In my first view (HelloViewController), I have a button. When the user presses this button, I'd like SecondViewController to show up. So, in my HelloViewController.m, I have an action method
-(IBAction)switchToSecondView:(id)sender {
}
In this method, how can I go about initializing my second view and displaying it?
Thanks in advance!
If you want to do something like flipping view, making it modal and then returning back to the main view do following:
Define a delegate to indicate that secondary view finished its work
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish;
#end
In main view do following:
- (void)flipsideViewControllerDidFinish {
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)showInfo {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
In secondary view do following:
- (IBAction)done {
[self.delegate flipsideViewControllerDidFinish];
}

iPhone release Label text

I am trying to release the label text each time the person click on a book title from the table view, it should change the detailViewController label (titleLabel) however it keeps showing the same book title.
Wondering If i have done something wrong - well I know I have but wondering how I fix it...
//
// BookDetailViewController.m
#import "BookDetailViewController.h"
#import "Book.h"
#implementation BookDetailViewController
#synthesize aBook, titleLabel;
// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
[super viewDidLoad];
[self bookDes];
self.title = #"Book Detail";
}
-(void)bookDes {
[self.titleLabel setText:nil];
[self.titleLabel setText:aBook.title];
}
- (void)dealloc {
[aBook release];
[titleLabel release];
[super dealloc];
}
#end
You are calling [self bookDes] from viewDidLoad... This method is called after a view controller has loaded its associated views into memory. How are you creating the BookDetailViewController? If you only create it once and then reuse the controller each time a user presses a book title, the viewDidLoad method will also only be called once.
If you already have the book title in your parent controller, why don't you just set the property from there when you push the child onto the navigation controller?
bookDetailsController.titleLabel.text = selectedBook.title;
EDIT FROM COMMENT:
Yes, the BookDetailsViewController is created once, then saved... so the viewDidLoad is only called once.
One thing you could try is setting the label in the parent's didSelectRowAtIndexPath method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller
if(bdvController == nil)
bdvController = [[BookDetailViewController alloc] initWithNibName:#"BookDetailView" bundle:[NSBundle mainBundle]];
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
bdvController.aBook = aBook;
bdvController.titleLabel.text = aBook.title;
[self.navigationController pushViewController:bdvController animated:YES];
}
there are better ways to do this like overriding the setter on the details controller and setting the label... but you should keep it simple and get it working first.
Hope this helps