我正在尝试从我的UiView初始化视图控制器。但我收到错误的行self.presentViewController(vc,动画:true,完成:nil)。我试图在单击表格行后显示另一个视图控制器。我已经初始化了故事板。
import UIKit
class CustomSwipeOut: UIView , UITableViewDataSource , UITableViewDelegate {
var label: UILabel = UILabel()
var myNames = ["item1","item2","item3"]
override init(frame: CGRect) {
super.init(frame: frame)
self.addCustomView()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addCustomView()
{
//add blank subview to the view
var blankView : UIView = UIView(frame: CGRectMake(0, 0, 300, 100))
blankView.backgroundColor = UIColor.greenColor()
self.addSubview(blankView)
//creating a tableview programmatically
var tblView : UITableView = UITableView()
tblView.frame = CGRectMake(0, 100, 300, 200)
self.addSubview(tblView)
tblView.delegate = self
tblView.dataSource = self
tblView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myCell")
}
//pragma mark- table view data source methods
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell") as UITableViewCell
cell.textLabel?.text = self.myNames[indexPath.row]
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.myNames.count
}
//pragma mark - table view delegate methods
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
switch indexPath.row {
case 0:
println("index o clicked")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("MoneySum") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
case 1:
println("index 1 clicked")
case 2:
println("index 2 clicked")
default:
println("no index")
}
}
}
答案 0 :(得分:1)
presentViewController:animated:completion:
是仅为UIViewController
而非UIView
(see Apple doc here)定义的方法。
您的UIView
可能位于由UIViewController
管理的视图层次结构中
解决方案是使用对该父UIViewController
的引用并在其上调用presentViewController:animated:completion:
。
答案 1 :(得分:1)
然而..我使用委托来完成从UIView传递id到UIViewController并检查id我实例化了ViewController来自UIViewController类,Para在他的回答中说。我这样做了如下
<强>步骤:强>
1)首先创建一个协议
2)创建符合协议的变量委托
3)然后创建一个回叫方法。
//Step1:
protocol SendIndexDelegate{
func sendIndex(Int);
}
class CustomSwipeView: UIView , UITableViewDataSource , UITableViewDelegate {
var delegate : SendIndexDelegate? //Step2
override init(frame: CGRect) {
super.init(frame: frame)
self.addCustomView()
}
......
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var row = indexPath.row
//for optional binding
if let temp = self.delegate {
delegate?.sendIndex(row) //Step 3
}else{
println("optional value contains nill value")
}
}
}
继续为另一个班级执行的步骤:
4)符合协议SendIndexDelegate(所以方法sendIndex(Int)必须由此类强制执行)
5)在可选变量delegate中为变量委托分配值self(它表示我将充当Class CustomSwipeView的委托并实现方法sendIndex(Int))
6)现在实现该方法并向其添加主体(因为它已被委托,因此必须通过回调方法处理上述类的操作)
class RootViewController: UIViewController,SendIndexDelegate //Step4 {
let rect: CGRect = CGRect (x: self.view.frame.size.width, y :10 , width: self.view.frame.size.width-50, height: self.view.frame.size.height-10)
var a = CustomSwipeView(frame : rect)
a.delegate = self//step5
self.myView = a
self.view.addSubview(self.myView)
}
// Step 6:
func sendIndex(row : Int){
switch row {
case 0:
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let moneySummaryVC: MoneySummaryVC = storyboard.instantiateViewControllerWithIdentifier("moneyVC") as! MoneySummaryVC
self.navigationController?.pushViewController(moneySummaryVC, animated: true)
default:
println("no index")
}
}