How to add UIViewcontroller to a spritebuilder cocos2d 3.x project? - uiviewcontroller

same as the title above. Please provide helpful link if possible. Googled it but did not find anything regarding Spritebuilder and uiviewcontroller integration. As it is needed for me to set up Google Analytics screen tag method inside viewDidLoad() method.
Thanks

This code will allow you to present UIViewController above your CCScene.
MyViewController *vc=[[MyViewController alloc]initWithNibName:#"MyViewController" bundle:nil];
UIViewController *rootViewController=[UIApplication sharedApplication].keyWindow.rootViewController;
if (!rootViewController) {
//some devices have no root view controller so... this code fix the issue
[[CCDirector sharedDirector]presentModalViewController:vc animated:YES];
}else{
[rootViewController presentViewController:vc animated:NO completion:nil];
}

Related

Share screenshot to facebook/twitter using cocos2d-x on IOS

I am porting my game from cocos2d (obj-c) to cocos2d-x 2.x (c++) but i am really struggling with getting the social features to work. Is it possible in any way to use the code below which works flawless in native objective-c and cocos2d.
Take screenshot
- (UIImage*) takeScreenShot
{
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize winSize = [CCDirector sharedDirector].winSize;
CCLayerColor* blankLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 0) width:winSize.width height:winSize.height];
blankLayer.position = ccp(winSize.width/2, winSize.height/2);
CCRenderTexture* rtx = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];
[rtx begin];
[blankLayer visit];
//[[[CCDirector sharedDirector] runningScene] visit];
[layer_main visit];
[rtx end];
return [rtx getUIImage];
}
Post on facebook
- (void)share_score_facebook: (id) sender{
if (SYSTEM_VERSION_LESS_THAN(#"6.0")){
[AppVariables showAlert:#"Outdated iOS" message:#"You must have atleast iOS 6.0 to use the social features.\nPlease update to a newer iOS."];
return;
}
[[CCDirector sharedDirector] pause];
[[CCDirector sharedDirector] stopAnimation];
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
UIImage * screenshot = [self takeScreenShot];
SLComposeViewController *faceBookPost = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[faceBookPost setInitialText:[NSString stringWithFormat:#"Game on! Just scored %d on Game. Can you Beat it?",total_score]];
[faceBookPost addImage:screenshot];
[faceBookPost addURL:[NSURL URLWithString:#"URL_HERE"]];
[[app navController] presentViewController:faceBookPost animated:YES completion:nil];
faceBookPost.completionHandler = ^(SLComposeViewControllerResult result)
{
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];
[[app navController] dismissViewControllerAnimated:YES completion:nil];
};
}
You can compile any Objective-C code with cocos2d-x, but you need modify your code to adapt for cocos2d-x API. You want to take a screenshot and post it to Facebook, right? How about using the following instead of your Objective-C code?
Cocos2d-x v3.2-alpha0 Released! - Added utils::captureScreen to take screeshot
v3.2-alpha0 ccUtils.cpp seems it is able to work on earlier version of cocos2d-x
Screw - aims to bring native Facebook SDK functionalities to cocos2d-x

Whats the best way to setup & populate my nstableview with json data?

I'm sorry if this has already been answered. I'm a bit of a noob. From all the examples I've found I can't seem to work this out.
I'm building a mac osx application using Xcode 5. My interface that I've created in Interface builder has three tableviews. My data to populate these views is json format and it is being received successfully using NSURLConnection.
What is the best way to popular the tableviews? A lot of the tutorials I've followed use an arraycontroller that is dragged and dropped using IB. From what I understand I don't need an array controller and need to hand code controllers for each of tableviews. Is that correct? If so, please explain how I go about this and setup the bindings.
Also, when clicking on a column in tableview 1 I'm planning on making it adjust the data in tableview 2 & 3 so if that impacts anything please let me know.
Thanks in advance!
Update 6 Jan
Here is my code currently..
ContactsTableViewController.h
#import <Foundation/Foundation.h>
#interface ContactsTableViewController : NSObject <NSTableViewDataSource, NSTableViewDelegate> {
IBOutlet NSTableView *contactsTableView;
}
-(id) init;
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView;
-(NSView *) tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;
#end
ContactsTableViewController.m
#import "ContactsTableViewController.h"
#implementation ContactsTableViewController
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
NSLog(#"numberOfRowsInTableView called");
return 1;
}
- (id) init {
NSLog(#"init called");
[contactsTableView setDelegate:self];
[contactsTableView setDataSource:self];
[contactsTableView reloadData];
return self;
}
- (void) initme {
NSLog(#"customfunc");
}
-(NSView *) tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSLog(#"viewForTableColumn called");
NSTextField *result = [tableView makeViewWithIdentifier:#"name" owner:self];// result is now guaranteed to be valid,
result.stringValue = #"My Name"; //[self.nameArray objectAtIndex:row];
return result;
}
#end
In my appDelegate:
- (void) awakeFromNib {
NSLog(#"AWAKE");
ContactsTableViewController *contactsController = [[ContactsTableViewController alloc] init];
}
Currently nothing is showing in the tableview and numberOfRowsInTableView & viewForTableColumn aren't getting called.
Look at the UITableViewDataSource, you need to implement the protocol in one of your classes, probable your view controller, implement the UITableViewDelegate to get row selection events in the same class.

Trouble with new View Controller called from a UITextField Xcode

In the current App that I am writing, I have a UITextField that has a link in it. So I want to call a new View Controller instead of Safari. I can have it open in Safari, but I don't want the user to leave my App. I have done some research and am doing the following.
I made a new file called MyApplication and placed the following inside the .m file to override openURL
#import "MyApplication.h"
#implementation MyApplication
- (BOOL)openURL:(NSURL *)url
{
if ([self handleOpenURL:url])
return YES;
else
return [super openURL:url];
}
- (BOOL)handleOpenURL:(NSURL *)url
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"71"];
[self presentViewController:vc animated:YES completion:nil];
return YES;
}
#end
In the attributes inspector under View Controller I called the Title 71. I also called the storyboard ID and restoration ID 71 just to be sure! In the main.m file I have replaced the third nil with #"MyApplication" as you can see.
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, #"MyApplication", NSStringFromClass([AppDelegate class]));
}
}
However, the App fails to build because in the method handleOpenURL I am getting an error on the line
[self presentViewController:vc animated:YES completion:nil];
It says No visible #interface for 'MyApplication' declares the selector 'presentViewController:animated:completion:' All the samples and questions I have seen are using the deprecated method presentModal but Xcode tells me to use this newer method presentViewController. So does anyone know what I am doing wrong? I must be missing something, but I can't for the life of me find out. Thanks in advance on any insight to this matter!!!
May be you're missing the :
#import <UIKit/UIKit.h>

UIViewController.navigationController becomes nil

I bumped into a problem where UIViewController.navigationController becomes nil and I'm desperately trying to find an answer to this one.
The UINavigationController gets setup in the application delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.rootViewController = [[RootViewController alloc] initWithNibName:#"RootView" bundle:nil];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:self.rootViewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
When the RootViewController is appearing, the self.navigationController member is set and I can use it to hide the navigation bar, like so:
- (void)viewWillAppear:(BOOL)animated {
NSLog( #"self = %#, self.navigationController = %#", self, self.navigationController );
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
The debug output shows values for self and self.navigationController.
When a button is clicked in this controller, self remains the same value indeed but self.navigationController is now nil:
- (IBAction)buttonClicked:(id)sender {
NSLog( #"self = %#, self.navigationController = %#", self, self.navigationController );
// here, self.navigationController is nil, so
// [self.navigationController pushViewController:...] doesn't work :-(
}
I've seen dozens of questions regarding this problem and the answer is always that the UIViewController is not part of a UINavigationController. Since accessing the navigationController in viewWillAppear works fine, I believe something else must be going on. Do you have any pointers? I'll happily provide more detail if necessary.
Try this in the app delegate:
[(UINavigationController *)self.window.rootViewController pushViewController:yourViewController animated:NO];
the rootviewcontroller is actually UINavigationController if you po to debug it. This works for me.
Your code shows that you are only using the navigationController's view but just pray that navigationController life is handled by some magic hand which is not the case.
You need someone to be the explicit owner of the navigationController here.
In fact, the following line:
[self.window addSubview:navigationController.view];
seems to indicate that what you want is for the window's rootViewController to be navigationController:
self.window.rootViewController = navigationController;
But also, it seems that the application's delegate is to be an owner of navigationController as well so navigationController should, in fact, be an ivar of your app's delegate.
In short, fix your object graph (and it will coincidentally do the extra retain you manually did and fix your bug)
I had a problem with a nil view controller and found that it was not connected properly in storyboard to the app delegate.
As always, it helps to formulate the question just to find the solution some minutes later.
I fell prey to ARC, I guess. Once I retained the UINavigationController in the application delegate, it worked fine.

iPhone4 EXC_BAD_ACCESS for UIView after dealloc. How to debug?

I'm working with Apple's Accelerometer Graph Example:
http://developer.apple.com/library/ios/#samplecode/AccelerometerGraph/Introduction/Intro.html
I'm pushing 2 Graph Views onto a navigation controller:
GraphViewController* graphViewController = [[GraphViewController alloc]initWithNibName:#"GraphViewController" bundle:nil];
[self.navigationController pushViewController:graphViewController animated:YES];
[graphViewController release];
The graph's are updated by an external method:
[motionManager startDeviceMotionUpdatesToQueue:motionQueue withHandler:^(CMDeviceMotion *motion, NSError *error) {
...
if(graphDelegate)
{
[self performSelectorInBackground:#selector(notifyGraphDelegateWithMotionEvent:) withObject:motion];
}
}
, which calls
[unfiltered addX:filteredValue y:unfilteredvalue z:10];
for each graph. The frequency of updates is 20 times per second
When I pop the view from the navigation controller, I get EXC_BAD_ACCESS after [super dealloc]
-(void)dealloc
{
// Since 'text' and 'current' are weak references, we do not release them here.
// [super dealloc] will take care to release 'text' as a subview, and releasing 'segments' will release 'current'.
[segments release];
[super dealloc];
}
This is a nasty error, and I really don't know how to troubleshoot something like that. It seems to be something about the order in which the views are de-allocated, as the crash happens after the view is popped. Any ideas on how to troubleshoot something like that?
Set NSZombieEnabled, MallocStackLogging, and guard malloc in the debugger. Then, when your App crashes, type this in the gdb console:
(gdb) info malloc-history 0x543216
Replace 0x543216 with the address of the object that caused the crash, and you will get a much more useful stack trace and it should help you pinpoint the exact line in your code that is causing the problem.
See this article for more detailed instructions.