(macOS, Swift, storyboards)How can I have two windows open from the beginning? (without clicking any button)What I have tried:1- CREATE A NEW WINDOW- Main.storyboard I have a Window Controller from the library- I have created a New document Cocoa Class SecondViewController- Main.storyboard > I select View Controller. In Identity Inspector I select Class SecondViewController2- MAKE IT LAUNCH FROM THE BEGINNING- AppDelegate.swift inside applicationDidFinishLaunching: let storyboard = NSStoryboard(name: "Main", bundle: nil) let windowController = storyboard.instantiateController(withIdentifier: "MySecondController") as! NSWindowController windowController.showWindow(self)- I think my problem is how to identify MySecondController in Main.storyboardI tried to select Window inside Window Controller. Then go to Identity Inspector and Identifier. But when I run, it does not appear. How should I identify that second window, where exactly should I put MySecondController name? Or perhaps I have not created well the second window?
Post
Replies
Boosts
Views
Activity
(macOS, Swift, storyboards)Can I change a label on hover?WHAT I HAVE TRIED1- I have put a button in storyboard and make it transparent. It works if I click, but I could nof find how to if hover@IBOutlet weak var label1: NSTextField!@IBAction func button1(_ sender: NSButton) { label1.textColor = NSColor.gray}2- I have put a NSView and drag a NSClickGestureRecognizer to it. Again, it works on click but I cannot find a way to do it with hover@IBOutlet weak var label2: NSTextField!@IBAction func gestureRecognizer1(_ sender: NSClickGestureRecognizer) { label2.textColor = NSColor.gray}I could find something in Apple documentation. But it talks about UIButton. I cannot find a way to adapt to my case:https://developer.apple.com/documentation/uikit/uihovergesturerecognizer
Swift, Storyboards, Core DataI have this code that makes an insert with Core Data. How would it be read, update and delete in this specific case?I have seen a lot of tutorials and each one makes things a bit different. I could not adapt to this case.(I have already created the data model with the entity Users and the Attrituves name and password. I also have linked the buttons read, update and delete. I am looking for the most bàsic, without tables... Just like the insert I have here. Thank you!)import UIKit
import CoreData
class ViewController: UIViewController {
@IBOutlet weak var name: UITextField!
@IBOutlet weak var password: UITextField!
lazy var context : NSManagedObjectContext = {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}()
// INSERT
@IBAction func button(_ sender: UIButton) {
guard name.hasText && password.hasText else {
return
}
let user = Users(context: context)
user.name = self.name.text!
user.password = self.password.text!
do {
try context.save()
print ("saved")
} catch {
print(error)
}
}
// READ
@IBAction func readButton(_ sender: UIButton) {
}
// UPDATE
@IBAction func updateButton(_ sender: UIButton) {
}
// DELETE
@IBAction func deleteButton(_ sender: UIButton) {
}
}
What is the right way to insert or write today's date in Core Data?in .xcdatamodeld I have the Entity Contingut and the Attributes:dateCreated of Type Datetext1 of Type String func today1() {
let date = Date()
let calendar = Calendar.current
let day = calendar.component(.day, from: date)
let month = calendar.component(.month, from: date)
let year = calendar.component(.year, from: date)
let avui = "\(year)-\(month)-\(day)"
print (avui)
}
// CREATE
@IBAction func createData(_ sender: UIButton) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let contingutEntity = NSEntityDescription.entity(forEntityName: "Contingut", in: managedContext)!
let contingut = NSManagedObject(entity: contingutEntity, insertInto: managedContext)
contingut.setValue(today1(), forKey: "dateCreated")
contingut.setValue("some text", forKey: "text1")
do {
try managedContext.save()
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}When I create a new text1, I want to register the day. What is the right way to do that?
I update one field in Core Data. This code works well:func updateData(){
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest:NSFetchRequest = NSFetchRequest.init(entityName: "User")
fetchRequest.predicate = NSPredicate(format: "username = %@", "John")
do {
let test = try managedContext.fetch(fetchRequest)
let objectUpdate = test[0] as! NSManagedObject
objectUpdate.setValue("John2", forKey: "username")
objectUpdate.setValue("John2@example.com", forKey: "email")
objectUpdate.setValue("newPassword", forKey: "password")
do {
try managedContext.save()
}
catch {
print(error)
}
} catch {
print(error)
}
}I would like to add a way to check if what I try to update exists. In that specific case, I want to check first if I have in the database the username John
macOS, Swift, StoryboardsI have two windows. I have opened the second window with a Storyboard Segue Kind Sheet. I want to close that window with a fade out. I put that in SecondViewController.swift. This works well to make the fade out. But, if I understand, the window is not visible but it is still there and I cannot click any button behind. How to solve that?(If I use dismiss(self) I close the window but without the fade out. I want fade out, and then close) @IBAction func close1(_ sender: NSButton) { self.view.window?.animationBehavior = .none self.view.window?.makeKeyAndOrderFront(nil) NSAnimationContext.runAnimationGroup({ (context) -> Void in context.duration = 2 self.view.window?.animator().alphaValue = 0 }, completionHandler: nil) // dismiss(self) }
macOS, Storyboards, Swift.I have a button to perform a segue to a second page. I can make a control drag from the button to the second page. In Attributes, I have selected a Kind Sheet. Is it possible to open it with a fade in?To make things easy let's say that:The first window is ViewController.swiftThe second window is SecondViewControllerI have button1 in ViewController.swift with segue1 that goes to SecondViewController.swift
macOS, Swift, storyboardsI have a label with minutes and seconds of a countdown. 00:00I want it horizontally centered in the container and at a certain distance from the top. When I put those constraints, all the label moves when there is a new second as it tries to center: 00:01, 00:02... it creates and strange effect. I suppose that the correct behaviour would be to have the two points fixed in the center. How to use constraints in this case?
Is it possible to have a small WebView? for instance 200 x 250I have a Webkit View from the LibraryIn the ViewController: import Cocoa
import WebKit
class ViewController: NSViewController {
@IBOutlet weak var webView1: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.apple.com")
let request = URLRequest(url:url!)
webView1.load(request)
}I can see the content of the web site but it is cut. I do not need a full web browser only an image representation of the site.
macOS, SwiftIs it possible to remove the scroll in WebKit View? (or make it not visible)I have a WebKit View from library and I have made the connection to the ViewController.swift:@IBOutlet weak var webView1: WKWebView!
When I Run a project I see nothing, the window does not appear. I see no error. I see the "Build succeed" and the general menu of the bar, but no window.Even when I create a new project, with no code, sometimes I see the window, sometimes I do not. When I see the window, if I add some code, just a few lines, then run, and I cannot see the window anymore. I tried many things and I do not see a pattern, it seems random. I tried to give a different name, use the shortcut, use the button, I am sure that is not hidden behind or something, I compare a project that works and one that does not and I see no difference ...I can do no work and I have no idea what's going on. Any suggestion?macOS Catalina 10.15.5Xcode 11.5Swift
macOS, Swift, User Interface: Storyboard
I have two pages: ViewController and Preferences. I have a button on the Preferences window and I want to trigger an action on the ViewController window. How can I do that?
The button is in Preferences window and the action should be in the ViewController window.
(In storyboard, If I open the Assistant Editor I get Preferences.swift. I cannot make the regular connection with the control drag)
macOS, Swift, storyboards
How can I make the window active in all spaces?
I could find how to do that with storyboard: Select the window > Attributes > Spaces > Can join all spaces. How can I do the same with code (activate and de-activate)
I have an NSView and I want it to be half of the window.
This is what I have tried: In storyboard I dragged a Custom View
I give it a Constraint of 0 in Top, Left and Bottom
How can I make it half of the screen even when the user changes the size of the screen?
@IBOutlet weak var nsView1: NSView!
nsView1.wantsLayer = true
nsView1.layer?.backgroundColor = NSColor.red.cgColor
How to make a link when I click to a button in macOS?
This works well in iOS. How to do the same with macOS:
@IBAction func button1(_ sender: UIButton) {
if let url = URL(string: "some link") {
UIApplication.shared.open(url)
}
}