我正在 Xcode 9.2 上创建一个 macOS 应用,我无法弄清楚为什么在viewDidLoad()
创建视图层次结构后,我可以' t再次使用IBOutlet
引用 StoryBoard 中的视图。我发现了类似的问题,当我在viewDidLoad()
时,他们建议将我要更新的视图保存为变量,以便我以后可以使用它;如果是这样,使用IBOutlet
有什么好处?是否有人可以解释如何在这种情况下更新PDFView
viewDidLoad()
以外import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
// MARK: - Properties
var filePath: URL?
var result: Int?
var fileObject: Data?
// MARK: - Outlets
@IBOutlet weak var openFileMenuItem: NSMenuItem!
// MARK: - App Life Cycle
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
// MARK: - My Functions
@IBAction func openFile(_ sender: NSMenuItem) {
//Get the window of the app
var window = NSApplication.shared.mainWindow!
// Create NSOpenPanel and setting properties
let panel = NSOpenPanel()
panel.title = "Select file"
panel.allowsMultipleSelection = false;
panel.canChooseDirectories = false;
panel.canCreateDirectories = false;
panel.canChooseFiles = true;
// Filtering file extension
let fileTypes: [String] = ["pdf"]
panel.allowedFileTypes = fileTypes
// Showing OpenPanel to the user for selecting the file
panel.beginSheetModal(for: window) {
(result) in
if result.rawValue == NSFileHandlingPanelOKButton {
// Getting url of the file
self.filePath = panel.urls[0]
print(self.filePath)
// Creating Data Object of the file for creating later the PDFDocument in the controller
do {
if let fileData = self.filePath {
self.fileObject = try Data.init(contentsOf: self.filePath!)
print("\(self.fileObject) ")
}
} catch let error as NSError {
print("Error with file Data Object: \(error)")
}
// Getting the mainViewController
let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil)
let mainController = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "mainViewControllerID")) as! MainViewController
// Appdelegate call function of mainViewController to update the content of PDFView
mainController.showPdfDocument(fileData: self.fileObject!)
}
}
}
}
的内容而不会获得nil?
AppDelegate
import Foundation
import Quartz
class MainViewController: NSViewController {
// MARK: - Property
var pdfdocument: PDFDocument?
// MARK: - Outlets
@IBOutlet var mainView: NSView!
@IBOutlet var pdfView: PDFView!
// MARK: - App Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
// MARK: - My Functions
func showPdfDocument(fileData: Data) {
print("appdelegate calling showPdfDocument on mainViewController " )
pdfdocument = PDFDocument(data: fileData)
pdfView.document = pdfdocument
}
}
MainViewController:
$stmt = $db->prepare("INSERT INTO person(name, lastname) VALUES (?, ?)");
$stmt->bind_param("ss", $name, $lastname);
$name= '?';
$lastname='?';
$stmt->execute();
我遇到的错误:
答案 0 :(得分:0)
我用AppDelegate
中的当前代码解决了问题。基本上我使用contentViewController
来更新PDFView
但我不知道这是否是一个好习惯,因为我应该用自己的控制器更新PDFView
而不是更新窗户。
:
let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil)
let mainViewController = NSApplication.shared.mainWindow?.contentViewController as! MainViewController
mainViewController.showPdfDocument(fileData: self.fileObject!)
通过此链接(https://developer.apple.com/documentation/appkit/nswindow/1419615-contentviewcontroller)阅读Apple文档,它说“直接分配contentView值会清除根视图控制器。”。我应该以编程方式将mainView分配给contentView以自动将我的mainViewController设置为初始控制器吗?
我还发现我可以将我的控制器设置为IB的初始控制器,您可以从此图像中看到:
我想做的理想事情是将contentViewController作为初始viewController(IB中的默认设置),然后实例化我的mainViewController以更新pdfView。问题是,通过实例化,新的viewController具有nil的所有内容(IBOutlet,vars等...)。是从contentViewController到mainViewController的向下转换是实现这个任务的好方法吗?