Why UISearchController is not released on tvOS? - tvos

I'm using the next code to initialize a search stack:
- (IBAction)openSearch:(id)sender {
TVSearchResultsController *resultsController = [TVSearchResultsController new];
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:[TVSearchResultsController new]];
searchController.searchResultsUpdater = resultsController;
[self presentViewController:searchController animated:YES completion:nil];
}
After search controller is dismissed, both UISearchController and TVSearchResultsController stay in memory and dealloc will never be called, that leads to a memory leak.
I've also tried to use UISearchContainerViewController but results are same.
What am I doing wrong? How to initialize and present UISearchController properly on tvOS?

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.

returning to a uiviewcontroller without navigationcontroller

my application has a UIViewController that isn't wrapped in a UINavigationController from it I call different UINavigationControllers each representing a state in a state machine.
calling these UINavigationControllers from code is done like this:
-(void) callState1
{
[popover dismissPopoverAnimated:YES];
UINavigationController *state1NavigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"state1Navigation"];
[self presentViewController:state1NavigationController animated:NO completion:nil];
}
in state1 there is a custom UIBarButtonItem representing the back control.
self.navigationItem.leftBarButtonItem = self.backBtn;
Is there a native code that returns to the calling UIViewController?
or should I implement the same code from callState1 in the back button calling the UIViewController, and if so how can I synchronize the data between them?
Since you are presenting the view controller (state1NavigationController) modally, you need to use the method dismissViewControllerAnimated:completion: to dismiss the state1NavigationController and go back.

Cocos2d + UIViewController switch

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...

Need right direction for reload application by action

My scenario is a UIView Controller displays different subview programmatically... Like a game board. Now I'm in a incredible situation:
I can't find a way to reload application after the game is finish!? What I would like to do is after terminate scenario clic on IBAction for star new game. My doubt is about ViewDidLoad or call Main.
try moving your code from viewDidLoad into a new function like -(void)startGame or -(void)refreshViews: and call that function whenever you want to reload your view. even in viewDidLoad you can load your view using this function.
-(void)viewDidLoad
{
[super viewDidLoad];
[self startGame];
}
remember if you are creating allocating and add some of your subviews outside this function then they will remain visible untill and unless yu remove them from superView before calling startGame.
I solved it with this code:
(IBAction)newGame:(id)sender{
UIView *parent = self.view.superview;
[self.view removeFromSuperview];
self.view = nil; // unloads the view
[parent addSubview:self.view];
}

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