What Method to Override when View goes Active? - objective-c

I have a Tab Bar Controller that contains five views. The views have elements that update a variable in the root app delegate. I'd like the views to change whenever this variable changes value.
In the absence of an event notification mechanism to update all views, I'd like them to update when they are displayed (ie., when their tab bar button is pressed.)
What method should I override? I thought it was viewDidLoad but I realize this is being called only once, and when I go to another view, make a change, and go back to it, nothing happens.

Try one of these
- (void)viewWillAppear:(BOOL)animated
- (void)viewDidAppear:(BOOL)animated
They will handle events just before and after a view of corresponding view controller will become visible.

Related

View Controllers, Popup buttons, AppDelegates, Status Menus, and headaches

This is kinda long but I want to give as many details as possible.
I have created a status bar app. One of the things I've done is made it to where there is a "Settings" menu item that if you click on, an NSWindow pops up with 5 different buttons -- these buttons are tied to their own custom NSViewController so that if you click them, the View Controller changes, but the window remains the same, just like any other StatusBar app.
On one of the view controllers is a popup button with several user selectable options. If I add a button to the specific VC (view controller) and link that to an ibaction that pulls the selected index of the popup button, it works just fine (i.e. I select item 2, it logs item 2).
Now here's where things get. . . odd. I've created a submenu that would in theory allow the user to quickly change the selected item of the popup linked to my custom view controller. Think of this submenu as being labeled 1-4 and clicking one of those items would change the index of the popup button, however every time I try this and follow it up with a NSLog to ensure the button has changed, it ALWAYS reads zero, in other words, it NEVER changes.
Here's what I tried.
Just like with every other view controller I've ever used, I create a new object of my view controller (alloc, init), and reference the popup button object that i've created as a property. This compiles just fine, but returns only zero no matter what is selected.
I've tried calling the method that I know works (the button that is on the view controller containing the popup), but again, this returns only zero no matter what is selected.
Any advice would be greatly appreciated.
tl;dr: can't change a popupbutton on a view controller from the app delegate or nsstatusmenu item, it just always returns zero.
EDIT: I have found a work around since I can communicate to my app delegate just fine, I created an int property and set it to zero. From there, whenever my popupbutton on my VC changes, I change the value of the app delegate int property and just ensure that whenever the nib launches, it set's the selected item to the app delegate property. I feel there has to be a more straight forward way to just get the value of the popup and change it from the app delegate.
EDIT 2: Some code as requested. I realized I needed to point the the view controller by using initwithnibname from the app delegate and then load the view. Doing so allows me to pull the first object in the popup button, any subsequent changes are not recognized unless I do so via my test button on the view controller itself. Also, I'm adding values to my popup in my awakefromnib in the view controller.
searches_VC = [[Searches_ViewController alloc] initWithNibName:#"Searches_ViewController" bundle:nil];
[searches_VC loadView];
NSLog(#"Selected Title: %#",[[searches_VC profilePopupOutlet] titleOfSelectedItem]);

Which event is called after the view is shown

I have created a small app with a storyboard.
Now, I have created a first view, which should be my splash. I am now searching for the right place to put my work into it. After this work, I would programmatically triggering a Storyboard Segue to switch to the next view. For this, I am searching the right place/event. It should be an event which is called right after the view will be shown.
I have tried viewDidLoad but it seems, that this event is triggered before the view is shown on the screen.
The (void)viewDidLoad method is called the 1st time the view is loaded.
So it's the good place where to write your init/setup lines.
The (void)viewDidAppear: method is called everytime your view is displayed.
Hope this helps.
Actually, and for a little nitpicking here. viewDidAppear is not called every time a VC is shown, but each time it is inserted in the VC hierarchy. So it seems to be what you want to use for now. But if you have later some use case where you want some action to take place each time the view is displayed to the user you need other means. For example this method will not be called when you push back some navigation VC (your VC is already in the hierarchy).

Determine if viewcontroller is already on stack and if so, go to or dismiss it

I have an app I am working on that has a main screen with two buttons. One will take you to a view of a GPS (map) and then once there (new VC) it has options for setting that position or bringing up a list (tableview, another VC) of all locations already tagged.
At the list VC, if you click on the table cell, it will bring up the VC with the map. Problem is, this then adds the same VC bak on the stack. If a user clicks the Cancel button, they go back ones screen, then cancel goes back another screen, etc... until back to the main.
I know I can do the [self.navigationController popToRootViewControllerAnimated:YES]; to pop back to root but that is not always what I want.
Also, I know I can do: [[[self presentingViewController] presentingViewController] dismissModalViewControllerAnimated:YES];
I guess what I am saying is I want to "reuse" the GPS map view so I can call it from other VC's, so that is why I didn't go with the "pass back" to calling VC. So, is there away to either when a button is pressed and is to present a new VC, can I dismiss the prior one after the new one is shown? This way, a dismiss of current VC would take me back to where I need to be.
I hope makes sense and also that this question doesn't fall into the "Not an actual question" category.
Any help or better suggestions is greatly appreciated. Thx
Geo...
If you want to jump back some number of levels in a navigation controller's VC stack, you'll probably want to use its popToViewController:animated: method. To figure out if a particular view controller is on that stack, look at the navigation controller's viewControllers property. Be careful, though, as this kind of jumping around is a rather nonstandard UI behavior (even though there's API for it) which might confuse your users.
Also, using navigation controllers and presenting modally aren't the only ways to manage multiple view controllers -- you can always set the window's rootViewController yourself (and animate the change with UIView animations), even wrapping up your custom transition type in a custom UIStoryboardSegue if you like.
You can put a delegate in the table view. So that when a cell is pressed the info is passed to the delegate method in the VC which will dismiss the table view and reloads itself with the new info. You will have to implement refresh method in that VC.

Increasing number of living Views

I've set up a really simple project using storyboards including two views as shown here: http://i.stack.imgur.com/iRx21.png. The navigation can be done by either selecting a cell in the custom table view or hitting the back button labelled with "<<". Everything works fine except the following:
when I switch between the views, every time an instantiation happens. The profiling shows an increasing number of view objects. I would like to keep only one of each view and instantiation should be happen only once. What am I doing wrong? (I'm using ARC.)
Thanks in advance!
You should not link your back button to the parent view controller. This is what causes the new instantiation.
The way to go is to embed the table view into UINavigationController (in IB, choose Editor -> Imbed In -> Navigation Controller. Then change your segue to a Push segue. You can of course hide the navigation bar etc. to make things look exactly as you like. Then, link the back button to the controller with an IBAction and in the handler do a simple
[self.navigationController popViewControllerAnimated:YES];
This would be the appropriate logic of what you are doing. Of course, you can also push the web view modally and then handle the button click with
[self dismissModalViewControllerAnimated:YES];

Adding and Removing UIBarButtonItems to UINavigationBar from Multiple Views

I currently have a Main Window set up as a UINavigationController (also is the root view controller), and I have two views. The first view is the login screen, and the second screen is a table view screen. What I would like to have happen is to show different UIBarButtonItems based on what screen is showing up. For instance, when the logon screen is being displayed, I want a left button on the navigation bar to be displayed (more specifically, it'd be a Settings button before logging in). Once the user logs in, I want the left button to say "Logout" and the right to be a reload button. I've tried programmatically adding the buttons, but they won't show up. Any suggestions?
Also, I've gotten most of this done in IB, but I feel like it would probably be easier to add these buttons programmatically.
All subclasses of UIViewController have a property called navigationItem. While it is readonly, you can alter its properties. So in LoginViewController, you would do self.navigationItem.leftBarButtonItem = /* Code to create the bar button */ and likewise within the TableViewController. Let me know if you need more detail.