prepareForSegue未在UIBarButtonItem选择器内调用

时间:2015-06-26 15:06:11

标签: ios swift interface-builder

这是我的代码,我注册了UIBarButtonItems及其选择器函数。

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    var edit = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editTruck"))
    let delete = UIBarButtonItem(title: "Delete", style: UIBarButtonItemStyle.Plain, target: self, action: "deleteTruck")
    let menu = UIBarButtonItem(image: UIImage(named: "menu"), style: UIBarButtonItemStyle.Plain, target: self.revealViewController(), action: "rightRevealToggle:")

    self.navigationItem.rightBarButtonItems = [menu,delete,edit]

    self.title = truckName.text

    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
}

func editTruck(){

    println("insdie edittruck")

    self.performSegueWithIdentifier("editTruck", sender: self)

     func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
    println("inside prepare")            
        if segue.identifier == "editTruck"{
            let vc = segue.destinationViewController as? UINavigationController
            let topView = vc?.topViewController as! AddTruckViewController
            topView.truckNameValue = name.text!
            topView.totalFuelupsValue = totalFuelups.text!
            topView.totalDistanceTrackedValue = totalDistanceTracked.text!
            topView.truckYearValue = truckYear.text!
            topView.truckMakeValue = truckMake.text!
            topView.truckModelValue = truckModel.text!
            topView.truckImagevalue = truckImage.image!
            topView.engineMakeValue = engineMake.text!
            topView.engineModelValue = engineModel.text!
            topView.horsePowerValue = horsePower.text!
            topView.axleValue = axle.text!
            topView.trailerTypeValue = trailerType.text!
            topView.steerTireMakeValue = steerTireMake.text!
            topView.steerTireModelValue = steerTireModel.text!
            topView.steerTireSizeValue = steerTireSize.text!
            topView.driveTireMakeValue = driveTireMake.text!
            topView.driveTireModelValue = driveTireModel.text!
            topView.driveTireSizeValue = driveTireSize.text!
            topView.transmissionMakeValue = transmissionMake.text!
            topView.transmissionModelValue = transmissionModel.text!
            topView.distanceUnitValue = distanceUnit.text!
            topView.volumeUnitValue = volumeUnit.text!
        }
    }
}

在IB I Control中从TruckSpecsViewController(自我的按钮以编程方式创建的顶部的黄色图标)拖动,我上面的代码来自我想要切换到的场景。我将标识符添加到" editTruck"的segue中。在上面的代码中,performSegueWithIdentifier可以工作,但prepareForSegue不起作用。没有错误或任何它永远不会被调用的东西。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

prepareForSegue:sender方法移至与View Controller中的editTruck方法相同的级别。范围的改变将解决您的问题。

iOS会自动调用View Controller上的prepareForSegue:sender方法,以响应故事板中的segue或在代码中调用performSegueWithIdentifier:sender:

你永远不会自己打电话给prepareForSegue:sender;您只需在View Controller中实现此方法。