Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 视图控制器如何显示它';她是谁?_Ios_Swift_Class_Uiviewcontroller_Swift2 - Fatal编程技术网

Ios 视图控制器如何显示它';她是谁?

Ios 视图控制器如何显示它';她是谁?,ios,swift,class,uiviewcontroller,swift2,Ios,Swift,Class,Uiviewcontroller,Swift2,我有下面的课程。其想法是,它将使用自定义进度窗口视图控制器来处理各种不同事件的进度。问题是,因为这是在一个类中,而不是在一个视图控制器中——它是自己的,所以我不确定如何在从故事板实例化progressWindow之后使它真正显示出来 我该怎么做?目前我收到一个错误,应用程序试图在自身上显示模型视图控制器 import Foundation import UIKit class StatusProgress{ static var cancelCode = {} static v

我有下面的课程。其想法是,它将使用自定义进度窗口视图控制器来处理各种不同事件的进度。问题是,因为这是在一个类中,而不是在一个视图控制器中——它是自己的,所以我不确定如何在从故事板实例化progressWindow之后使它真正显示出来

我该怎么做?目前我收到一个错误,应用程序试图在自身上显示模型视图控制器

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>.')
        })
    }
}
我忘了在这里提到的是在另一个视图控制器中运行它的代码

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.
    }
    */

}

如果视图生命周期事件对您不太重要,您可以将进度控制器的视图添加到当前控制器的视图中。或者,如果在
show()
函数中提供UIView参数,效果会更好

static func show(attachToView: UIView ){
        dispatch_async(dispatch_get_main_queue(),{
            theProgressWindowController = self.storyboard.instantiateViewControllerWithIdentifier("progressWindow") as! ProgressWindowViewController
             attachToView.addSubview(theProgressWindowController.view)
        })
    }
毕竟,您最好从superview中删除进度视图

   static func cancel(){
            dispatch_async(dispatch_get_main_queue(),{
                theProgressWindowController = self.storyboard.instantiateViewControllerWithIdentifier("progressWindow") as! ProgressWindowViewController
                 theProgressWindowController.view.removeFromSuperview()
            })
        }

如果视图生命周期事件对您不太重要,您可以将进度控制器的视图添加到当前控制器的视图中。或者,如果在
show()
函数中提供UIView参数,效果会更好

static func show(attachToView: UIView ){
        dispatch_async(dispatch_get_main_queue(),{
            theProgressWindowController = self.storyboard.instantiateViewControllerWithIdentifier("progressWindow") as! ProgressWindowViewController
             attachToView.addSubview(theProgressWindowController.view)
        })
    }
毕竟,您最好从superview中删除进度视图

   static func cancel(){
            dispatch_async(dispatch_get_main_queue(),{
                theProgressWindowController = self.storyboard.instantiateViewControllerWithIdentifier("progressWindow") as! ProgressWindowViewController
                 theProgressWindowController.view.removeFromSuperview()
            })
        }

最大的问题是,
StatusProgress
类正在实例化并显示视图控制器。视图控制器应实例化并显示其他视图控制器,而模型对象不应。因此,您需要将显示新视图控制器的逻辑移动到您的
SyncViewController
。然后使用委派与
SyncViewController
通信同步已完成

protocol StatusProgressDelegate {
  func statusProgress(status: StatusProgress, shouldShow: Bool)
  func statusProgress(status: StatusProgress, shouldCancel: Bool)
}

您的
StatusProgress
对象将有一个符合该协议的委托,并在其show和cancel方法中调用该委托。这意味着您需要使静态函数成为实例方法,并为类编写初始值设定项,以便可以实例化它

最大的问题是您的
StatusProgress
类正在实例化并显示视图控制器。视图控制器应实例化并显示其他视图控制器,而模型对象不应。因此,您需要将显示新视图控制器的逻辑移动到您的
SyncViewController
。然后使用委派与
SyncViewController
通信同步已完成

protocol StatusProgressDelegate {
  func statusProgress(status: StatusProgress, shouldShow: Bool)
  func statusProgress(status: StatusProgress, shouldCancel: Bool)
}

您的
StatusProgress
对象将有一个符合该协议的委托,并在其show和cancel方法中调用该委托。这意味着您需要使静态函数成为实例方法,并为类编写初始值设定项,以便可以实例化它

这也是一个有趣的策略。你认为钱德勒的上述答案是更好的heximal方法,还是你描述的方法仍然很好?正如你提到的,你的进度控制不是一个很好的控制器,所以是的,我认为我的方法是可行的。我甚至可以用同样的策略指向类似的控制。这是一个有趣的战术。你认为钱德勒的上述答案是更好的heximal方法,还是你描述的方法仍然很好?正如你提到的,你的进度控制不是一个很好的控制器,所以是的,我认为我的方法是可行的。我甚至可以用同样的策略指向类似的控制。这是我如何发送代理消息的问题?这是一个有趣的想法,我喜欢它。如果可能的话,你能用例子更新吗?这是一个超级基本的iOS设计模式。如果您曾经使用过表视图或集合视图,那么您已经使用过此模式。通过调用委托对象上协议中列出的函数来发送委托消息。你可以在这里找到更多的信息:我不知道为什么这对我来说如此困难。我得到了代表,但不确定在这种情况下如何使用它。我会继续尝试,已经尝试了8个小时:)。如果有效的话,我会发布我的答案,并很快接受。好的,委托方法差不多完成了,这就是我想到的(),但是我遇到了另一个问题,有什么想法吗?我该如何发送委托消息?这是一个有趣的想法,我喜欢它。如果可能的话,你能用例子更新吗?这是一个超级基本的iOS设计模式。如果您曾经使用过表视图或集合视图,那么您已经使用过此模式。通过调用委托对象上协议中列出的函数来发送委托消息。你可以在这里找到更多的信息:我不知道为什么这对我来说如此困难。我得到了代表,但不确定在这种情况下如何使用它。我会继续尝试,已经尝试了8个小时:)。我会发布我的答案,如果它有效的话,我会很快接受它。好的,委托方法差不多完成了,这就是我想到的(),但是我遇到了另一个问题,有什么想法吗?