Im building a small IOS app using PhoneGap, and after setting up a base project and I noticed that under AppDelegate.h the following PhoneGap Object is initialised:
#property (nonatomic, strong) IBOutlet CDVViewController* viewController;
However I also noticed the MainViewController inherits the CDViewController, and MainViewController is displayed after running though AppDelegate so I dont quite understand why it does just do the following:
#property (nonatomic, strong) IBOutlet MainViewController* viewController;
I amended the code like above, and it works perfectly. Is there any reason why it uses the CDViewController instead of MainViewController :S?
Thanks
MainViewController is a subclass of CDVViewController, therefore more generic and less confusing.
Related
I'm making an app that will use PubNub for a group chat part of the app. I turned on Playback on my app and I completed the tutorial for setting up the code. I'm confused though, because all the code was in the AppDelegate, and I have my chat view controller as part of my storyboard. My question is, what setup code do I have to do in my view controller so I can get all the past 100 messages using the historyForChannel:start:end:limit:withCompletion: method. Would I have to make a new instance of the PubNub client? That doesn't make sense since the user will be switching view controllers and It should be stored in a long life property.
What setup code do I have to do in my view controllers to get the past messages? (To be loaded into a very elaborate tableview setup)
So I figured out a working solution. First, you have to make the PubNub client property public by defining it in the AppDelegate.h file, not the .m implementation.
// AppDelegate.h
#import <UIKit/UIKit.h>
#import <PubNub/PubNub.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate, PNObjectEventListener>
#property (strong, nonatomic) UIWindow *window;
// Stores reference on PubNub client to make sure what it won't be released.
#property (nonatomic) PubNub *pnClient;
#end
And don't forget to delete from the AppDelegate.m
#import "AppDelegate.h"
#interface AppDelegate ()
/*
// Stores reference on PubNub client to make sure what it won't be released.
#property (nonatomic) PubNub *pnClient;
*/ // Delete from here
#end
#implementation AppDelegate
If you want to do notifications and such, keep the AppDelegate as a listener to the [self.pnClient] property. If not, just delete <PNObjectEventListener> from the AppDelegate.h and [self.pnClient addListener:self]; from your AppDelegate.m. If you prefer to keep it, just don't delete that stuff.
Now, #import your AppDelegate in your ChatViewController.h or the .m of you prefer. Then, make your .h conform to the <PNObjectEventListener> delegate. The, before you forget, add another client in your .h or .m to store the property of your PubNub client in your AppDelegate. :
// Stores reference on PubNub client to make sure what it won't be released.
#property (nonatomic) PubNub *pnClient;
Next, in your viewDidLoad method, add:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.pnClient = appDelegate.pnClient;
[self.pnClient addListener:self];
This code first grabs the AppDelegate of your app, (so there are no shared instance or singleton things involved). Then, It sets the pnClient of your app delegate to your "temporary" client in your view controller. (See why we moved the AppDelegate's client to the .h?) And lastly, it adds self as a listener, so you can do stuff in your view controller.
Thats all there is to it!
I would suggest using your chat controller to populate a UITableView or something else, and the AppDelegate for handling notifications.
I have a location class in my project which calls methods in different several view controllers. I have defined the class in each view controller as self which works fine but when I push to and from other view controllers it starts behaving irrationally and calls the method in the wrong controller causing all sorts of problems
My location class is setup correctly and I believe I have called them correctly but I am obviously missing something like deallocating the delegate once I am done with it?
This is what is in each of my view controllers
.h
#import "AHLocationClass.h"
AHLocationClass *location;
#interface AHSelectionController : UIViewController <AHLocationDelegate> {
.m
location = [[AHLocationClass alloc] init];
location.delegate = self;
Note, I know using notification will fix this but in my project using this method will quickly make things messy
It turns out that declaring globally was my issue, declaring it as a property fixed this inconsistency.
#property (nonatomic, strong) AHLocationClass *location;
TLDR
Properties are not showing on a UIViewController that I was able to access when working on this project yesterday. This is not as simple as how do I set a property on a VC? I can not see the properties of the VC.
The Situation
A custom UIViewController (A) pushes to a second UIViewController (B), pushes to a third UIViewController (C)
//programmatically calling a push to the next view in A to B to C, example code from B to C.
EnterFinalHRViewController * finalHeartRate = [self.storyboard instantiateViewControllerWithIdentifier:#"EnterFinalHeartRateController"];
finalHeartRate.lanes = _lanes;
[self.navigationController pushViewController:finalHeartRate animated:YES];
The NSMutableArray, lanes, gets passed along twice. Not a problem.
When I arrive in VC(C) the debugger reads:
A self = (EnterFinalHRViewController *) #"0 objects" 0x15da40c0
V [0]
I have a header file in VC(C) that reads
#import <UIKit/UIKit.h>
#import "SwimTimingViewController.h"
#import "Lane.h"
#interface EnterFinalHRViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource, UIPickerViewDataSource, UIPickerViewDelegate>
#property (nonatomic, strong) NSMutableArray *lanes;
#end
For Clarity
Lanes is just an example of one property that is passed along.
This is not just trying to set and access a property of a VC across a segue
I've looked everywhere I can, any more links would be appreciated as well.
The Question
Why can't I see any properties of self in the final UIViewController in the debugger, how would one fix it, and or any useful debugging tips.
My Theory
Since upgrading to a new version of xcode, last night, something broke / xcode bug, and I don't know what that is. I am now using Xcode Version 6.1 (6A1052d). I am testing using my iPhone 5c running ios7. (I will be upgrading as soon as everything works 100% in ios7).
Quick shout out to thank all the contributors here at SO and beyond. The help is alway very warmly welcomed.
So I'm on my first iPhone app. I'm actually rather far into it. I have already learned from many mistakes, but I feel I've made an ultimate mistake. I'm using segues to navigate to different views. I get into about 5 segue views deep, which I'm realizing is causing a LOT of allocated memory. In other words, View A calls View B, B segues into C, C into D, etc..From what I understand, by the time I get to D I now have instances of A B C and D open, which does not sound good. I am using delegates for example like below:
Just an example of what I'm doing throughout my app:
First View:
#interface FirstViewController : UIViewController<SecondViewControllerDelegate>
#end
Second View:
#class SecondViewController;
#protocol SecondReviewOrderViewControllerDelegate <NSObject>
- (void)secondViewControllerDidCancel:(SecondViewController *)controller;
#end
#interface SecondViewController : UIViewController<ThirdViewControllerDelegate>
#property (strong, nonatomic) id <SecondViewControllerDelegate> delegate;
#end
Third View:
#class ThirdViewController;
#protocol ThirdReviewOrderViewControllerDelegate <NSObject>
- (void)thirdViewControllerDidCancel:(ThirdViewController *)controller;
#end
#interface ThirdViewController : UIViewController<>
#property (strong, nonatomic) id <ThirdViewControllerDelegate> delegate;
#end
And so on and on onto view 4 and 5.
My question is, if this is wrong, which it seems to be, what is the right way to about navigating views and passing data from one viewcontroller to another? Thanks for any tips.
From what I understand, by the time I get to D I now have instances of A B C and D open, which does not sound good
A view controller, of itself, is a fairly lightweight object, and there is no problem whatever with going many levels deep (e.g. pushing five view controllers onto a navigation controller stack). However, memory and images you may be holding on to are not lightweight, so be sure to implement didReceiveMemoryWarning and take it seriously if it arrives.
A strategy for letting go of large retained memory-hogging stuff in response to didReceiveMemoryWarning is to save it off to disk (if it can't be recreated on demand) and then use lazy initialization to read it back in the next time you are asked for it.
I am developing a game, and I would like to transition between several views, e.g. Menu Screen, Game Screen, Game Over Screen etc. What would be the easiest way of doing this? I'm not sure if I should use a view stack as the order that the views are shown is not always reversed.
I assume by "view stack" you mean a UINavigationController?
The easiest way is to keep references to all of the view controllers somewhere, for example I see people use the application delegate a lot, so your application delegate's class extension would look a little like:
#interface AppDelegate ()
#property (nonatomic, strong) UIViewController *rootViewController; //this is what gets set as the window's root VC
#property (nonatomic, strong) UIViewController *mainScreenViewController;
#property (nonatomic, strong) UIViewController *gameScreenViewController;
#property (nonatomic, strong) UIViewController *gameOverScreenViewController;
#end
Assume rootViewController just controls a container view for the rest of the app (You would probably actually be well served putting all this logic into the root view controller though...)
Now anytime you need to show a certain screen, call a method like:
- (void)switchToViewController:(UIViewController *)viewController
{
[self.rootViewController.view.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
[self.rootViewController.view addSubview:viewController.view];
}
You can now write methods that are named more memorably like -switchToGameOverScreen
- (void)switchToGameOverScreen
{
[self switchToViewController:self.gameOverScreenViewController];
}
This basic pattern of view navigation is roughly found in UITabBarController and often in views controlled by UISegmentedControls.
Hopefully this helps!
If you are developing a game in Cocos-2d,the transitions between scene can be done like this:
[[CCDirector sharedDirector] replaceScene:[CCTransitionCrossFade transitionWithDuration:0.5f scene:[GameOver scene]]];
Here GameOver is a new scene ,to which it goes after transition effect.
There are many other transitions in cocos-2d for going from one scene to another i.e
0. CCTransitionCrossFade
1. CCTransitionFade
2. CCTransitionFadeBL
3. CCTransitionFadeDown
4. CCTransitionFadeTR
5. CCTransitionFadeUp
6. CCTransitionFlipAngular
7. CCTransitionFlipX
8. CCTransitionFlipY
9. CCTransitionJumpZoom
10. CCTransitionMoveInB
11. CCTransitionMoveInL
12. CCTransitionMoveInT
13. CCTransitionPageTurn
14. CCTransitionRadialCCW
15. CCTransitionRotoZoom
16. CCTransitionShrinkGrow
17. CCTransitionSlideInB
18. CCTransitionSlideInL
19. CCTransitionSlideInR
20. CCTransitionSlideInT
21. CCTransitionSplitCols
22. CCTransitionSplitRows
23. CCTransitionTurnOffTiles
24. CCTransitionZoomFlipAngular
25. CCTransitionZoomFlipX
26. CCTransitionZoomFlipY
Thanks