I have numerous ViewControllers within a NavigationController setup and was wondering if it's possible to run code when the user clicks "back".
Basically my app runs a test and I would like "back" to also stop the test. I know I can add a separate stop button but this would be smoother.
Thanks
EDITED:
I have added:
-(void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([[StdTCPTestViewController class] isSubclassOfClass:[ViewController class]])
NSLog(#"We are going back to VC1");
}
to my code but this gets called as soon as the view loads, not when the back button is pressed. The StdTCPTestViewController is VC2 and ViewController is VC1 in the example below. NavController -- VC1 -- VC2 is the hierarchy and the segue is push from VC1 to VC2.
Any other tweaks?
Let's assume you have 2 VCs in the Navigation Controller stack:
VC2 on top of the stack (that is the front VC that is showing)
VC1 behind/below VC2 on the stack (that is the back VC you will go back to when you press back on VC2)
Every time VC2 is "pushed" onto the navigation controller stack: viewDidLoad of VC2 is called. And everytime it is "popped", the view is unloaded. On the other hand, VC1 on the stack does not call its viewDidload (when VC2 is popped) since VC1 is still loaded in memory (remember you were pushing VC2 on top of VC1 so VC1 must have stayed in memory!)
So in a way, if you want some code executed when the back button is pressed, you should put that code in the viewDidAppear (or viewWillAppear) method of VC1.
Now, if the code you wanted to write has to be executed on VC2 before you pop it, then you can use in VC2:
-(void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([[viewController class] isSubclassOfClass:[VC1 class]])
NSLog(#"We are going back to VC1");
}
This is a UINavigationController delegate method so VC2 would have to adopt that protocol by adding UINavigationControllerDelegate at the end of its #interface line in the .h file:
#interface VC2 : UIViewController <UINavigationControllerDelegate>
And you would have to set VC2 as delegate for the UINavigationController so you have to put in viewDidLoad of VC2:
self.navigationController.delegate=self;
You will obviously also need to import VC1.h file into VC2 since you are referring to it in the delegate method code above.
Hope this helps
Related
I apologize in advance because I am very new to swift. I have an array of strings (answerSet) that pop up randomly and without repeating when a view controller is tapped. I did this through a while loop and added an if statement so once the last string in the array pops up the user can tap the screen and it triggers a segue to go to the next view controller (toEnd). This doesn't work properly as a couple strings occur when the screen is tapped but then the segue is triggered before all the strings in the array have been shown. I have provided the code. Please let me know how to fix this and thanks so much! PM.pngmy code
You need to remove the element in answerSet rather than setting it to an empty string.
answerSet.remove(at: randomIndex)
Without this, it can trigger the segue if on the next loop the randomIndex gets an empty string.
I have an array of MovieClips (on the main stages actions) that i want to refer to from within other movieclips placed on the stage.
var hotSpots:Array =[hotSpot1, hotSpot2, control_mc, tip_mc]
for each (var removeHotspots:MovieClip in hotSpots)
{
removeHotspots.visible = false;
}
How do i refer back to this array from within another movieclip without having to add the array again?
I tried...
var hotSpots:Array = Object(this).hotSpots
and then within my event Listener...
for each (var removeHotspots:MovieClip in hotSpots)
{
removeHotspots.visible = true;
}
But it doesn't seem to work? Can anyone help.
I have alot of arrays like this that i really dont want to have to add to each movieclip everytime i need to call them.
"this" refers to the MovieClip your code is within. If you want to access the parent MovieClip (in this case the root or stage) you can either use parent.hotSpots or stage.hotSpots. However, this requires the MovieClip to be added to the stage (and thereby the display list), otherwise both parent and stage will be null.
If you want to wait for a MovieClip to be on stage before executing code where you refer to the stage variable you can use the event Event.ADDED_TO_STAGE
i have got a probling concerning my animation.Sqeuence.
Only the first Sequence Element is being executed. The 2nd is ignored.
I want the Sprite spell_1 to move to the Hero (hero_x_exact,hero_y_exact), and after that to the destination.
Each element works fine for itself but not in a Sequence.
goog.require('lime.animation.Sequence');
...
var spellmovement = new lime.animation.Sequence(
spell_1.runAction(new lime.animation.MoveTo(hero_x_exact,hero_y_exact).setDuration(1).enableOptimizations()),
spell_1.runAction(new lime.animation.MoveTo(target_coord_x_spell,target_coord_y_spell).setDuration(1).enableOptimizations())
);
What might work better is using a spritesheet with each animation on the spritesheet Then you can build something that loads each animation by name. Obj_Walk001.png, Obj_Walk002.png
Then create a method to read each animation per frame in the scheduling manager.
You can find an example of this code in limejs/lime/demos/test. Then goto run.htm and click on Frame4 this one will show you how limejs is going it.
Using iOS6's awesome new UICollectionView how am I able to delete all of the UICollectionViewCell objects in a big loop?
Say I've loaded all my data into it already, I hit refresh, I want to delete everything currently in there, then just call my stuff again.
I've found deleteItemsAtIndexPaths which takes an NSArray, so how can I get all items into it?
The proper way of clearing out a UICollectionVew is to simply clear the data source and then reload the collection view.
So if your data source was an array:
self.dataArray = nil;
[self.collectionView reloadData];
Boom, you're cleared out.
Turns out I can use deleteSections and pass a NSIndexSet through to it, making a range of 0,0 and it'll delete the one and only section.
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 0)];
[self.collectionView deleteSections:indexSet];
I could probably just use indexSetWithIndex but when I did my app crashed.
I have a main view where I initialize severals arrays and released them in viewDidUnload. So when I change to other view where I have the instruccions and I come back to the main view all the array are nil again.
What I what to do is: when the app is lunched, the arrays are initialized, y use them, I could go to others views and when come back to the main view that the arrays keep the values, and only when the app is close then release all the arrays.
How do I have to do it?
Just release them in -dealloc and initialize them in viewDidLoad. That way theyre only released or nullified when the view shuts down and when it is reloaded, they are reinitialized.
Hope this helps.
You can use different approaches for your problem. I can tell you some ways.
You can write methods for view initialization which will take NSArray as parameter. Like:
- (id)initWithArray:(NSArray *)myArray {
[super init];
//here you can store an NSArray inside this ViewController in #property. For ex:
currentArray = myArray;
return self;
}
You can use global storage like CoreData and use NSManagedObject class to define instances for necessary object in every view.
You can use delegate approach. For example you can delegate methods from second view in first view.