Memory not released after removing subviews (ARC) - ios

I am trying to solve "Received Memory Warning" issue.
My app has 2 view controllers and when you click a button on first view controller,
detail view controller appears.
The detail view controller has a view inherited of UIView called 'topView' and the view has many subviews.
The subviews are also inherited of UIView and each subview has 2 UILabels.
My question is when you go back to first view controller by clicking back button,
'topView' is not released, even if I put the following code in viewDidDisappear.
How can I release the memory of topView?
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[self.topView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
self.topView= nil;
}
Am I missing some thing?
I think I'm heading to some wrong direction, so please give me advice.

I'm not sure, but here's a thought. When you call this line:
[[self.topView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
It removes all of the subviews which declare self.topView as a superview. The next line:
self.topView = nil
Doesn't remove the top view itself, but rather nil's your pointer to it. Because views are retained by their superviews, this object will stick in memory until the view controller is removed and its view is released.

Check your variable declarations (weak, strong) to be sure that the topView object hasn't retain somewhere else.
Such as IBOutlet's usually declared with a Weak Key:
#property (weak, nonatomic) IBOutlet UIView * topView;

Related

Why is it null when I try to access data in another view controller? (Objective-C, XCode)

I am trying to use the .text of UITextField in my first view controller in another .text of UITextField in my second view controller, but my firstPage.firstTField.text turns out to be (null) in my second view controller even though I printed _firstTField.text in my first view controller and it printed out the input that was entered.
What may be the problem? Why is null?
FirstViewController.h
#property (weak, nonatomic) IBOutlet UITextField *firstTField;
SecondViewController.h
#property (weak, nonatomic) IBOutlet UITextField *secondTField;
SecondViewController.m
#import "FirstViewController.h"
- (void)viewDidLoad {
[super viewDidLoad];
FirstViewController *firstPage = [[FirstViewController alloc] init];
_secondTField.text = firstPage.firstTField.text;
}
You should treat a view controller's views as private. That's part of the appearance of the view controller, not it's function, and if objects outside of the view controller expect the views to look a certain way then when you change the appearance of the view controller's views it breaks other code. Bad.
In this situation it's worse. It just doesn't work, for the reason #nhgrif explains. You just created a new FirstViewController object, and it's views don't even exist yet. (A view controllers views are not created until the system is asked to display the view controller to the screen.)
You should create a property in your view controller that exposes the string(s) you need to read/write and use that instead.
However it's strange that you would create a new instance of a view controller and then immediately try to read text from one of it's fields. How can it possibly have useful data if the view controller was created on the line before? What are you expecting to happen?

Use of undeclared type ‘UIContainerView’

I’m using xcode 7 , I’ve a storyboard controller with an UIContainerView
When I’m trying to create an outlet to the controller there is this error "Use of undeclared type UIContainerView"
it’s not a bug of xcode 7 because there is the same error on xcode 6
i need to create an outlet because when i switch the segmented control i have to programmatically change the embed of the container
It's an error or i mustn't create an outlet for a container? It's seems that there is not something called UIContainerView in the library, it's strange
There is no such class called UIContainerView. You need to create an outlet of UIView and connect that to your container view.
You can switch the content of container view like:
// Property
#property (nonatomic, weak) IBOutlet UIView *container;
#property (nonatomic, strong) UIViewController *first;
#property (nonatomic, strong) UIViewController *second;
// Method that removes first vc from view and shows second vc
// Assumes first and second properties already initialized
- (void)showSecondVC
{
// Removes first view controller
[self.first.view removeFromSuperview];
[self.first willMoveToParentViewController:nil];
[self.first removeFromParentViewController];
// Shows second view controller
[self addChildViewController:self.second];
[self.second didMoveToParentViewController:self];
self.second.view.frame = self.container.bounds;
[self.container addSubview:self.second.view];
}
It is confusing because IB lablels it as UIContainerView, but it's type is really just a UIView.
UIContainerView is not a class, so you are getting error. Instead use UIView. Container view is actually a concept in storyboard, that allow you to do similar programming stuff:
Initialise a second view controller
Add it as child view controller
Add its view at location of container view with same frame.
When you add a container view then all above stuff is done automatically.
If you want to switch to different view controller then you will create multiple container view. Show and hide container views based on UISegmentedController's selectedIndex

Display UIview controller as a subview using storyboard IOS7

in my application I have a UIView object in second view controller. Now I want to display the UIView as a popup or sub view when I click the button from first view controller. Please tell me how to do this? I have seen many solution for the nib files but I didn't find any for storyboard.
I have connected the IBOulet of view in my second view controller.
#property (strong, nonatomic) IBOutlet UIView *popview;
And i have imported the second view controller in my firstview please tell me how to make this one i have been stuck here for long time please help me out on this one.
Thanks.
In your FirstViewController which has UIView *popview:
- (void)viewDidLoad
{
[super viewDidLoad];
// you don't want to show PopView
self.popview.alpha = 0;
}
- (void) showPopView
{
//you want to show PopView
self.popview.alpha = 1;
}
Try Adding this in your code
-(void)viewDidLoad
{
popView.hidden=true;
}
and when ever user clicks an action change the hidden property to false just like
-(IBAction)AnyActionPerferformed:(id)sender
{
popView.hidden=false;
}
You can Learn about this from
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html
You don't need an extra UIViewController
In your storyboard, drag a new UIView to your view controller. (It will be your popup view)
Link to your view controller. (#property (strong, nonatomic) IBOutlet UIView *popview;)
When you want to hide popview set it's alpha to 0.

Where in ViewController should UIGestureRecognizer be set up?

If ViewController's view is set to MyView
And ViewController maintains a pointer to it's view (set up via storyboards)
#property (weak, nonatomic) IBOutlet MyView *v;
Where in ViewController, should the following take place?
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:v action:#selector(pan:)];
[v addGestureRecognizer: panGesture];
viewDidLoad is a good place for this kind of initialization, since the view hierarchy will have been loaded into memory at that point, whether it was loaded from a storyboard or nib, or created programmatically.
Since it appears you are using a XIB file for your view, viewDidLoad is proper.

Update IBOutlet on Main View from Sub View

I have a main view which has a UISlider on it.
From the main view I add a subview using:
gameView *myViewController = [[gameView alloc] initWithNibName:#"gameView" bundle:nil];
[self.view addSubview:myViewController.view];
The subview is created on top of the main view.
When I remove the sub view using:
[self.view removeFromSuperview];
the main view underneath becomes visible.
I want to be able to update the value of the UISlider on the main view, from the sub view, before I call [self.view removeFromSuperview]
Is it possible?
Basically the question can be generalized to how to update an IBOutlet on the main view from the sub view.
Help is greatly appreciated.
Many thanks!
Yes, it's possible.
And there's a few ways to do this. Here's how I would do it:
First, make your parent view controller's UISlider a property that can be accessed by other objects.
Secondly, give your gameView object an instance variable that you'll link to the parent view (let's call it id savedParent;)
Then, before you do removeFromSuperview, you can simply do something like:
ParentViewController * parentVC = (ParentViewController *) savedParent;
if(parentVC)
{
// some float value of whatever you want to set the slider value to
parentVC.slider.value = 0.5f;
}
Also, why are you instantiating a whole View Controller object (gameView) if you simply want to add a subview? When you do your removeFromSubview call, the view gets removed but your gameView view controller isn't released (and might even be getting lost & leaked in memory, leading to a crash). If you want to do a subview, subclass UIView. If you want to push a new view controller, push the whole controller (and not just the view it contains).
Here is another way:
I'm not sure what the slider is representing, but you need to create an object that represents this
#interface MyGameThing : NSObject
#property (assign) CGFloat myValue;
#end
#implementation MyGameThing {
CGFloat *_value;
}
#synthesize myValue = _myValue;
#end
You then need to pass that object to both of your view controllers (or make it a singleton).
Then, on ParentViewController, in the viewWillAppear, just set the slider to the new value.
Daniel.
(p.s. don't just add view controllers views to the superview, use presentModalViewController / dismissModalViewController or a navigation controller).

Resources