Bugged Game Center Matchmaker appearance - objective-c

I am developing multiplayer for my game and I have encountered following issue
I am using Cocos 2d 2.1 , iOS 6 and following code to show matchmaker (Landscape orientation)
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[GCHelper sharedInstance] findMatchWithMinPlayers:2 maxPlayers:4 viewController:[app navController] delegate:self];
And thats how it appears
Following code is used for that function
- (void)findMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers
viewController:(UIViewController *)viewController
delegate:(id<GCHelperDelegate>)theDelegate {
if (!gameCenterAvailable) return;
matchStarted = NO;
self.match = nil;
self.presentingViewController = viewController;
delegate = theDelegate;
[presentingViewController dismissModalViewControllerAnimated:NO];
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = minPlayers;
request.maxPlayers = maxPlayers;
GKMatchmakerViewController *mmvc =
[[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
mmvc.matchmakerDelegate = self;
[presentingViewController presentViewController:mmvc animated:YES completion:nil];
}

is this code in HelloWorldLayer? if so is that the first layer that the director calls? I have the same problem but it's fixes if I follow strictly the cocos2d template that was created for me. Meaning that I use the IntroLayer as the initial layer and from there it transitions into the HelloWorldLayer where I have the game center code. I think it has something to do with the director being loaded too late but I'm not sure

This happens when you try to push on a UIViewController, but you only have a UIView in your app, and no UIViewController. Let me guess, you do not have a rootViewController.
If you are working from the EAGLView template, you must declare a UIViewController for the UIView. This can be as simple as,
- (void) applicationDidFinishLaunching:(UIApplication *)application
{
window.rootViewController = [[UIViewController alloc] init];
window.rootViewController.view = glView; // MUST SET THIS UP,
// NOW THE EAGLView HAS A UIViewController.
[glView startAnimation];
}
Then, anytime you want to push on one of those GameKit UIViewControllers, use the presentViewController method of your Window object's rootViewController
For example:
[self.window.rootViewController presentViewController:vc animated:TRUE completion:^(void){puts("DONE");} ] ;
The bugginess will go away and the window will be correctly formed.

Related

Instance variables not working in ViewController in UINavigationController

I'm still new to iOS development but I've ran into a problem that I can't solve and I've tried looking online but can't find anything yet.
I'm using a UIImagePickerController to pick and image and I'm using it in the App Delegate. When an image is returned, in the imagePickerController:didFinishPickingMediaWithInfo method, I want to make a new navigation controller with a view controller and put it over the "app".
Here is how I'm doing it:
CustomNavigationController *customNavigationController = [[CustomNavigationController alloc] init];
PhotoViewController *photoViewController = [[PhotoViewController alloc] initWithNibName:#"PhotoViewController" bundle:[NSBundle mainBundle]];
[customNavigationController pushViewController:photoViewController animated:NO];
[customNavigationController.view setFrame:[[UIScreen mainScreen] applicationFrame]];
[photoViewController release];
[self.window addSubview:customNavigationController.view];
//Call method of photoViewController.
[[customNavigationController.viewControllers objectAtIndex:0] addPhotoFromData:info];
[self.tabBarController dismissViewControllerAnimated:YES completion:nil]; //Get rid of UIImagePickerController
However in the photoViewController, I don't have access to any instance variables synthesized and loaded in viewDidLoad. They all return to null. There is also a tableView and calls to reload the tableView do not actually cause the tableView to respond.
Here is some of the code from photoViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.photoCount = 0;
self.timestamp = [[NSDate date] timeIntervalSince1970];
self.photos = [[NSMutableArray alloc] initWithCapacity:5];
self.photosData = [[NSMutableArray alloc] initWithCapacity:5];
self.uploadingCount = 0;
UINib *customCell = [UINib nibWithNibName:#"CustomTableCell" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:customCell forCellReuseIdentifier:#"customCell"];
self.tableView.separatorColor = [UIColor clearColor];
NSLog(#"%#", self);
}
and also the addPhotoFromData method:
- (void)addPhotoFromData:(NSDictionary *)info
{
[self.photos addObject:info];
NSLog(#"%#", self.photos); //Doesn't add "info" and does not return anything when later called from other methods.
[self.tableView reloadData]; //Doesn't work
Everything was working before I add in the UINavigationController. I'm completely lost.
EDIT: After some more debugging attempts, I have discovered that the view is not loaded when addPhotoFromData is called. viewDidLoad is called afterwords. Is there a way to delay method calls?
Any reason you can't have the PhotoViewController call addPhotoFromData: inside the viewDidLoad method? You can always access properties of the app delegate from the view controller:
YourAppDelegateType * delegate = [UIApplication sharedApplication].delegate;
[self addPhotoFromData:delegate.info];

ios sdk attach and play video file in view controller before attaching tab bar to main window

I have an non ARC project. So i am maintaining its memory management. It has an tabbar and navigation controller.In that on launch before showing tab bar i have to show an launch video of 5 sec kind of thing. so i have two question
Best and easy way to show an view controller before attaching tab bar controller to main window without leaks.Following is my current technique and code but code analyzer is showing me potential leaks in my video controller.
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UIViewController *viewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil] ;
myNavigationController = [ [ UINavigationController alloc ] initWithRootViewController: viewController ];
[viewController release];
NSMutableArray *viewControllers;
viewControllers = [[NSMutableArray alloc] init];
[viewControllers addObject: myNavigationController]; //Tab 1
myNavigationController release];
// ADD Tab 2 //ADD Tab 3 //ADD Tab 4
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = viewControllers;
[viewControllers release];
//Add video contoller before showing tabs
self.videoController = [[VideoPlayViewController alloc] initWithNibName:#"VideoPlayViewController" bundle:nil];
[self.window addSubview:videoController.view];
[self.window makeKeyAndVisible];
Here is my Video Player controller code
- (void)viewDidLoad
{
[super viewDidLoad];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:#"some url"];
//------ init code in between and added observer to movie playback finish callback ------
[self.view addSubview:moviePlayer.view ]; //show potential leak here if i not release moviePlayer
//[moviePlayer release]; //if i release here controller show me black window with no video playing
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
[player stop];
[player.view removeFromSuperview];
[player release]; //show incorrect decrement of reference count of an object that is not owned at this point by caller
//Fire notification to add tab bar as root view controller
}
And after video is played i get notificaton in my appdelegate and then
[videoController.view removeFromSuperview];
[self.videoController release];
self.videoController = nil;
self.window.rootViewController = self.tabBarController;
and my main app delegate dealloc as usual
- (void)dealloc {
[_window release];
[_tabBarController release];
[super dealloc];
}
i think i explained my problem correctly.Please anyone have any better way to do this.
Thanks
Declare the moviePlayer variable outside of viewDidLoad and then release it in moviePlayBackDidFinish. You're adding a reference to it and then removing only that reference. The reason you're being notified about a leak is that moviePlayer is never released - and with the current setup of your code you can't release it.
MPMoviePlayerController *moviePlayer; //keep reference to moviePlayer
- (void)viewDidLoad
{
[super viewDidLoad];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:#"some url"];
[self.view addSubview:moviePlayer.view ];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
[moviePlayer stop];
[moviePlayer.view removeFromSuperview];
[moviePlayer release];
}

Having trouble getting UIImagePickerController Working

I have two classes: View1 and SelectImage. View1 imports SelectImage. SelectImage is used to present a UIView to the user which allows them to select how to grab a photo from their device. Tapping a button in that list then sends a message to one of the methods below...
In this code:
-(void)importFromCamera:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePicker setDelegate:self];
[self presentModalViewController:imagePicker animated:YES];
}
-(void)importFromPhotoLibrary:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[imagePicker setDelegate:self];
[self presentModalViewController:imagePicker animated:YES];
}
-(void)importFromSavedPhotosAlbum:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
UIViewController *viewController = [UIViewController alloc];
[imagePicker setDelegate:self];
[self presentModalViewController:imagePicker animated:YES];
}
the compiler complains:
Receiver type 'SelectImage' for instance message does not declare a
method with selector 'presentModalViewController:animated:'
At each instance of [self presentModalViewController:imagePicker animated:YES];
Not sure what I'm missing. I've looked around and can only find examples that keep everything in one class.
Is it because the class is a UIView and not a UIViewController? I'm pretty sure I have the necessary delegate protocols in place: .
I'm trying to keep this functionality contained in a class to allow other views to use it as well (View2, View3, etc).
Thanks!
Yes. presentModalViewController is a method on UIViewController not UIView
Note that on iOS 5 the preferred method to use is:
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
SelectImage class could have a delegate which your UIViewController implements. The action methods of your buttons on SelectImage class would call your delegate (ie. the UIViewController).
SelectImage should be a UIViewController. Call it SelectImageController, then view1 can add it's view like [view1 addSubview:self.selectImageController.view];

Tab bar, reload every time tab is pressed

I am creating an app in which I have five tabs. I need to reload each controller every time when tab is pressed.
Put the code you want to reload, in the view will appear or in view did appear of all the view.
All the best.
Example:
// AppDelegate ( and <UITabBarControllerDelegate> )
// Creation tabbar and controllers
UIViewController* myController1 = [[UIViewController alloc] init] autorelease];
UINavigationController* nav1 = [[[UINavigationController alloc] initWithRootViewController:myController1] autorelease];
UIViewController* myController2 = [[UIViewController alloc] init] autorelease];
UINavigationController* nav2 = [[[UINavigationController alloc] initWithRootViewController:myController2] autorelease];
NSArray *array = [NSArray arrayWithObjects: myController1, myController2, nil];
UITabBarController* tab = [[[UITabBarController alloc] init] autorelease];
tab.viewControllers = array;
tab.delegate = self; // <-- don't forget set delegate for TabBarController
// TabBarController Delegate methods
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
{
// Reload selected VC's view
[viewController.view setNeedsDisplay];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//other codes
[self.tabBarController setDelegate:self]
//other codes
}
// UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([viewController respondsToSelector:#selector(reloadDataTemp)]) {
[(YourViewController *)viewController reloadData];
}
}
So write a method to redraw the elements on your page and call it on tab press. I will edit this post if you provide more information on the problem you are facing.
if you are you are using the uitableview use this
[tableview reloaddata];
I hope you are talking about the webview the webview should reload every time a tabbar item is navigated well just implement [webview reload] in the tab bar delegate.
Swift 5
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let vc = self.viewControllers?[1] as? stepVC // ViewController That need to be loaded
vc?.viewDidLoad()
}

Loading an OverlayView from XIB -vs- programmatically for use with UIImagePickerController

I am currently making a camera app for iPhone and I have a strange phenomenon that I can't figure out. I would appreciate some help understanding.
When recreating an overlay view for passing to UIImagePickerController, I have been successfully been able to create the view programmatically. What I haven't been able to do is create the view with/without controller in IB, load it and pass it to the overlay pointer successfully. If I do it via IB, the view is not opaque and obscures the view of the camera completely. I can not figure out why.
I was thinking that the normal view pointer might be assigned when loading from XIB and therefore overwrite the camera's view, but I have an example programmatically where view and overlayView are set equal in the controller class. Perhaps the load order is overwriting a pointer?
Help would be appreciated... kind regards.
This works fine...
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// dislodge the MainView from the MainViewController
//
MainViewController *aController = [[MainViewController alloc] init]; //WithNibName:#"MainView" bundle:nil];
aController.sourceType= UIImagePickerControllerSourceTypeCamera;
aController.delegate= aController;
aController.showsCameraControls= NO;
// Programmatically load the MainView from the Nib and assign it as the OverlayView instead
//
NSArray* nibViews= [[NSBundle mainBundle] loadNibNamed:#"MainView" owner:aController options:nil];
UIView* uiView= [nibViews objectAtIndex:0];
uiView.opaque= NO;
uiView.backgroundColor= [UIColor clearColor];
aController.cameraOverlayView= uiView;
self.mainViewController = aController;
[aController release];
mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
[window addSubview:[mainViewController view]];
[window makeKeyAndVisible];
}
This DOESN'T...
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// dislodge the MainView from the MainViewController
//
MainViewController *aController = [[MainViewController alloc] initWithNibName:#"MainView" bundle:nil];
aController.sourceType= UIImagePickerControllerSourceTypeCamera;
aController.delegate= aController;
aController.showsCameraControls= NO;
aController.cameraOverlayView= aController.view;
self.mainViewController = aController;
[aController release];
mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
[window addSubview:[mainViewController view]];
[window makeKeyAndVisible];
}