使用分段控制动作调用子视图控制器?

时间:2017-03-25 13:18:53

标签: ios swift uisegmentedcontrol uicontainerview childviewcontroller

我的父视图控制器中有两个子视图控制器,我想在分段控件中更改值时调用它们,并希望通过子视图控制器设置父imageView的值。

protocol UserEdittedPhoto {
    func UserIsDone(image:UIImage)
}

class ControllerFinal:UIViewController, UserEdittedPhoto{

    func UserIsDone(imageEditted: UIImage){
        self.usedImage=imageEditted
        self.imageView.image=self.usedImage
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func segmentAction(sender:UISegmentedControl){

        if (segmentedControl.selectedSegmentIndex==0){

            performSegueWithIdentifier("EditIAm", sender: nil)
        }

        else if (segmentedControl.selectedSegmentIndex==1){

            performSegueWithIdentifier("EditIAm", sender: nil)
        }
    }

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

        if segue.identifier == "EditIAm"{

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewControllerWithIdentifier("ControllerEdit")
            self.presentViewController(controller, animated: true, completion: nil)

            let nextView = segue.destinationViewController as! ControllerEdit
            nextView.originalImage=self.imageView.image!
            nextView.delegate=self
        }

        else if segue.identifier == "FilterIAm"{

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewControllerWithIdentifier("ControllerFilters")
            self.presentViewController(controller, animated: true, completion: nil)

            let nextView = segue.destinationViewController as! ControllerFilters
            nextView.toBeFilter=self.imageView.image!
        }
    }

    class ControllerEdit:UIViewController{
        var delegate: UserEdittedPhoto? = nil
        if (delegate != nil){
        self.originalImage = UIImage(CIImage: CIImage(image: self.originalImage)!.exposureAdjustFilter(sliderValue.value)!)
        self.delegate!.UserIsDone(self.originalImage)
        print("I am Called!")
        }
    }

    class ControllerFilters:UIViewController{
        override func viewDidLoad() {
            print("SAHASHhqdwiuhiuhsaiuhsaiudhiuash")
            controllerFinal?.imageView.image=toBeFilter
            print("SAHASHhqdwiuhiuhsaiuhsaiudhiuash")
            super.viewDidLoad()
        }
}

2 个答案:

答案 0 :(得分:1)

在此函数中放置一个断点:

@IBAction func segmentAction(sender:UISegmentedControl){

    if (segmentedControl.selectedSegmentIndex==0){

        performSegueWithIdentifier("EditIAm", sender: nil)
    }

    else if (segmentedControl.selectedSegmentIndex==1){

        performSegueWithIdentifier("EditIAm", sender: nil)
    }
}

如果没有被调用,那么你可能没有将它连接到IB中的动作(@IBAction左边的圆圈是填充的吗?)

如果它被调用,那么确保segue名称是正确的 - 同样,在else中修复一个if,因为它看起来像你想要“FilterIAm”那里。

然后,在prepareForSegue:...中放置一个断点 - 是否被调用?如果没有,请重新检查名称与IB中的名称相同。

编辑:基于评论

您的prepareForSegue不应该创建ViewController。目标视图控制器是执行segue并传递给此函数的结果。

if segue.identifier == "EditIAm"{
    let nextView = segue.destinationViewController as! ControllerEdit
    nextView.originalImage=self.imageView.image!
    nextView.delegate=self
}

您不需要提供任何内容 - 将显示destinationViewController。你可以设置它的任何变量(如你所知) - 这就是为segue做准备的意思。

答案 1 :(得分:1)

<强>更新

为了在下面的评论中反映我们的讨论,我认为您不需要ContainersView Controllers来管理您的自定义控件(编辑/过滤器)。这太过分了。

相反,我认为你应该创建custom Views,然后将它们添加到主Storyboard

然后,当用户点击Segmented Control并向其传递值时,您只需隐藏/显示自定义视图,例如:

CustomEditView.valueY = newValueY
CustomFiltersView.valueX = newValueX

关于:

  

我需要通过segmentedControl动作强行调用它,这样   我在childView中的值被更新

然后,您需要将目标View Controllers映射到本地变量,并在用户按下细分时使用它们来更新目标View Controller variables

我在答案中更新了代码和“演示”,以反映这一点 (请注意,我只是在Strings中添加随机labels来表达观点。)

现在回答完整的答案......

在您描述的设置in your other question中,containers基于View ControllersStoryboard已存在performSegueWithIdentifier。您绝对不需要再次展示它们(您可以删除Segmented Control次来电)。

如果我理解正确,您只想根据他们通过containers选择的内容向用户显示不同的“控制器”。

有一些方法可以做到这一点,但最简单的方法是隐藏并显示ControllerEdit / ControllerFilters View Controllers的{​​{1}} - 通过更改containers isHidden变量状态。

像这样:

demo

Storyboard设置:

storyboard

代码(基于my other answer):

import UIKit

protocol UpdateImageProtocol {
    func userIsDone(image: UIImage)
}

class ViewController: UIViewController, UpdateImageProtocol {

    @IBOutlet weak var imageView: UIImageView!

    @IBOutlet weak var changeImageContainer: UIView!
    var controllerEdit: ControllerEdit?

    @IBOutlet weak var applyFilterContainer: UIView!
    var controllerFilters: ControllerFilters?

    var image = UIImage(named: "hello")

    override func viewDidLoad() {
        super.viewDidLoad()
        userIsDone(image: image!)
    }

    func userIsDone(image: UIImage) {
        imageView.image = image
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "controllerEdit" {
            let nextView = segue.destination as! ControllerEdit
            nextView.delegate = self
            controllerEdit = nextView

        } else if segue.identifier == "controllerFilters" {
            let nextView = segue.destination as! ControllerFilters
            controllerFilters = nextView
        }
    }

    @IBAction func segmentAction(_ sender: UISegmentedControl) {
        if sender.selectedSegmentIndex == 0 {
            changeImageContainer.isHidden = false
            applyFilterContainer.isHidden = true

            controllerEdit?.customLabel.text = String(arc4random_uniform(999))

        } else {
            changeImageContainer.isHidden = true
            applyFilterContainer.isHidden = false

            controllerFilters?.customLabel.text = String(arc4random_uniform(999))
        }
    }
}

class ControllerEdit: UIViewController {

    @IBOutlet weak var customLabel: UILabel!

    var image = UIImage(named: "newHello")
    var delegate: UpdateImageProtocol?

    @IBAction func changeImage(sender: UIButton) {
        delegate?.userIsDone(image: image!)
    }
}

class ControllerFilters: UIViewController {

    @IBOutlet weak var customLabel: UILabel!

    // TODO
}