自定义协议无法在swift中运行

时间:2014-07-30 07:40:27

标签: ios uiviewcontroller swift protocols

我有两个UIViewController的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传递的Array。但它显示出我在评论附近评论的错误。我还使用了HomeViewController的可选值,它还显示错误并使应用程序崩溃。请有人帮忙解决这个问题。

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

1 个答案:

答案 0 :(得分:1)

感谢您使用新代码。创建错误消息的问题在于:

@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会被基类的初始化者从Storyboard中“正确地”加热,你应该再次能够使用无参数化的初始化器构建View Controller。