两个不同的按钮执行segue到相同的视图控制器,但想通过一个按钮禁用segue的用户交互

我从相同屏幕上的两个不同按钮执行segue到相同的视图控制器,因此使用相同的segue标识符 . 说按钮是B1和B2 . 因此,当我从B1执行segue的segue时,我希望目标视图控制器禁用用户交互,但是当我从B2中删除时,我希望目标视图控制器可以被点击并且其文本字段是可编辑的 . 请帮我实现它 . 在这里......考虑B2被部分压缩,B1被认为是toplbl,请帮我用这个代码实现它,所以建议我在这里做的改变 .

@IBAction func partialPressed(sender: UIButton)
{
    let i = self.getIndex(sender.tag)
    let j = self.getSection(sender.tag)
    let indexarr = ["index":i, "section":j]

    if getSection(sender.tag) == 0
    {

    let arr = getArray(sender.tag)
    performSegueWithIdentifier("partialSupplement", sender: arr)

    }
    else if getSection(sender.tag) == 1
    {
        let arr = getArray(sender.tag)
        performSegueWithIdentifier("PartialPopup", sender: arr)




    }

    else if getSection(sender.tag) == 2
    {
        let arr = getArray(sender.tag)
        performSegueWithIdentifier("FoodLifestyle", sender: arr)



    else if getSection(sender.tag) == 3
    {
        let arr = getArray(sender.tag)
        performSegueWithIdentifier("Food", sender: arr)


    else if getSection(sender.tag) == 4
    {
        let arr = getArray(sender.tag)
        performSegueWithIdentifier("Others", sender: arr)
      task.resume()
    }

}

@IBAction func toplbl(sender:UIButton){

let i = self.getIndex(sender.tag)
    let j = self.getSection(sender.tag)
    let indexarr = ["index":i, "section":j]

    if getSection(sender.tag) == 0
    {
              task.resume()

        let arr1 = getArray(sender.tag)
        performSegueWithIdentifier("partialSupplement", sender: arr1)





    }
    else if getSection(sender.tag) == 1
    {
        let arr1 = getArray(sender.tag)
        performSegueWithIdentifier("PartialPopup", sender: arr1)


    }

    else if getSection(sender.tag) == 2
    {
        let arr1 = getArray(sender.tag)
        performSegueWithIdentifier("FoodLifestyle", sender: arr1)

              task.resume()
    }

    else if getSection(sender.tag) == 3
    {
        let arr1 = getArray(sender.tag)
        performSegueWithIdentifier("Food", sender: arr1)
          task.resume()
    }

    else if getSection(sender.tag) == 4
    {
        let arr1 = getArray(sender.tag)
        performSegueWithIdentifier("Others", sender: arr1)
                 task.resume()
    }

}

override func prepareForSegue(segue:UIStoryboardSegue,sender:AnyObject?){

if segue.identifier == "menuviewcontroller"
    {
        let menuTableViewController = segue.destinationViewController as! MenuViewcontroller
        menuTableViewController.transitioningDelegate = menuTransitionManager
        menuTransitionManager.delegate = self

       // menuTableViewController.customchildcelldataarray = self.customchildcelldataarray

    }
    if segue.identifier == "PartialPopup"
    {
        let popupViewController = segue.destinationViewController as! PatialViewController
        popupViewController.arr = sender as! CustomcomingupDataWorkOut
        //popupViewController.timings_id = popupViewController.arr.timings_id
        //menuTransitionManager.delegate = self
        // menuTableViewController.customchildcelldataarray = self.customchildcelldataarray

    }
    if segue.identifier == "partialSupplement"
    {
        let popupViewController = segue.destinationViewController as! PartialSupplementViewController
        var arr = sender as! CustomcomingUpDataSupplements
        popupViewController.lifestyletype = arr.supplementName
        popupViewController.lifestyleItem = arr.amount + arr.unit
        popupViewController.lifestyleQuantity = arr.dosage_main_name
        popupViewController.timings_id = arr.timings_id



        //popupViewController.arr = sender as! CustomcomingUpDataSupplements
        //popupViewController.timings_id = popupViewController.arr.timings_id
        //menuTransitionManager.delegate = self
        // menuTableViewController.customchildcelldataarray = self.customchildcelldataarray

    }

    if segue.identifier == "FoodLifestyle"
    {
        let popupViewController = segue.destinationViewController as! partialFoodViewController
        var arr = sender as! CustomcomingUpDataLifeStyle
        popupViewController.lifestyletype = arr.lifestyle_name
        popupViewController.lifestyleItem = arr.time + " minutes"
        popupViewController.lifestyleQuantity = "Time"
        popupViewController.timings_id = arr.timings_id


        //popupViewController.arr = sender as! CustomcomingupDataWorkOut
        //menuTransitionManager.delegate = self
        // menuTableViewController.customchildcelldataarray = self.customchildcelldataarray

    }
    if segue.identifier == "Food"
    {
        let popupViewController1 = segue.destinationViewController as! FoodViewController
        var arr = sender as! CustomcominUpDataFood
        print(arr)
        popupViewController1.type = arr.food_name
        popupViewController1.Item = arr.time
        popupViewController1.Quantity = " Amount"
        popupViewController1.timings_id = arr.timings_id


        //popupViewController.arr = sender as! CustomcomingupDataWorkOut
        //menuTransitionManager.delegate = self
        // menuTableViewController.customchildcelldataarray = self.customchildcelldataarray

    }
    //Others
    if segue.identifier == "Others"
    {
        let popupViewController = segue.destinationViewController as! PartialOthersViewController
        var arr = sender as! CustomcominUpDataOthers
        popupViewController.lifestyletype = arr.others_name
        popupViewController.lifestyleItem = arr.time + " minutes"
        popupViewController.lifestyleQuantity = "Completion"
        popupViewController.timings_id = arr.timings_id
        //popupViewController.arr = sender as! CustomcomingupDataWorkOut
        //menuTransitionManager.delegate = self
        // menuTableViewController.customchildcelldataarray = self.customchildcelldataarray

    }

}

回答(3)

3 years ago

使用prepareForSegue:方法 . iOS会将按钮作为 sender 参数传递给您,并且还可以将目标视图控制器作为 UIStoryboardSegue 参数的一部分提供 .

检查 sender 以查看它是 B1 还是 B2 . 当发件人是 B2 时,禁用segue目标控制器中的用户交互:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: Any?) {
    if segue.identifier == "MySegue" {
        let enableInteration = (sender != button2)
        segue.destinationViewController.view.userInteractionEnabled = enableInteration
    }
}

3 years ago

您可以使用一个密钥,并在调用segue之前更改该密钥的值 . 并根据该键改变了按钮/文本字段等的交互 . 这是一种解决方法,也许你会得到比这更好的答案 .

3 years ago

我发现了一个简单的方法 . 我们可以为partialpressed按钮声明一个全局或局部布尔变量,我们可以将其设置为true,对于toplbl按钮,我们可以将其设置为false,然后我们有两种方法来禁用用户交互:

  • 如果bool是全局的:转到目标视图控制器并且内部视图加载检查是否bool == false然后设置self.view.userinteraction = false

  • 如果bool是局部变量:在初始视图控制器中转到prpareforsegue并检查是否bool == false然后设置segue.destinationviewcontroller.view.userinteraction = false