Ich habe die folgende Klasse unten. Die Idee ist, dass ein benutzerdefinierter Progress Window View Controller verwendet wird, um den Fortschritt verschiedener Ereignisse zu verarbeiten. Das Problem ist, da dies in einer Klasse und nicht ein View-Controller ist es selbst, ich bin mir nicht sicher, wie man das progressWindow tatsächlich zeigen, nachdem ich es aus dem Storyboard instanziieren?Wie kann der View Controller sich selbst darstellen?
Wie mache ich das? Momentan bekomme ich einen Fehler, dass die Anwendung versucht hat, den Model-View-Controller auf sich selbst zu präsentieren.
import Foundation
import UIKit
class StatusProgress{
static var cancelCode = {}
static var runCode = {}
static var theProgressWindowController = ProgressWindowViewController()
static var returningViewControllerIdentifier = ""
static let storyboard = UIStoryboard(name: "Main", bundle: nil)
static func run(){
// This will run in parralel but on main queue. Has to be on this Queue because it might involve UI
dispatch_async(dispatch_get_main_queue(), {
// Update the UI on the main thread.
StatusProgress.runCode()
});
}
static func cancel(){
dispatch_async(dispatch_get_main_queue(), {
StatusProgress.cancelCode()
dispatch_sync(dispatch_get_main_queue(),{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier(returningViewControllerIdentifier)
vc.presentViewController(vc, animated: true, completion: nil)
})
});
}
static func show(){
dispatch_async(dispatch_get_main_queue(),{
theProgressWindowController = self.storyboard.instantiateViewControllerWithIdentifier("progressWindow") as! ProgressWindowViewController
theProgressWindowController.presentViewController(theProgressWindowController, animated: true, completion: nil) //use own instance to show it's self? (throws error! application tried to present modal view controller on itself. Presenting controller is <Inventory_Counter.ProgressWindowViewController: 0x1466ea390>.')
})
}
}
Mein Problem ist im Wesentlichen brauche ich einen Ersatz für diese Codezeile.
theProgressWindowController.presentViewController(theProgressWindowController, animated: true, completion: nil)
ich hier vergessen zu erwähnen ist der Code, der es in einem anderen View-Controller läuft.
SyncViewController.swift
import UIKit
class SyncViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func yesSyncButtonAction(sender: UIButton) {
StatusProgress.returningViewControllerIdentifier = "syncWindow"
StatusProgress.runCode = {
print("run code test")
}
StatusProgress.cancelCode = {
print("cancel code test")
}
StatusProgress.show()
}
@IBAction func noSyncActionButton(sender: UIButton) {
tabBarController?.selectedIndex = 1 //assume back to inventory section
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Wie würde ich die Delegiertenmeldungen senden? Das ist eine interessante Idee und ich mag sie. Kannst du mit Beispiel wenn möglich updaten? –
Dies ist ein super einfaches iOS Designmuster. Wenn Sie jemals Tabellenansichten oder Auflistungsansichten verwendet haben, haben Sie dieses Muster bereits verwendet. Sie senden die Delegat-Nachrichten, indem Sie die Funktionen aufrufen, die in dem Protokoll für das Delegat-Objekt aufgelistet werden. Sie können weitere Informationen hier finden: https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html –
Ich bin mir nicht sicher, warum das so schwer für mich ist, herauszufinden . Ich bekomme Delegierte aber nicht sicher, wie man es in dieser Situation benutzt. Ich werde es seit 8 Stunden immer wieder versuchen :). Ich werde meine Antwort posten, wenn es funktioniert und akzeptiere das bald. –