I have created a tabBarNavigated Application. In second tab, I do something that works fine, but now I want to do something in the first Tab, so first I try to NSLog a string, but I get no reaction.
- (void)viewDidLoad
{
NSLog(#"Test");
}
If I add a label to the view, it will be displayed, but no reaction on my code.
if I start my app, i see this view, but i can't call any actions in this method, even if i change the tab, and go back to the first one, still no logs.
I try to NSlog in GehaltView
this is the mainWindow
viewWillAppear dosn't work :(
The -viewDidLoad method is only called when your view is loaded. This method will not be called again unless the view gets unloaded, in which case -viewDidUnload will be called. A view can be unloaded if there is a memory issue, but otherwise they generally stick around.
If you want to trigger an action that happens every time the view appears, then you can use the -viewWillAppear: method instead. This method is called every time the view re-appears. You can track when the view disappears with -viewWillDisappear, and watch the two get called as you toggle between the two tabs.
Note also that -viewDidLoad may get called before the view appears, but -viewWillAppear will only be called when the view actually appears (or moments before, as the will indicates).
EDIT: The code should read
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(#"View Will Appear");
}
EDIT: This entire answer assumes that you have a subclass of UIViewController. It seems to me that you are by-passing using viewControllers, which in general is a bad idea.
I found the solution, in interface builder i have to add a custom class to the forst tab.
Related
I use storyboard with identifier, when i want to call some view, I use this code:
ViewB *view = [self.storyboard instantiateViewControllerWithIdentifier:#"View2"];
[self.navigationController pushViewController: view animated:YES];
But I want to do something when i pressed the back button, How can I know that happens this ?
That depends on what you are really up to.
First of all you cannot directly catch such an event or so. Unless you replace the back button with your own back button, assign yours to an action of yours and go from there. Within this action you can perform what ever you want to do. And you will have to care for calling popViewControllerAnimated: yourself.
If you don't really need to know that the button was pressed and just need to react when ever the view controller or its view respectively disappears then you can overwrite viewWillDisappear or viewDidDisappear. But be aware that these methods are called too every time when another view is pushed ontop of your very view.
viewWillUnload or viewDidUnload is probably more what you are looking for.
However, dealloc is called when the view controller is destroyed.
One of these will do depending on what you really need.
I am using storyboard and i stumbled upon something i have not been able to grasp.
I am putting up my view controller programmatically with performSegueWithIdentifier:.
It works like a charm, but what i noticed was that every single time i do this, i create a new instance of that viewController, and so, i have memory that keeps piling up. When i simulate a memory warning, i see that for each time i have been calling performSegueWithIdentifier:
i have a new instance of the view controller, and it NEVER gets deallocated. So memory just piles up and i cannot release it, which eventually causes a crash.
I just go to the view controller like this:
// If sales are registered, go to view
[self performSegueWithIdentifier:#"previousSaleSegue" sender:self];
What am i missing here?
You'll be stacking view controllers on top of each other instead of returning to a previous one. Assuming you are using a navigation controller, you'll be doing this:
A --push--> B --finished! Push---> New A ---Push---> B ---Finished! push--->A ...
What you should be doing is:
A --push--> B --finished! Pop -
^-----------------------------/
You're using modal segues by your comments, in this case you need to add an action to your return button to dismiss the view controller (returning to the previous one) instead of presenting another instance. The principle is the same. You'd use
[self dismissViewControllerAnimated:YES completion:nil];
If you're certain that viewcontroller instance does not get deallocated it must be because you keep reference to it somewhere (it could be a cycle on the controller itself).
I am working on an IPhone app that, through viewDidLoad, makes a connection and pulls data into a table. I would like the controller, or at least the table, to reload every time the controller is displayed, even if someone just switched from one tab to another or closed and re-opened the app on this controller. Is there a way to do this? I can't seem to find anything, it's also kind of a hard thing to search for. I have found discussions of when to put code in viewDidLoad but not another method that is run every them the controller displays.
Thanks,
Cheryl
You want to use
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//code to reload table viewdata
}
Each time the view appears this method is called. I use this a lot in my app.
There's no way to do it by injecting code in only one place.
You can think of a workaround, like for example sending custom "reload request" notification from required parts of code:
viewDidAppear: in your view controller
applicationDidBecomeActive: (if table is presented)
tabBarController:didSelectViewController: (if switched onto controller with the table)
tabBar:didSelectItem: (same as above)
etc.
In you view controller simply observe this notification and reload data when required.
Although, whats more important: do you really need to reload data under such harsh requirements? In most cases data reload happens when
view controller's viewDidLoad: is called
it is manually initiated (button, for example)
long time passed since the last update was received
Otherwise it's just an overkill.
I'm using a UINavigationController to navigate between classes. When I press a button on the first view to send me to the second one, everythin works fine. But when I wan't to return from the second, to the first view, the viewDidLoad method isn't being called, and I really need that to happen.
According to this post, I should somehow set my view to nil but I'm not sure where or how to do that. This is the code that I'm using to return to the first view:
NewSongController *nsContr = [[NewSongController alloc] initWithNibName:#"mNSController" bundle:nil];
[self.navigationController popViewControllerAnimated:YES];
[nsContr release];
Your code is not correct.
You don't need to instantiate your first controller in order to pop to it. It already exists.
viewDidLoad will only run when you load the viewController for the first time (i.e. when you push to it). When you push to other controllers they are put onto a stack (imagine a stack of cards). When you push another card onto the stack the cards beneath it are already there.
When you pop it is like removing a card from the stack. But the card underneath is already there so it doesn't need to load again. All it does is run viewWillAppear.
All you need to do to pop back is...
[self.navigationController popViewControllerAnimated:YES];
That's it.
Remove the stuff about NewSongController (if that is what you are trying to go back to).
Then in the NewSongController function - (void)viewWillAppear:animated; put the code you want to run when you get back to it.
Hope that helps.
Your first view is loaded and is pushed onto the navigation stack. Dont try to mess with whats on the stack without fully understanding how setting the view to nil will affect the behavior.
Whatever you do on viewDidLoad, doing it in viewWillAppear or viewDidAppear will give you the result you want.
The viewDidLoad wouldn't appear because it already exists in the navigation stack. Since you are going back in the stack you would want to have the code that you want to trigger in viewWillAppear or viewDidAppear which is executed when popping from one viewcontroller to the one below it.
I have UITableViewController where I need to put initialization code only once to populate data source. in which method to put this code.
I tried with viewWillAppear: method, but it get executed every time view appear.
if you want to display things only once that the View has comed on screen, then yeah. go for it. Otherwise you also have ViewdidLoad or ViewWillAppear if you have to arrange things before the view begins the transition to slide in.
All of these methods will be executed every time from the tableView you tap on a row.
anyway the pattern you are trying to achieve is called singleton, you can find out about it more over here
What should my Objective-C singleton look like?
You can put it in viewDidLoad and it will be only once.
The - (void)viewDidLoad method is probably the place you want use. It gets called once the view controller has finished doing its loading code (either by loading a XIB or by calling loadView).