How to change NSViewController Tab Programmatically on macOS applications - swift

I have a macOS application that have a TabViewController. The controller has 2 tabs and ViewControllers.
I Also have another viewController that is used as a sidebar with buttons.
I am trying to figure out how can I change the tab view or tab index using the buttons from my sidebar
Not sure where to start with coding.
I created an #IBAction with the following code in my view controller class
#IBAction func buttonTapped(_ sender: UIButton) {
self.tabBarController.selectedIndex = 1
}
No success

Related

How do I enable miniaturize button of new/additional NSViewController in swift macOS App

step1. I new a project: macOS App, swift and storyboard
step2. The default NSViewController named InitialViewController. I add a button named open.
step3. I add a NSViewController named NewViewController.
step4. I linked the button to the NewViewController with Modal kind
step5. I run the App and click the button, and then I found that the miniaturize button of NewViewController is disable.
How do I enable the miniaturize button of NewViewController? I have tried to add the code shown below but it's not working.
class NewViewController: NSViewController {
override func viewDidAppear() {
self.view.window?.standardWindowButton(.miniaturizeButton)?.isEnabled = true
}
}

How to reference a View from within a Window Controller?

I'm having a Window Controller with a toolbar. I also have a View Controller containing some views. How do I reference a view from the View Controller within my Window Controller? I'm still learning macOS development and I'm missing the bigger picture how code is structured and classes are meant to interact.
My concrete problem right now is this: Using XCode 9.4.1 I have a window with a toolbar and a button in it. That's how my WindowsController.swift looks like:
import Cocoa
class WindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
window?.titleVisibility = .hidden
}
#IBAction func startExport(_ sender: NSButton) {
print("Start Export")
}
}
In the ViewControllerScene there's a WKWebView that's loading a web page. When the button in the toolbar is pressed, I want to call that Web Views takeSnapshot method. So I need a reference in WindowsController.swift to that Web View, but control-dragging the Web View from the storyboard to WindowsController.swift in the assistant editor doesn't let me create that outlet.
This:
let vc = contentViewController as? ViewController
will take you to your view controller.

Xcode TabBar controller logout issue

I have a tab bar controller in my app. one of the tabs has a navigation Controller with a bar button. clicking the bar button segues to a tableViewController which has another button in it. The button segues to yet another TableViewController which includes a logout button.
#IBAction func logoutDidTap(_ sender: Any) {
try! FIRAuth.auth()?.signOut()
when I login to the app again and click on that tab, it takes me to the TableViewController with the logout button instead of the beginning of the tab. How can I fix this?
Since you have placed all the view controllers under the navigation controller, so you can easily pop them from the navigation stack when you are done logging out. Here's how to do it:-
#IBAction fund logoutDidTap(sender:Any){
try! FirAuth.auth()?.signout()
var viewControllers = navigationController?.viewControllers
viewControllers?.removeLast(2) // views to pop
navigationController?.setViewControllers(viewControllers!, animated: true)
}

Clicking the Button will move to the next controller?

How to Move to the Next Controller with a single click of a Button?
There is no data transfer, only one click of button and it will proceed to the next controller.
I created a controller named "ViewIntro" which is the initial controller. It's basically a Welcome Screen and there is a button at the bottom.
Once the button is clicked, it will proceed to the viewController which is the original controller whenever we start an xcode project I forgot to make the viewController as my welcome screen
Anyways, here is my current code of my ViewIntro:
`import Foundation
import UIKit
class ViewIntro : UIViewController{
#IBOutlet weak var btnEnter: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}`
I also had connected the ViewIntro to ViewController through identifier as shown in the attached image.
Right click your button and drag it to your controller. Just like creating an IBOutlet but you should select Action mode. Afret creating an IBAction for your button, just add this code into it:
self.performSegue(withIdentifier: "showMain", sender: nil)

Adding views with IBAction to a NSStackView crashes application

I want to use the NSStackView to stack views above each other, I also want them to de able to expand so I can't use the NSCollectionView if i understood it correctly.
So, in storyboard, I've created a NSStackView(embedded in scroll view) in the main view controller and a view controller that I want to fill it with:
The button will fill the stack view with ten views:
#IBOutlet weak var stackView: NSStackView!
#IBAction func redrawStackView(_ sender: Any) {
for i in 0..<10 {
let stackViewItemVC = storyboard?.instantiateController(withIdentifier: "StackViewItemVC") as! StackViewItemViewController
stackViewItemVC.id = i
stackView.addArrangedSubview(stackViewItemVC.view)
}
}
And the ViewController on the right simply looks like this:
class StackViewItemViewController: NSViewController {
var id: Int = -1
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
#IBAction func buttonPressed(_ sender: Any) {
debugPrint("StackViewItemViewController" + id.description + "pressed")
}
Running this small application works fine, every time I press the button ten more stack view items appears. But, when I have the audacity to press one of the buttons to the right the application crashes:
Where am I going wrong?
I have tried to work around the IBAction to verify that this what breaks, and the application will not crash if I subclass the button and make a "buttonDelegate" protocol with a function being called from mouseUp.
I guess the problem is that the viewController objects, which you create in the loop, are released immediately.
Even though the view is attached to the stackView, it's viewController is destroyed.
You can fix this issue by keeping a reference to each viewController.
You can do this by creating a new variable
var itemViewControllers = [StackViewItemViewController]()
and then add each newly created viewController to it:
itemViewController.append(stackViewItemVC)