Loading views into NSContainerView with swift
"Container View defines a region within a view controller's view subgraph that can include a child view controller. Create an embed segue from the container view to the child view controller in the storyboard."
You mentioned NSContainerView, so I assume you're trying to do this on macOS, not iOS. Here's a useful article and code project (for iOS 6, but I was able to set up switchable subviews in iOS 9 using this as a guide):
http://sandmoose.com/post/35714028270/storyboards-with-custom-container-view-controllers
The important bits are using the embed segue, then wiring the view controllers together through a combination of viewDidLoad and prepareForSegue, and then finally loading one of the switchable view controllers (say one for each of your buttons) from the storyboard, where they are not connected to anything else.
Switching out the view embedded in a NSContainerView
You can replace a subview manually like so:
let oldView = self.view.window?.contentView!.subviews[0]
let frame = oldView.frame
oldView.removeFromSuperview()
device.view.frame = frame
self.view.window?.contentView!.addSubview(device.view)
Swift: Switch between NSViewController inside Container View / NSView
Just note that in order for this to work you have to add storyboard identifiers to your view controllers, which can by going to your storyboard then selecting the Identity Inspector in the right hand pane and then entering the Storyboard ID
in the Identity
subcategory.
Then this implementation of ViewController
would achieve what you are looking for.
import Cocoa
class ViewController: NSViewController {
// link to the NSView Container
@IBOutlet weak var container : NSView!
var vc1 : ViewController1!
var vc2 : ViewController2!
var vc1Active : Bool = false
override func viewDidLoad() {
super.viewDidLoad()
// Make sure to set your storyboard identiefiers on ViewController1 and ViewController2
vc1 = NSStoryboard(name: "name", bundle: nil).instantiateController(withIdentifier: "ViewController1") as! ViewController1
vc2 = NSStoryboard(name: "name", bundle: nil).instantiateController(withIdentifier: "ViewController2") as! ViewController2
self.addChild(vc1)
self.addChild(vc2)
vc1.view.frame = self.container.bounds
self.container.addSubview(vc1.view)
vc1Active = true
}
// You can link this action to both buttons
@IBAction func switchViews(sender: NSButton) {
for sView in self.container.subviews {
sView.removeFromSuperview()
}
if vc1Active == true {
vc1Active = false
vc2.view.frame = self.container.bounds
self.container.addSubview(vc2.view)
} else {
vc1Active = true
vc1.view.frame = self.container.bounds
self.container.addSubview(vc1.view)
}
}
}
Load more data when NSCollectionView is scrolled to end of content (macOS)
it looks, I solved it. Create an observation. And connect nscollectionview's scrollview and use this when I scrolling..:
if (self.scrollViewColl.verticalScroller?.floatValue.isEqual(to: 1.0))! && loading == false
Why is NSCollectionView not loading in Objective-C?
The registered class should be CollectionViewItem
instead of NSCollectionViewItem
. Change
[collectionView registerClass:[NSCollectionViewItem class] forItemWithIdentifier:collectionViewItem.identifier];
to
[collectionView registerClass:[CollectionViewItem class] forItemWithIdentifier:collectionViewItem.identifier];
It's in the error could not load the nibName: NSCollectionViewItem
, but I'm so used to NS
that I didn't notice.
Access NSCollectionViewItem outlets in the class with the NSCollectionView
This was fixed by casting the cell to a DCartCollectionCell.
let image = array[indexPath.item]
if let item = self.collectionView.makeItem(withIdentifier: "DCartCollectionCell", for: indexPath) as? DCartCollectionCell {
item.imgView.image = photo
return item
}
Related Topics
How to Reference the View's Window in Swift 3.X Using Storyboards/Cocoa
What's Wrong with My #If Target_Os_Simulator Code for Realm Path Definition
Define Struct That Is Treated Like a Class in Swift
How to Await X Seconds with Async Await Swift 5.5
Cannot Invoke Initializer for Type 'Sqlite3_Destructor_Type'
Why Doesn't Swift Force My Designated Initializer to Call Super
Creating a Subclass of Skshapenode
Data Ranged Subscribe Strange Behavior
How to Make Uicollectionview Reload Once It Receives Data from Firebase
How to Convert Dispatchtimeinterval to Nstimeinterval (Or Double)
Exc_Bad_Instruction Happens When Using Dispatch_Get_Global_Queue on iOS 7(Swift)
Firestore - How to Get Around Array "Does-Not-Contain" Queries
Is There a Number Type with Bigger Capacity Than U_Long/Uint64 in Swift