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

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
}
}

Related

How to change NSViewController Tab Programmatically on macOS applications

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

Keyboard does not open when clicking on UITextfield

I create sign in page with swift programmatically coding. I tried simulator and device but keyboard did not opened when clicked on textfied.
when I write code in viewController with stroyboard, keyboard had opened. But, without using storyboard, keyboard is not opening.
Did you set the TextField's delegate? In the Storyboard you must have, but in code that is sometimes forgotten.
class ViewController: UIViewController, UITextFieldDelegate {
let textField = UITextField()
func viewDidLoad() {
super.viewDidLoad()
textfield.delegate = self // Did you do this?
}
}
If you are using simulator then you must go to I/O -> Keyboard -> Toggle software keyboard.
If you are using physical device then through code you need to set as as first responser as below:
textfieldName.becomeFirstResponder()

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.

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)

Menubar with Storyboard - validateMenuItem not get called

I'm trying to setup a menubar Application using storyboard but my validateMenuItem method not get called.
I will try to explain what i did.
First i dragged a Menu Item in my Application Scene. Then one Object for my MenuController. Created a MenuController (MenuController.swift) and filled it with code. Back in my storyboard I set my Menu delegate to MenuController and MenuController Outlet to Menu. (I'm not totally sure whether i have set the delegates correctly.)
When i start the app, the menu icon appears and the first item title is set to test. But when i'm clicking the icon the validateMenuItem method not get called.
MenuController.swift
import Cocoa
class MenuController: NSObject {
var statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
#IBOutlet weak var statusMenu: NSMenu!
#IBOutlet weak var item1: NSMenuItem!
override func awakeFromNib() {
print("awakeFromNib")
self.item1.title = "Test"
let icon = NSImage(named: "menubarIcon")
statusItem.image = icon
statusItem.menu = statusMenu
}
override func validateMenuItem(menuItem: NSMenuItem) -> Bool {
print("validateMenuItem")
return true
}
}
Storyboard Menu Delegates
(source: picr.de)
Storyboard MenuController Delegates
(source: picr.de)
Has anybody an idea?
Greets from Austria!
The menu/UI validation mechanism does not query the menu's delegate but uses the item's target to determine the enabled state instead.
If the target is not explicitly set, it walks the responder chain.
To get basic validation, you have to make sure that the following things are setup:
"Auto Enables Items" is checked in Interface Builder (on by default)
You control-dragged the menu's action to the first responder
The menu's action is implemented in a class that is part of the responder chain (e.g. a view controller that manages a set of actions for your UI)
A basic implementation of validateUserInterfaceItem: could look like the following for an item with an action selector called test:
func validateUserInterfaceItem(anItem: NSValidatedUserInterfaceItem) -> Bool
{
if anItem.action() == Selector("test:") {
print("validating item \(anItem)")
return true
}
return true
}