Cocos2d + UIViewController switch - uiviewcontroller

Its a game with cocos2d 2.1 beta.
I used seperate viewController that comes when pressing button. When I fast switch between these two then some time game hangs...Not crash..fps label works. Something like stopAnimation..
What's wrong with my code? How can I avoid hang? Only when fast switched.
-(void)showNativeView
{
UIViewController *controller = [[UIViewController alloc] init];
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] presentModalViewController:controller animated:NO];
[UIView animateWithDuration:1.0
animations:^{controller.view.alpha = 1.0;}];
[controller release];
}
-(void)gotoGameAgain
{
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[app navController].modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[[app navController] dismissModalViewControllerAnimated:YES];
}

Do you really need to initiate the UIViewController when the button is clicked? It may be better if you initiate only once and than use it when the button is clicked. Initiating it evertime you clicked on the button may be creating some performance issues during the fast switch...

Related

How to pass some variable to parent view controller?

I have a UIViewController its my main view controller. When the app starting that main viewcontroller is loading. Then there is a bar button. When I click on that, another view controller loding like this.
LoginViewController *viewController = [[LoginViewController alloc]
initWithNibName:#"LoginViewController" bundle:nil];
viewController.mainViewController = self ;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[navController setModalPresentationStyle:UIModalPresentationFormSheet];
navController.navigationBar.tintColor = [UIColor blackColor];
[self presentModalViewController:navController animated:YES];
In that new view controller I am doing some tasks and when click the close button, the results should be loaded in on the parent view controller.How can I do this. In parent view controller ViewDidAppear also not calling when I just close the top view controller. How can I do this? Please help me.
Thanks
First of all the line of code
[self presentModalViewController:navController animated:YES];
is deprecated and its been a long time, I don't know why are you still using it.
// Use below code instead
[self presentViewController:viewController animated:YES completion:nil];
Also you need to use protocols and delegates to pass any object to parent viewcontroller while you are dismissing the other viewcontroller.
If you can show up some code we might be able to figure out that why your viewDidAppear is not calling.

Open UIViewController modally from tab bar with transparent background and transition style

I've got a custom tab bar that I created using this link: https://github.com/jain-mohit/CustomTabBar. Now, I want to open a view modally after pressing a button. The opened view must be transparent, meaning I can see the view beneath it. The modal view also must a navigation bar so I can put a "Cancel" button to dismiss it. I've tried it like this:
DetailsViewController *detailViewController = [[DetailsViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:detailViewController];
nav.modalPresentationStyle = UIModalPresentationCurrentContext;
nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
detailViewController.view.backgroundColor = [UIColor greenColor];
detailViewController.view.alpha = 0.5f;
[self presentViewController:nav animated:YES completion:nil];
This method quite works but when opening, the view doesn't transition from bottom to top. And I also got this error:
Presenting view controllers on detached view controllers is discouraged <UINavigationController: 0x8f52280>
So, I'm looking for another way to do this. Hopefully someone can help. This method should work for iOS6, 7 and 8. Also I'm not using storyboard to do this.
Thank You.
Try to use UIWindow, instead of presenting a new ViewController:
UIWindow *w = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
w.windowLevel = UIWindowLevelAlert;
w.autoresizesSubviews = NO;
w.clipsToBounds = YES;
w.hidden = NO;
[w addSubview:detailViewController];
In this case background of your new ViewController will be transparent, but you can't use UINavigationController here...

UIViewController custom transition keyboard does not disappear

I'm presenting an UIViewController using a custom transition implementation:
JWModalController *modal = [[JWModalController alloc] init];
CGRect originalFrame = [[self view] convertRect:[[modal view] frame] toView:nil];
[[modal view] setFrame:CGRectZero];
[[self view] addSubview:[modal view]];
[self addChildViewController:modal];
[modal didMoveToParentViewController:self];
[UIView animateWithDuration:0.6 animations:^{
[[modal view] setFrame:originalFrame];
}];
This works great and i'm quite happy with the result.
BUT: When presenting a viewController the regular way ([self presentViewController:modal completion:nil];), the keyboard dismisses if a textfield is first responder.
When using my way, the keyboard does not dismiss.
Sure, I could easily keep track of the first responders or save the textField and call -resignFirstResponder manually... But I really want to know how to do it the right way.

UITabbarController change items dynamically

I want to edit UITabbarItems during the view life time.
The background is that I have an App with login view and according to whether the user is admin or not, the admin TabbarItem should be visible or not.
I'm using this code to initially create the UITabbarController in the AppDelegate:
// AppDelegate.m
settings = [[settingsViewController alloc] init];
settings.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Einstellungen" image:[UIImage imageNamed:#"settings.png"] tag:3];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:readState, friends, settings, nil];
When I try to manipulate the items later from another UIViewController, nothing happens and the UITabbar remains like it was before.
I actually tried two ways I could imagine:
[[self tabBarController] setToolbarItems:[[[self tabBarController] toolbarItems] arrayByAddingObject:admin] animated:NO];
[[self tabBarController] setViewControllers:[[[self tabBarController] viewControllers] arrayByAddingObject:admin] animated:NO];
How can I reach my goal? Thanks in advance, with kind regards, Julian
I figured out a workaround for my problem. I don't really like to import the AppDelegate but it seems as the tabbarController property is not being automatically set for the UIViewControllers of a UITabbarController.
// generate an instance of the needed UIViewController
adminViewController *admin = [[adminViewController alloc] init];
admin.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Admin" image:[UIImage imageNamed:#"admin.png"] tag:5];
// get the AppDelegate instance
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// get the »viewControllers« array of the AppDelegates UITabbarController as mutable array
NSMutableArray *viewControllersArray = [NSMutableArray arrayWithArray:[[appDelegate tabBarController] viewControllers]];
// insert the UITabbarItem of the needed UIViewController
[viewControllersArray insertObject:admin atIndex:2];
// Finally tell the app delegates UITabbarController to set the needed viewControllers
[[appDelegate tabBarController] setViewControllers:viewControllersArray animated:NO];

Adding a ViewController to Appdelegate

I am trying to add a View Controller to the Appdelegate Class. My code goes this way..
[self.view addsubView:viewcontroller.view];
But unfortunately i am not able to view the controller view. Please suggest if i am going wrong somewhere.Thanks for ur time.
The method in AppdidFinishLaunching is:-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
// [self.window addSubview:videoController.view];
self.window.rootViewController = videoController;
[self.window makeKeyAndVisible];
return YES;
}
and in the Load view of ViewController i have written as
UIButton *playMovie = [UIButton buttonWithType:UIButtonTypeRoundedRect];
playMovie.frame = CGRectMake(70,30,100,50);
[playMovie setTitle:#"Play Movie" forState:UIControlStateHighlighted];
[playMovie addTarget:self action:#selector(playMovie:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:playMovie];
The problem here is i am not able to view the button and the view.Please help.
Under no circumstances should you ever add a view controller's view manually to the interface like this. The app delegate is not, itself, a view controller; so viewcontroller is not its child. So the view is not yours to add. Use view controllers correctly: a view controller is either your app window's rootViewController, or it is some other view controller's child - and in either case, its view is placed into the interface for you automatically.
If this view is intended to be the root view of the app, then do what the Xcode 4.2 project templates do (e.g. the Single View Application): instantiate the view controller and assign it as self.window.rootViewController. If not, you must not use a view controller to add it as a subview as you are trying to do; just obtain the view and add it, without the intervening view controller.
It might help you to read my chapter on view controllers: http://www.apeth.com/iOSBook/ch19.html
Try below it works for me,
SFRmbrVC *viewController=[[SFRmbrVC alloc]initWithNibName:#"SFRmbrVC" bundle:nil];
[self.window.rootViewController addChildViewController:viewController];
viewController.parentController = self.window.rootViewController;
viewController.view.frame = self.window.rootViewController.view.frame;
[self.window.rootViewController.view addSubview:viewController.view];
viewController.view.alpha = 0;
[viewController didMoveToParentViewController:self.window.rootViewController];
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^
{
viewController.view.alpha = 1;
}
completion:nil];
The best steps to work out with this are
Create your own delegate and make an instance in the AppDelegate
eg.
#protocol MyAppDelegate <NSObject>
#optional
- (void)myApplicationWillResignActive:(UIApplication *)application;
#end
Make a call's of your delegate in the MyAppdelegate
eg.
- (void)applicationWillResignActive:(UIApplication *)application {
[myDelegate myApplicationWillResignActive:application]
}
Implement it in your viewController class (ViewController).
4.assign you viewController class instance to myAppdelegate instance in AppDelegate
eg.
MyAppDelegate*myDelegate=(MyAppDelegate*)[[UIApplication sharedApplication] delegate];
myDelegate=self; //self=viewController class