【发布时间】:2014-07-30 07:40:27
【问题描述】:
我有两个UIViewControllers 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 类。这对我来说也是可能的。但我想用这两个UIViewControllers 解决它。
【问题讨论】:
-
我无法重现您的“类“ArrangeViewController”没有初始化程序”错误。你在最新的测试版,测试版 4 上吗?你还有其他房产吗?当您单击错误时,您是否获得有关哪个属性导致错误的更多信息(如果我通过添加未初始化的属性来强制您的错误,我会收到“修复它”的提示,告诉我哪个属性是问题。)您能否发布一个完整的简短示例来重现您的错误?
-
(另外,您的第二个错误只是您的第一个错误的副作用,假设您的错误消息实际上是“ArrangeViewController is not Constructible with ()”......那是因为Swift 找不到有效的初始化程序,因为您的 ArrangeViewController 已被导致第一个错误的任何原因破坏。)
-
@MattGibson 没有其他选项可以查看错误。 xcode 仅显示这两个错误。我不知道该怎么做。我可以初始化一些东西吗??
-
@MattGibson 我正在使用 xcode6-beta4
-
@MattGibson 请标记这个问题吗?让大家看到。。我很想解决这个问题。
标签: ios uiviewcontroller swift protocols