gpt4 book ai didi

ios - 回调方法不在 Swift 中从 UIView 调用到 ViewController

转载 作者:行者123 更新时间:2023-11-28 13:15:41 24 4
gpt4 key购买 nike

我有两个类 CustomSwipOutUIView 的子类和 ViewController 的子类 em>UIViewController。委托(delegate)的回调方法 未在ViewController 类 中从 didSelectRowAtIndexPath 中定义的 TableView 委托(delegate)方法触发>CustomSwipeOut。我在 ViewController 类中完成了可选绑定(bind) var a = CustomSwipeOut(); a.delegate = ViewController()

protocol SendIndexDelegate{

func sendIndex(Int);

}

class CustomSwipeOut: UIView , UITableViewDataSource , UITableViewDelegate {


var label: UILabel = UILabel()
var myNames = ["item1","item2","item3"]
var delegate : SendIndexDelegate?


override init()
{
super.init()

}

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) {

var row = indexPath.row
if let temp = self.delegate {

delegate?.sendIndex(row)

}else{

println("optional value contains nill value")




}

}

// View Controller 类

    class ViewController: UIViewController , SendIndexDelegate {

var myView :UIView = UIView()


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

var a = CustomSwipeOut()
a.delegate = ViewController()

let rect: CGRect = CGRect (x: self.view.frame.size.width, y :50 , width: self.view.frame.size.width-100, height: self.view.frame.size.height-100)
self.myView = CustomSwipeOut(frame : rect )
self.view.addSubview(self.myView)

//optional chaining


}

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


@IBAction func showSideMenu() {

UIView.animateWithDuration(0.2, animations:{

self.myView.frame = CGRectMake(100, 50,self.view.frame.size.width-100,self.view.frame.size.height-100)

} )

}

//delegate method
func sendIndex(row : Int)
{
switch 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)



println("index 2 clicked")

...

default:
println("no index")


}


}


}

最佳答案

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let rect: CGRect = CGRect (x: self.view.frame.size.width, y :50 , width: self.view.frame.size.width-100, height: self.view.frame.size.height-100)
var a = CustomSwipeOut(frame : rect)
a.delegate = self
self.myView = a
self.view.addSubview(self.myView)

//optional chaining




}

关于ios - 回调方法不在 Swift 中从 UIView 调用到 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29096756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com