I constantly find "leaks" using Xcode instruments (8.2) where free is the last call on the object/memory. If free is being called successfully then why would instruments be reporting this as a leak? Is this really a leak or a bug in instruments?
Here is one example
For example, this small segment of code seems to cause instruments to report leaks like these.
class RDTranslateComponentView: RDMessageCellComponentView {
let textView: RDTextMessageCellComponentView
let button: UIButton
convenience init() {
self.init(frame: CGRect.zero)
}
override init(frame: CGRect) {
self.textView = RDTextMessageCellComponentView()
self.button = UIButton(type: .custom)
super.init(frame: frame)
self.setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupView() {
self.textView.label.font = UIFont.proximaSemiBold(withSize: 14)
self.textView.label.textColor = UIColor.remindBrand()
self.textView.bubble.backgroundColor = UIColor.white
self.addSubviews([ self.textView, self.button ])
self.button.addTarget(self, action: #selector(pressedButton(_:)), for: .touchUpInside)
}
#objc private func pressedButton(_ sender: UIButton?) {
/// Code that calls into a delegate
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
return self.textView.sizeThatFits(size)
}
override func layoutSubviews() {
super.layoutSubviews()
self.textView.frame = self.bounds
self.button.frame = self.bounds
}
}
class RDMessageCellComponentView: UIView {
/// The inside of this is just a bag of constants. Doesn't even touch anything View related so I'm omitting.
}
Here's a stack trace from my code in association to a leak. Keep in mind this isn't the only place this happens. This case props up often though and seems to be exclusive to code that has UILabel instantiated inside the init method of a subview that is added as a subview to another view.
Related
I am currently developing an iOS application with login and sign up forms. To make sure that the keyboard does not cover any UITextFields I've implemented the following solution provided by Apple and discussed in this issue.
To briefly sum it up, this solution uses a UIScrollView in which the different UI elements are placed and UIKeyboardDidShowNotification and UIKeyboardDidHideNotification to move the elements up and down when the keyboard appears/disappears so that the UITextFields aren't hidden.
This works like a charm except for one thing: for all my UIViewControllers I have to repeat the same code. To tackle my problem I have tried:
to create a base UIViewController, providing an implementation for the different functions, that can be subclasses by the other UIViewControllers;
to use a protocol and a protocol extension to provide a default implementation for the different functions and make my UIViewControllers conform to it.
Both solutions didn't solve my problem. For the first solution, I wasn't able to connect the UIScrollView of my base class through the Interface Builder although it was declared.
#IBOutlet weak var scrollView: UIScrollView!
When trying to implement the second solution, the UIViewController implementing my protocol somehow did not recognise the declared methods and their implementations.
The protocol declaration:
protocol ScrollViewProtocol {
var scrollView: UIScrollView! { get set }
var activeTextField: UITextField? { get set }
func addTapGestureRecognizer()
func singleTapGestureCaptured()
func registerForKeyboardNotifications()
func deregisterForKeyboardNotifications()
func keyboardWasShown(notification: NSNotification)
func keyboardWillBeHidden(notification: NSNotification)
func setActiveTextField(textField: UITextField)
func unsetActiveTextField()
}
The protocol extension implements all functions expect for the addTapGestureRecognizer() as I would like to avoid using #objc:
extension ScrollViewProtocol where Self: UIViewController {
// The implementation for the different functions
// as described in the provided links expect for the following method
func registerFromKeyboardNotifications() {
NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardDidShowNotification, object: nil, queue: nil, usingBlock: { notification in
self.keyboardWasShown(notification)
})
NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardDidHideNotification, object: nil, queue: nil, usingBlock: { notification in
self.keyboardWillBeHidden(notification)
})
}
}
Does anyone have a good solution to my problem, knowingly how could I avoid repeating the code related to moving the UITextFields up and down when the keyboard appears/disappears? Or does anyone know why my solutions did not work?
I found a solution. I'll post it in case someone once to do the same thing.
So, I ended up deleting the UIScrollView outlet in my base class and replacing it with a simple property that I set in my inheriting classes. The code for my base class look as follow:
import UIKit
class ScrollViewController: UIViewController, UITextFieldDelegate {
// MARK: Properties
var scrollView: UIScrollView!
var activeTextField: UITextField?
// MARK: View cycle
override func viewDidLoad() {
super.viewDidLoad()
let singleTap = UITapGestureRecognizer(target: self, action: #selector(singleTapGestureCaptured))
scrollView.addGestureRecognizer(singleTap)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
registerForKeyboardNotifications()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
deregisterFromKeyboardNotifications()
}
// MARK: Gesture recognizer
func singleTapGestureCaptured(sender: AnyObject) {
view.endEditing(true)
}
// MARK: Keyboard management
func registerForKeyboardNotifications() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWasShown), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillBeHidden), name: UIKeyboardWillHideNotification, object: nil)
}
func deregisterFromKeyboardNotifications() {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWasShown(notification: NSNotification) {
scrollView.scrollEnabled = true
let info : NSDictionary = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
var aRect : CGRect = self.view.frame
aRect.size.height -= keyboardSize!.height
if let activeFieldPresent = activeTextField {
if (!CGRectContainsPoint(aRect, activeFieldPresent.frame.origin)) {
scrollView.scrollRectToVisible(activeFieldPresent.frame, animated: true)
}
}
}
func keyboardWillBeHidden(notification: NSNotification) {
let info : NSDictionary = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
view.endEditing(true)
scrollView.scrollEnabled = false
}
// MARK: Text field management
func textFieldDidBeginEditing(textField: UITextField) {
activeTextField = textField
}
func textFieldDidEndEditing(textField: UITextField) {
activeTextField = nil
}
}
And here is the inheriting class code:
class ViewController: ScrollViewController {
#IBOutlet weak var scrollViewOutlet: UIScrollView! {
didSet {
self.scrollView = self.scrollViewOutlet
}
}
// Your view controller functions
}
I hope this will help!
I am trying to create a subclass of UIViewController and then through the storyboard or programmatically pass data to it. What I have tried so far is to have a subViewController swift file with a class subViewController that is a UIViewController
import UIKit
class subViewController: UIViewController {
var s1 = String()
init(myString: String) {
self.s1 = myString
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
#IBOutlet var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
print(self.s1)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
then in Identity Inspector I connect this to the view controller
and in AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let SubVC = subViewController(myString: "Komic")
self.window?.rootViewController = SubVC
self.window?.makeKeyAndVisible()
return true
}
This loads a black screen but it logs out myString. As far as I understand it doesn't create the view but it just creates the instance of the class and that's why it's giving me the black screen. I also know that this part
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
runs when the storyboard gives you the view. But I can't pass data in there that's why I am trying to do this programmaticaly. Is it possible somehow to do that? I know I could instantiateWithIdentifier with the storyboard but I can't find a way to pass my data through that....any help?
An (ugly) way to solve this issue:
You can set your var s1 from an external buffer in your code (AppDelegate variable in this example)
required init?(coder aDecoder: NSCoder) {
self.s1 = UIApplication.shared().delegate.bufferForS1
super.init(coder: aDecoder)
}
And when you initiate your UIViewController through Storyboard:
UIApplication.shared().delegate.bufferForS1 = myS1Value
self.navigationController!.pushViewControllerFading(self.storyboard!.instantiateViewController(withIdentifier: "myViewControllerID") as UIViewController)
You are not loading the view controller from the storyboard when you call your custom initialiser, if you create a customer initialiser it is your responsibility to create the view hierarchy programatically - typically in loadView() although lots of people do it in viewDidLoad().
In Order to the load the view hierarchy you defined in the storyboard you can do this:
let storyBoard = UIStoryboard(name: "storyboardName", bundle: NSBundle.mainBundle())
let viewController = storyBoard.instantiateViewControllerWithIdentifier("storyboardId")
You define the storyboard Id of a view controller in the identity inspector
I am trying to make a SystemStatusBar popover for Mac. (Roughly, translating this Cocoa app to a Swift app). However, the view that I am using never shows up and the popup appears in the bottom left of the screen replicating the StatusBarItem.
This is what I expect (and that happens in the example from case of example from the link):
and this is what actually shows up (in my, Swift version of the application) instead of the NSPopover being shown in the StatusBar [Showing or Hiding the popup is controlled by the two buttons as shown in the previous figure. In this screenshot I have not added that window as it remains the same.]:
This is the AppDelegate:
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var window: NSWindow!
var statusView : StatusView!
var popController: PopViewController!
#IBAction func showPop(sender: NSButton)
{
statusView.showPopup()
}
#IBAction func hidePop(sender: NSButton)
{
statusView.hidePopup()
}
func applicationDidFinishLaunching(aNotification: NSNotification)
{
var height = NSStatusBar.systemStatusBar().thickness
statusView = StatusView(frame: NSMakeRect(0, 0, CGFloat(height), CGFloat(height)))
}
}
The CustomView:
class StatusView : NSView, NSMenuDelegate
{
var imageView: NSImageView!
var statusItem: NSStatusItem!
var popover: NSPopover!
var popController: PopViewController!
required init? (coder: NSCoder)
{
super.init(coder: coder)
}
override init(frame frameRect: NSRect)
{
var height = NSStatusBar.systemStatusBar().thickness
imageView = NSImageView(frame: NSMakeRect(0, 0, CGFloat(height), CGFloat(height)))
statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(CGFloat(height))
super.init(frame: frameRect)
imageView.image = NSImage(named: "mf-image-black.png")
self.addSubview(imageView)
statusItem.view = self
popover = NSPopover()
popController = PopViewController(nibName: "PopViewController", bundle: nil)
popController.view = self
popover.contentViewController = popController
}
func showPopup()
{
if(!popover.shown)
{
popover.showRelativeToRect(self.frame, ofView: self, preferredEdge: NSMinYEdge)
}
}
func hidePopup()
{
if(popover.shown)
{
popover.close()
}
}
}
and the ViewController:
class PopViewController: NSViewController
{
#IBOutlet var statusView: StatusView!
override init?(nibName: String?, bundle: NSBundle?) {
super.init(nibName: nibName, bundle: bundle)
}
required init?(coder: NSCoder)
{
super.init(coder: coder)
}
}
I am not exactly sure what is that I am missing here. The StatusItem never seems to make use of the PopViewController nib.
changing corner radius of NSView should be pretty straight forward however i am getting error message "fatal error: Can't unwrap Optional.None". is there a chance i am doing this with 10.9 not 10.10 and this is happening due framework differences? or the code is wrong.
class aRoundView: NSView {
let cornerRad = 5.0
init(frame: NSRect) {
super.init(frame: frame)
self.layer.cornerRadius = cornerRad
}
override func drawRect(dirtyRect: NSRect)
{
super.drawRect(dirtyRect)
NSColor.redColor().setFill()
NSRectFill(dirtyRect)
}
}
EDIT
calling it in
func applicationDidFinishLaunching(aNotification: NSNotification?) {
let aView = mainViewTest(frame: NSMakeRect(0, 0, 100, 100))
self.window.contentView.addSubview(aView)
}
actually it is not just that. any iteration with self.layer gives same result, backgroundcolor etc.
That is because self.layer is an optional value, which is currently not set. Add self.wantsLayer = true before self.layer.cornerRadius, to make sure a proper layer exists.
init(frame: NSRect) {
super.init(frame: frame)
self.wantsLayer = true
self.layer.cornerRadius = cornerRad
}
When I create a new subclass of UITextView in the Xcode 6 Beta, the following code is automatically provided.
import UIKit
class TerminalView: UITextView {
init(frame: CGRect) {
super.init(frame: frame)
// Initialization code
}
}
The previous code (completely provided by Xcode with nothing removed) gives the following error.
Must call a designated initializer of the superclass 'UITextView'
As far as I know, the designated for all subclasses of UIView is -initWithFrame: (or in Swift, init(frame:). If this is the case, why does the code provided by Xcode result in an error? I have added no new instance variables to the class, so nothing else has to be initialized yet.
It seems as though the only initializer that works for now is:
super.init(frame: CGRect, textContainer: NSTextContainer?)
which can be called with
super.init(frame: CGRect.zero, textContainer: nil)
This is most likely a bug in the initial beta release and will be fixed in upcoming beta releases.
For 2020:
class SpecialText: UITextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
common()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
common()
}
private func common() {
backgroundColor = .yellow
font = .systemFont(ofSize: 26)
textColor = .green
}
}