Ios 自定义协议在swift中不起作用

Ios 自定义协议在swift中不起作用,ios,uiviewcontroller,swift,protocols,Ios,Uiviewcontroller,Swift,Protocols,我有两个UIViewControllers HomeViewController和ArrangeViewController 在ArrangeViewController中,我有以下代码 import UIKit protocol ArrangeClassProtocol { func recieveThearray(language : NSMutableArray) } class ArrangeViewController: UIViewController, UITableV

我有两个
UIViewController
s HomeViewController和ArrangeViewController

在ArrangeViewController中,我有以下代码

import UIKit

protocol ArrangeClassProtocol
{
    func recieveThearray(language : NSMutableArray)
}

class ArrangeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { // class "ArrangeViewController" has no initializer

    var ArrangeClassDelegateObject : ArrangeClassProtocol?

    // Global Variables Goes Here
    var languageNamesArray: NSMutableArray = ["Tamil","English"]
    var userDefaults : NSUserDefaults = NSUserDefaults.standardUserDefaults()
    var tempArray : NSMutableArray = NSMutableArray()

    // Outlets Goes Here
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Saving the Array in a UserDefaultObject
            if userDefaults.boolForKey("languageNamesArrayuserDefaults")
            {
                tempArray = userDefaults.objectForKey("languageNamesArrayuserDefaults") as NSMutableArray
            }
            else
            {
                tempArray = languageNamesArray
            }

        self.tableView.separatorInset = UIEdgeInsetsZero

         // TableView Reordering
        self.tableView.setEditing(true, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func prefersStatusBarHidden() -> Bool
    {
        return true
    }


    // Delegate Methods of the UITableView
     func numberOfSectionsInTableView(tableView: UITableView!) -> Int {

        return 1
    }

     func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {

            return tempArray.count
    }

     func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

        let cell = tableView.dequeueReusableCellWithIdentifier("Arrange", forIndexPath: indexPath) as ArrangeTableViewCell
        cell.languageName.font = UIFont(name: "Proxima Nova", size: 18)
        cell.languageName.text = tempArray.objectAtIndex(indexPath.row) as NSString
        cell.showsReorderControl = true
        return cell
    }

    // Delegate Methods for dragging the cell
     func tableView(tableView: UITableView!, editingStyleForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCellEditingStyle
    {

        return UITableViewCellEditingStyle.None
    }

     func tableView(tableView: UITableView!, canMoveRowAtIndexPath indexPath: NSIndexPath!) -> Bool
    {
        return true
    }

     func tableView(tableView: UITableView!, moveRowAtIndexPath sourceIndexPath: NSIndexPath!, toIndexPath destinationIndexPath: NSIndexPath!)
    {
        var stringToMove = tempArray.objectAtIndex(sourceIndexPath.row) as NSString
        tempArray .removeObjectAtIndex(sourceIndexPath.row)
        tempArray .insertObject(stringToMove, atIndex: destinationIndexPath.row)
    }

     func tableView(tableView: UITableView!, targetIndexPathForMoveFromRowAtIndexPath sourceIndexPath: NSIndexPath!, toProposedIndexPath proposedDestinationIndexPath: NSIndexPath!) -> NSIndexPath!
    {
        let section:AnyObject = tempArray .objectAtIndex(sourceIndexPath.section)
        var sectionCount = tempArray.count as NSInteger
        if sourceIndexPath.section != proposedDestinationIndexPath.section
        {
            var rowinSourceSection:NSInteger =  (sourceIndexPath.section > proposedDestinationIndexPath.section) ? 0 : (sectionCount-1)

            return NSIndexPath(forRow: rowinSourceSection, inSection: sourceIndexPath.row)


        }
        else if proposedDestinationIndexPath.row >= sectionCount
        {

            return NSIndexPath(forRow: (sectionCount-1), inSection: sourceIndexPath.row)
        }


        return proposedDestinationIndexPath
    }

    // Creating the HomeViewController Object and presenting the ViewController
    @IBAction func closeButtonClicked(sender: UIButton)
    {
        userDefaults.setObject(tempArray, forKey: "languageNamesArrayuserDefaults")
        userDefaults.synchronize()
        ArrangeClassDelegateObject?.recieveThearray(languageNamesArray)
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}
在HomeViewController中,我使用此代码

class HomeViewController: UIViewController, ArrangeClassProtocol {

   var ArrangeClassObject : ArrangeViewController = ArrangeViewController() // ArrangeViewController is Constructible with ()
  override func viewDidLoad() {
        super.viewDidLoad()

        self.ArrangeClassObject.ArrangeClassDelegateObject = self
}

func recieveThearray(language: NSMutableArray)
    {
        println(language)
    }
}
我想访问正在从ArrangeViewController传递的阵列。但它显示了错误,我在声明附近注释掉了。我还将可选值用于HomeViewController,它还显示错误并使应用程序崩溃。请有人帮我弄清楚这件事


我是通过github的帖子得到这个主意的。在那个项目中,他使用了一个
UIViewController
和另一个swift类。这对我来说也是可能的。但是我想用这两个
UIViewController
s解决这个问题

谢谢你的新代码。创建错误消息的问题如下:

@IBOutlet weak var tableView: UITableView!
如果将此代码更改为:

@IBOutlet weak var tableView: UITableView?
然后编译器将停止抱怨缺少初始化器

然而,这更多的是一个诊断工具,而不是解决问题的实际方法,即您需要一个初始化器。我认为发生的事情是,直到您在层次结构中自己的类级别添加未初始化的属性,您将获得一个默认的初始化器。在添加未初始化属性时,Swift将停止生成默认初始化器,并希望您提供自己的初始化器

如果您检查UIViewController的标题,您会发现以下建议:

/*
  The designated initializer. If you subclass UIViewController, you must call the super implementation of this
  method, even if you aren't using a NIB.  (As a convenience, the default init method will do this for you,
  and specify nil for both of this methods arguments.) In the specified NIB, the File's Owner proxy should
  have its class set to your view controller subclass, with the view outlet connected to the main view. If you
  invoke this method with a nil nib name, then this class' -loadView method will attempt to load a NIB whose
  name is the same as your view controller's class. If no such NIB in fact exists then you must either call
  -setView: before -view is invoked, or override the -loadView method to set up your views programatically.
*/
init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!)
因此,不妨试试:

init()  {
    super.init(nibName: nil, bundle: nil)
}

…这将恢复最初提供的初始化器的功能。我猜基类的初始化器将从情节提要中正确地“预热”您的IBOutlet,并且您应该能够再次使用无参数初始化器构造视图控制器。

我无法重现您的“类”ArrangeViewController“没有初始化器”错误。你是最新的测试版,测试版4吗?你还有别的房子吗?当您单击错误时,您是否获得有关哪个属性导致错误的更多信息(如果我通过添加未初始化的属性强制您出错,我会得到一个“修复它”提示,告诉我哪个属性是问题所在。)您是否可以发布一个完整、简短的示例来重现您的错误?(另外,第二个错误只是第一个错误的副作用,假设错误消息实际为“ArrangeViewController不能用()构造”……这是因为Swift找不到有效的初始值设定项,因为ArrangeViewController已被导致第一个错误的任何原因破坏。)@MattGibson没有其他选项可以查看错误。xcode oly显示这两个错误。我不知道该怎么做。我可以初始化一些东西吗?@MattGibson我正在使用xcode 6-beta4@MattGibson请把这个问题标出来,好让每个人都能看到。我真的很想解决这个问题。我已经更新了我的答案。我可能出了点问题就我所知,视图控制器的初始化是如何工作的。我会等到其他一些人有机会检查这个问题并获得更多的意见。我已经对你的问题进行了投票,现在你已经发布了足够的代码来重现这个问题。