How can my UIViewController know who put it on the stack?
This is for an addition to a legacy iPhone app with the architecture already defined (years ago, by somebody else.)
The main limitation is that the functionality of the main menu system is based on configuration files, so I can't call any specific initialisation code from the main menu.
This means that the view I am developing is stand-alone, and has to somehow manage its states with the information from the system.
Further, on each screen there is a "Settings" button, taking the user to a Settings pane, that is pushed on the navigation stack on top of "my" view. When the user closes the settings pane, my view reappears, as per normal navigation.
OK, so here is my problem:
When the user enters my view from the menu I want it to be reset so all input fields are empty.
If the user goes to the settings screen开发者_StackOverflow中文版 and returns to my screen, I want all previous input to be preserved, i.e., not reset to empty fields.
If the user then goes back to the main menu, and re-enters my screen, fields should be empty again.
Is there a robust, documented and preferably simple way to know if I should reset the fields in this scenario?
Can you check the navigation stack to see if the settings page is currently on the stack?
- (void)viewWillAppear:(BOOL)aAnimated
{
[super viewWillAppear:aAnimated];
NSArray* stack = [[self navigationController] viewControllers];
UIViewController* last = [stack lastObject];
}
Presumably viewDidDisappear is called when your view is hidden for some reason. You could presumably get the UIWindow and work up the view chain to find where your view is in the chain (if it is at all) and what is hiding it.
Non-trivial, though, and the sort of thing that will likely need to be "maze bright" vs robust.
(Though if a navigation controller is consistently used it becomes much simpler.)
In the view controller in question, how about you set up a delegate.
id delegate;
@property (nonatomic, assign) id delegate;
for the header, then synthesize in the implementation.
Whenever you push to this view controller, set self
as the delegate from the pushing view. Then in this view controller, you can perform a check in the viewDidLoad
or viewDidAppear:
(or wherever you feel it would be necessary) with something like the following:
if ([self.delegate isKindOfClass:[SomeClass class]]) {
// now you can find which class sent to this view;
}
That should do the trick, so I hope it helps you out
EDIT: considering you are switching views without always using a nav controller, the above won't be valid all the time. In that case, you are probably better off using an internal property as well as an outlet to your settings pane. So in this view controller, you'll want something like this in the header:
BOOL shouldReset;
@property (readwrite) BOOL shouldReset;
In your viewDidLoad
, you'll want to initialize this as shouldReset = YES
. You should also put this in your viewDidDisappear:
since it is your default behavior. When you present the settings pane, give the settings an outlet to the current view controller so you can, from within the settings (when you press the back button) set [self.otherViewController setShouldReset:NO]
. Then in your viewDidAppear:
for the original view controller in question, you can check to see if it should reset its fields or not
精彩评论