What holds strong reference onto my viewController?
You can use profile the code with Instruments. In Xcode, select Product > Profile. When Instruments loads, select Allocations as the template.
This will allow you to trace which objects are setting what strong references.
Swift: Why aren't my UITextFieldDelegate methods being called?
In UITextField
, delegate
is declared as a weak
property.
weak var delegate: UITextFieldDelegate? { get set }
This means that it doesn't retain the delegate
instance and will be set to nil
when the delegate
is freed.
You are creating the delegate
and assigning it to a local variable myTextFieldDelegate
. That variable will be freed at the end of the function, so even though you assigned myTextFieldDelegate
to the myTextField.delegate
, it is freed and myTextField.delegate
gets set to nil
because it is weak.
To fix this, create a property in your ViewController
to hold the delegate object:
var myTextFieldDelegate: MyTextFieldDelegate?
Deallocate view controllers in navigation controller that have a reference to self
As the other answers have said you need to make it a weak reference like this:
weak var controller: UIViewControler?
However I would go further and say that you should not be keeping a reference to to a UIViewController inside any UIView based object (UIImageView, UITextField, etc). The UIViews should not need to know anything about their UIViewControllers.
Instead you should be using a delegation pattern. This is a basic example:
1) Create a protocol for the custom UIImageField like this:
protocol MyImageFieldProtocol: class {
func imageTapped()
}
2) Then add a delegate like this:
weak var delegate: MyImageFieldProtocol?
3) Your UIViewController then conforms to the protocol like this:
class MyViewController: UIViewController, MyImageFieldProtocol {
}
4) Somewhere inside the view controller (viewDidLoad is usually a good place you assign the view controller to the image views delegate like this:
func viewDidLoad {
super.viewDidLoad()
myImageView.delegate = self
}
5) Then add the function to respond to the protocol action to the view controller like this:
func imageTapped {
self.performSegue(withIdentifier: "MySegue", sender: nil)
}
Retain Cycle in ARC
A retain cycle is a situation when object A
retains object B
, and object B
retains object A
at the same time*. Here is an example:
@class Child;
@interface Parent : NSObject {
Child *child; // Instance variables are implicitly __strong
}
@end
@interface Child : NSObject {
Parent *parent;
}
@end
You can fix a retain cycle in ARC by using __weak
variables or weak
properties for your "back links", i.e. links to direct or indirect parents in an object hierarchy:
@class Child;
@interface Parent : NSObject {
Child *child;
}
@end
@interface Child : NSObject {
__weak Parent *parent;
}
@end
* This is the most primitive form of a retain cycle; there may be a long chain of objects that retain each other in a circle.
Related Topics
How to Limit an iOS App Only to 4 Inch Screen Devices
Dashed Line Border Around Uiview
Uiview Backgroundcolor Disappears When Uitableviewcell Is Selected
Iphone:How to Detect the End of Slider Drag
Xcode 4: Build Failed, No Issues
Referring to Weak Self Inside a Nested Block
iOS Different Font Sizes Within Single Size Class for Different Devices
Https iOS with Self Signed Certificate
Dateformatter Returns Unexpected Date for Timezone
Googlemaps Basic iOS Demo App Crash - Unrecognized Selector Sent to Instance
How to Get the Height and Width of an Uiimage
Uicollectionview Auto Scroll to Cell at Indexpath
Error Itms-90164/90046: Invalid Code Signing Entitlements