I am writing a macOS Cocoa application with Swift and I don't know how to prevent, that the main window shows up on application startup. I want to check first, if there is a new version available on the server: if there is one, a spash screen should appear. I don't know how to accomplish this and couldn't find good information too. I also don't know how to get rid of the windows standard buttons like for closing the window. Any help?
Check settings of your main window in Xcode. There are some checkboxes which control window buttons and other settings in Attributes inspector.
You can disable or enable them programmatically when you accessing Window object.
For example you can implement this approach in WindowController like this:
class WindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
window!.styleMask &= ~NSClosableWindowMask
}
}
Previously you need to add your custom WindowController class to window controller object in storyboard (Identity inspector tab).
Related
I have created this personal project that is a movie mock up called Movly, I have mainly worked within storyboard and have yet to write any code to the app. There is multiple screens to the app accessible by the push of a button, but i am new to what kind of functions i can add in order to make my app an app, rather than just UI
In order to be able to programatically control the view controllers you will need to first create a custom view controller. You can do this by going to File > New > File > Cocoa Touch Class, you can name it whatever you want. Then go to the view controller in storyboard. Press the button 'Show the Identity Inspector' (the rextangle) and then you will see a section labelled custom class. By changing the drop down box next to Class and selecting the view controller you just made you now have access to the view controller programatically.
If you want to control things on the screen I recommend researching #IBOutlet and #IBAction (sounds complicated but it is quite simple really) and then you can write functions when textFields are changed/ buttons are pressed.
In order to write a function you need to use the following layout
func functionName() {
}
If you want to take in some kind of data you can put what it is in the bracket, e.g.
func functionName(name: String) {
}
and if you want to return something from the function you can use
func functionName() -> String {
}
When you want to call the function you just do functionName()
I am trying to create a customized dock menu for my swift application in the dock so that when the user right click on the dock tile of my app after it has been launched, it will show my customized item in addition to the default dock menu items. I prefer to do it programmatically or creating the (static) menu item using Xcode.
I have reviewed similar questions posted here (How can I add a menu to the application in the dock?) and (Adding Items to the Dock Menu from my View Controller in my Cocoa App) but both referred to the old nib interface instead of how to create one using storyboard. I also reviewed the file template library in storyboard but couldn't find a template for dock menu (I only see one for Main Menu).
Any pointer on how to achieve this using storyboard or programmatically with swift will be much appreciated.
EDIT 4/24/2020: Since Ken's response, I have decided to customize the dock menu programmatically. Here is how I implemented applicationDockMenu(_:) in AppDelegate
EDIT #2 4/24/2020: fixed missing _ input argument to method and problem solved.
func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {
let myMenu = NSMenu(title: "MyMenu")
let myMenuItem = NSMenuItem(title: "My Item 1", action: nil, keyEquivalent: "")
myMenu.addItem(myMenuItem)
return myMenu
}
I must be missing something else since the new item did not show up with I right-clicked on the app's dock icon after the app is launched.
Any idea?
Thanks.
Kenny
You can implement the method applicationDockMenu(_:) in your app delegate class to return a menu of items to be added to the Dock menu.
If you check the docs for that method, it also discusses other methods for providing a static Dock menu. One of them involves creating a separate NIB. If there isn't a template for a menu NIB, you can simply create an empty one and add a menu object to it. That NIB is referenced by a key in the Info.plist file and doesn't need to involve your storyboard.
I have made a pretty simple mac app with two windows. I am an iOS developer so I am very familiar with swift but a few of the UI elements in iOS apps don’t translate well when building OS X apps. I want to take advantage of the menu bar at the top of the screen. I want to click “New” under file which is there by default and open a new window of my initial VC. How do I do that?
I have read a bunch of posts and they have told me to build a new menu bar but I feel like there should be an easier way with the menu bar that is there by default. How do I call a specific VC even once I have managed to create an outlet or add an action for the new button? Can I just instantiate the VC like we would in iOS? I just want the easiest way to do this.
This is what I used to present the new window:
let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil)
let myWindowController = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "LoginVC")) as! LoginViewController
if let window = NSApplication.shared.mainWindow
window.contentViewController = myWindowController //as! NSWindowController // just swap
}
By default the "New" menu item will send the newDocument(_:) message to the "first responder". If any object along the responder chain implements it, then the menu item will be enabled, otherwise the menu item is disabled.
To respond to this message you could either:
implement func newDocument(_ sender: Any?) somewhere in the responder chain (probably somewhere towards the end of it, like in the app delegate). In a document-based application the NSDocumentController would already handle this.
set your own action and target for the "New" menu item to call another method on a specific object to have it create and display the new window. This would not work for actions that are contextual based on where in the application the user is (for example Copy or Paste).
I am in Xcode 6 and the view controller code was showing up under the automatic preview: now it is gone.
You cannot hook up the text fields under manual when selected. Where did it go, and how do I get it back? I have to keep deleting the VC and making new ones. is this a problem with Swift? I don't won't to have to keep deleting 10 VC and redoing them to code.
It's not a problem with Swift; based on your answer, this may not be your problem, but you may not have your view controller file connected to your view in the storyboard (I am led to believe this by the fact that you have had to delete and re-add the file over and over again).
First, make sure that you've selected the view:
Then, open the Inspector menu and go to the Identity section:
Under "Custom Class," make sure that "Class" is the name of your class in your VC file (see below): it should auto-complete as you type it in.
Then, in your view controller file, make sure that you have defined the class name properly, like this:
class ViewController: UIViewController {
// VC code
}
This is not the answer but i had to type the #IBOutlet var firstNameTextField: UITextField! into the code under the manual preview instead of deleting the VC.swift. I then added a button and typed the #IBAction func savePointOfContactToDefault(sender: AnyObject){} and drag it to the button and the automatic preview now shows the code and you can now drag text, label, ect to the code. Ya seems to be a workaround.
I'm trying to create a LSUIElement app that behaves like Spotlight, CoverSutra and other apps of that type. I managed to create a custom NSStatusItem, which popups up an NSWindow but the problem is that the app that currently has focus will the focus to my custom NSWindow.
I've based myself on Matt Gemmell's example (http://mattgemmell.com/2008/03/04/using-maattachedwindow-with-an-nsstatusitem)
For example, if you're in Safari and click on the Spotlight icon, the current Safari window does not gray out and keeps focused. When you press ESC in Spotlight, the focus is back to the Safari window.
I haven't managed to do this with my custom NSWindow. I have to click back on a window to set focus back to it.
So I'd like to know which route to go to achieve this. Is the solution a NSWindow, NSPanel, NSMenu with a custom NSMenuItem?
Use NSPanel and NSNonactivatingPanelMask as styleMask or tick the option in IB.