ios - 使用分段控制操作调用 subview Controller ?

标签 ios swift uisegmentedcontrol uicontainerview childviewcontroller

我的父 View Controller 中有两个 subview Controller ,我想在分段控件中的值更改时调用它们,并希望通过 subview Controller 设置父 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()
        }
}

最佳答案

更新

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

相反,我认为您应该创建自定义 View ,然后将它们添加到主Storyboard

然后,当用户点击Segmented Control 并将值传递给他们时,您可以简单地隐藏/显示您的自定义 View ,例如:

CustomEditView.valueY = newValueY
CustomFiltersView.valueX = newValueX

关于:

I need to call it forcefully through segmentedControl action, so that my values in the childView be updated

然后您需要将目标 View Controllers 映射到局部变量,并在用户按下这些段时使用它们来更新目标 View Controller 变量

我已经在我的回答中更新了代码和“演示”以反射(reflect)这一点。
(请注意,我只是将随机 Strings 放在 labels 中以表明观点。)

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


在您描述的设置中 in your other question ,它基于 containersView Controllers 已经存在于 Storyboard 中。您绝对不需要再次呈现它们(您可以删除 performSegueWithIdentifier 调用)。

如果我没理解错的话,您只是想根据用户通过分段控件选择的内容向他们显示不同的“ Controller ”。

有一些方法可以做到这一点,但最简单的方法是隐藏和显示 ControllerEdit/ControllerFilterscontainers View Controllers -- 通过更改 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
}

关于ios - 使用分段控制操作调用 subview Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43016838/

相关文章:

ios - UIView渐进式动画

swift - 无法从 Firestore 数据库加载分段控制 TableView 中的数据

ios - UIAlertViewController 在模拟器中打开慢速动画后没有响应

ios - 如何在网站内容更改时发送推送通知

cocoa-touch - 从 UISegmentedControl 切换 Storyboard场景?

ios - UISegmentedControl 标题文本模糊

c# - 从 UIViewController 派生的类中的 Monotouch Dialog 实现

ios - 使用越狱调整/脚本将新的 UIBackgroundMode 添加到 App-Store 应用程序

ios - swift/Xcode6 : Are PDF images working with UIImage(named:)?

ios - Swift 中 WebView 的问题