I am developing an mac osx application which have initial window and viewcontroller launched from main storyboard. I want to replace the content view loaded by storyboard with my view.
I am doing this -
func replaceContentView() {
parentViewController = MainViewController(nibName: "MainContainerView", bundle: nil)!
let fullScreenFrame = NSScreen.mainScreen()?.visibleFrame
self.initialWindow.setFrame(fullScreenFrame!, display: false, animate: false)
self.initialWindow.contentView = parentViewController! . view
}
Problem with this approach is that the default viewcontroller is never getting deallocated. deinit() of default viewController is not called at all.
This is causing memory leak. So how to completely remove default content view and associated viewcontroller?
Storyboards don't deal in views, they deal in viewcontrollers. What a Storyboard does when it loads a view into a window is that it creates an NSViewController and then goes
windowController.contentViewController = theViewController
That implicitly also inserts theViewController.view as the window's content view. So do the same, and all will be fine.
Marek's example is wrong, because CustomView shouldn't be an NSView subclass, it should be a CustomViewController class that owns a view containing the label etc. As a bonus, NSViewController will take care of loading the XIB for you as well.
Alternately, you could set windowController.contentViewController = nil (which will remove the old view controller and its content view) and then set your content view. But really, why fight the framework when that's exactly what NSViewController is intended for?
You can write the code in deinit method,may it will help you.
deinit {
// perform the deinitialization
}
Your contentViewController within NSWindow instance still holds strongly its old view. You have replaced just property on your NSWindow instance.
To clarify what you have done:
NSWindow holds strongly against new view
NSViewController holds strongly against old view
You should assign your new view into contentViewController.view property as well
This might be helpfull:
NSWindow.h
/* NSViewController Support */
/* The main content view controller for the window. This provides the contentView of the window. Assigning this value will remove the existing contentView and will make the contentViewController.view the main contentView for the window. The default value is nil. The contentViewController only controls the contentView, and not the title of the window. The window title can easily be bound to the contentViewController with the following: [window bind:NSTitleBinding toObject:contentViewController withKeyPath:#"title" options:nil]. Setting the contentViewController will cause the window to resize based on the current size of the contentViewController. Autolayout should be used to restrict the size of the window. The value of the contentViewController is encoded in the NIB. Directly assigning a contentView will clear out the rootViewController.
*/
#availability(OSX, introduced=10.10)
var contentViewController: NSViewController?
/* The view controller for the window's contentView. Tracks the window property of the same name.
*/
#property (strong) NSViewController *contentViewController NS_AVAILABLE_MAC(10_10);
However what you do seems incorrect if you do this on launch.
You either set custom subclass of contentView to your new nsview subclass which can load it's view from another XIB (no need for storyboard).
Abstract example:
class CustomView: NSView {
#IBOutlet var contentView: NSView!
#IBOutlet weak var label: NSTextField!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initSubviews()
}
override init(frame: CGRect) {
super.init(frame: frame)
initSubviews()
}
func initSubviews() {
let nib = NSNib(nibName: "CustomView", bundle: nil)
nib.instantiateWithOwner(self, topLevelObjects: nil)
contentView.frame = bounds
addSubview(contentView)
}
}
PS: topLevelObjects is set to nil because you hold strongly contentView. So no need to worry about memory management.
Related
I'm trying to use a custom view I created.
I use instantiation from nib, but it causes an infinite loop which I'm not sure how to fix. Any idea?
Here is the image of the run result:
And here's the code that causes the issue:
// MARK: - Init & Setup
// Needed for IBDesignable
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup(){
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = UIViewAutoresizing(rawValue: UIViewAutoresizing.RawValue(UInt8(UIViewAutoresizing.flexibleWidth.rawValue) | UInt8(UIViewAutoresizing.flexibleHeight.rawValue)))
addSubview(view)
}
func loadViewFromNib() -> UIView{
let bundle = Bundle(for:type(of: self))
let nib = UINib(nibName: "LoginView", bundle: bundle) // TEST: changin bundle from bundle-> nil
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
Edit: Here's an image of the connection
Thanks :)
Quick answer:
Since the top-level view in the Xib had its custom class set to YourCustomeView, the xib loading process loadViewFromNib will then call your initWithCoder method again ⛔️🚫♽.
Quick fix:
In the Xib, instead of setting the custom class of the view as YourCustomeView, set YourCustomeView as The File Owner of the Xib.
More info
https://medium.com/#anandin02/loading-custom-views-in-ios-the-right-way-bedfc06a4fbd
It is not clear from your example (please next time provide the whole file, not just parts of it), but it looks like you are trying to instantiate LoginView from itself.
init() -> setup() -> loadViewFromNib() -> init() -> ...
The problem is that the instantiate will call the LoginView's init() method, which will again call loadViewFromNib(). I hope you see the problem now.
What are you trying to do with loadViewFromNib?
Edit: In the top right corner of your second image there is a "Custom class" section. There, in the "class" field you have to provide the name of your controller (LoginViewController or something like that). This way you connect your view and controller, and you don't need that custom loadViewFromNib method that causes your problem.
For anyone having this issue, if you register a Nib for a table or collection view, you are telling that parent view to go and load a nib named 'x' whenever it needs to dequeue a cell.
Normally with Nib code, you'll want a method to go and load the actual XML that makes your layout to tie it to the Nib class, but when you register a reusable view, your registration means that the parent view is responsible for doing this. It will literally go and load an XML file for you and try and tie that to a class when you cast it. By adding another loadNib call inside of this will then cause an infinite loop to happen.
All you need to do is set the cell Nib's class to your custom class, register it and it will do the rest for you with reusable cells - remove the loadNib method from any initialisers inside your cell and add registration code on the collection or table view class.
I have a specific situation but what I'm looking for is a generic solution. Currently I have a UIImageView that contains an image, a few labels, and multiple levels of constraints. I would like to configure this set of controls' properties once and reuse them inside of multiple controllers. Such that if I have to update this set, I would do it in one place and all the controller instances would get the change (sort of like how Sketch works with symbols).
You, sir, need a custom View!
My typical approach for this is to create an xib file, design the view I need, and create a class that subclasses UIView.
When you do this, you can assign the class of the xib File's Owner (in interface builder) and link up any #IBOutlets from the view to your custom class.
For the class, you'll need to implement a few methods. Here is an example custom view:
class LoadingView: UIView {
#IBOutlet var view: UIView!
#IBOutlet weak var messageLabel: UILabel!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadViewFromNib()
setUpView()
}
override init(frame: CGRect) {
super.init(frame: frame)
loadViewFromNib()
setUpView()
}
func setUpView() {
self.view.layer.cornerRadius = 10.0
self.view.layer.masksToBounds = true
}
private func loadViewFromNib() {
let bundle = Bundle.init(for: self.classForCoder)
bundle.loadNibNamed("LoadingView", owner: self, options: nil)
self.view.frame = bounds
self.addSubview(self.view)
}
}
You are required (pun intended) to implement the required init and the override init methods, and the other two are (kind of) optional. The loadViewFromNib is a convenience method that implements the logic to actually load the xib file from your app's bundle.
Don't forget to match the nib name with your xib file name! You'll thank me later. ;)
You can use this view in storyboards and use constraints, etc. by placing a regular old view and assigning its class to your custom class.
You can also play around with #IBDesignable to actually see your custom view in interface builder, though it tends to constantly reload and slow down Xcode unless you toggle a setting that I can't remember the name of right now (sorry!).
Enjoy!
What you want is not possible exactly in the way you describe it but there's a way to achieve the same result.
Create a subclass of UIView that will contain all the content you want, once you do that there are two options.
The first (and best, imo) option is to generate your layout with code when the view is initialized. This will allow you to add the view to other view controllers and it will initialize itself. The downside of this method is that you'll need to create the constraints with code.
The second option is to create a xib with your views and constraints and initialize your custom class from that xib. The downside of this is that you'll have to instantiate your view with code and place it in the view hierarchy yourself. You could create a container in the storyboard where you will add the view and pin it to the edges.
The main ViewController is embedded in a UINavigationController subclass, and the VC has a subview that is loaded from a nib. The subview is called MenuView, and contains UIButtons that will link to other VCs.
To keep my main ViewController less unruly, I have put all these buttons into a subview that loads from a nib that animates the menu opening and closing.
However, I would like to present other view controllers from these, sometimes "Modally", sometimes "Show". What I have done seems to work, but I just want to know if this is alright, or if I have caused some unwanted effects that I'm unaware of (like a strong reference cycle that would cause a memory leak, or something). Or is there a better way to do this?
Some code:
In MenuView.swift
class MenuView: UIView {
var navigationController = CustomNavigationController()
func combinedInit(){
NSBundle.mainBundle().loadNibNamed("MenuViewXib", owner: self, options: nil)
addSubview(mainView)
mainView.frame = self.bounds
}
#IBAction func optionsAction(sender: AnyObject) {
self.navigationController.performSegueWithIdentifier("presentOptions", sender: self)
}
In ViewController.swift
menuView.navigationController = self.navigationController as! CustomNavigationController
Short answer: No, it is not alright to access a view controller from within some view in the hierarchy, because that would break all the MVC rules written.
UIView objects are meant to display UI components in the screen and are responsible for drawing and laying out their child views correctly. That's all there is. Nothing more, nothing less.
You should handle those kind of interactions between views and controllers always in the controller in which the view in question actually belong. If you need to send messages from a view to its view controller, you can make use of either the delegate approach or NSNotificationCenter class.
If I were in your shoes, I would use a delegate when view needs some information from its view controller. It is more understandable than using notification center as it makes it much easier to keep track of what's going on between. If the view controller needs some information from a view (in other words, the other way around), I'd go with the notification center.
protocol MenuViewDelegate: class {
func menuViewDidClick(menuView: MenuView)
}
class MenuView: UIView {
var weak delegate: MenuViewDelegate?
#IBAction func optionsAction(sender: AnyObject) {
delegate?.menuViewDidClick(self)
}
}
Let's look at what's going on at the view controller side:
class MenuViewController: UIViewController, MenuViewDelegate {
override func viewDidLoad() {
...
self.menuView.delegate = self
}
func menuViewDidClick(menuView: MenuView) {
navigationController?.performSegueWithIdentifier("presentOptions", sender: self)
}
}
For more information about communication patterns in iOS, you might want to take a look at this great article in order to comprehend how they work.
I was using this example which explains the use of NSNotification.
In my case, I have a UIViewController in which I have a UITableView. To this tableview I am assigning a dataSource and delegate programatically by instatiating my UITableViewController. So far I have not declared any inits, and thus have been using the simple init() to initialize my UITableViewController. (This UITableViewController is not on the StoryBoard).
class foo: UIViewController{
#IBOutlet weak var fooTable: UITableView!
var fooTableViewController = MyTableViewController()
override func viewDidLoad(){
super.viewDidLoad()
fooTable.delegate = fooTableViewController
fooTable.dataSource = fooTableViewController
}
}
class MyTableViewController: UITableViewController {
override func viewDidLoad(){
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "notificationReceived", name: "TEST_NOTIFICATION", object: nil)
}
}
If I try to add the observer in viewDidLoad() of the UIViewController, it does not work.
So my question is: Does using the NSNotification require the usage of init(coder aDecoder: NSCoder)? If so, then what is the correct way to initialize using this init in swift? How should I be instantiating MyTableViewController in my UIViewController instance foo?
viewDidLoad is only called when the view of a view controller is loaded - in the code you're showing you create a table view controller subclass, assign it as the datasource and delegate of another table view (confusing, as it will already be the datasource and delegate of its own table view), but never actually do anything with the table view controller's view.
This means that viewDidLoad will not be called.
You should probably be adding your table view controller's tableView as a subview and also adding it as a child view controller so that rotation and appearance events are forwarded properly.
Note that the question and answer are nothing whatsoever to do with notification centers or Swift, but just about understanding the view controller lifecycle.
If you want a separate object to act as a datasource and delegate for your table view, great idea, but don't use a UITableViewController subclass. Just create a plain object which conforms to the data source and/or delegate protocols.
I am writing a master-detail application. When table row item is clicked, I load the image in the DetailViewController, which has a UIImageView. I assume the UIImageView is instantiated by app and I only need to set the UIImage.
imageView.image = UIImage(name:"camera")
However, the imageView is nil. I sure that the imageView is linked with UIImageView on storyboard.
#IBOutlet weak var imageView: UIImageView!
func configureView() {
if !imageView {
println("nilnilnil") // nilnilnil is printed...
}
}
You need to make sure that the view has already been loaded. You should at least wait until after viewDidLoad. Usually you would do this directly in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.image = UIImage(name: "camera")
}
This is because the subviews are not created and the connections are not setup until the view is loaded. The view is not loaded until the view property of the view controller is accessed – usually, this is when the view controller is presented for the first time.
Minor Note: A variable is "instantiated" or "initialized" not "instanced"