1

I have an UIImageView (img) on the container2View as an IBOutlet. How can I change the img.image and refresh the view from other container (containerView)?

We have to use delegation

img01

mainController

class mainController: UIViewController, containerDelegate {

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

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

        switch segue.identifier! {

            case "container":

            var view = segue.destinationViewController as containerController
            view.delegate = self

            break

            default: break
        }
    }

    func change(name: String) {

        println(name)    //prints the name
        container2Controller().img.image = UIImage(named: "name")   //not seems to work
    }
}

containerController

protocol containerDelegate {

    func change(name: String)
}

class containerController: UIViewController {

    var delegate: containerDelegate?

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

    @IBAction func click() {

        delegate?.change("hi")    
    }
}

container2Controller

class container2Controller: UIViewController, containerDelegate {

    @IBOutlet var img: UIImageView!

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

    func change(name: String){

        self.img.image = UIImage(named: name)
    }
}
elGeekalpha
  • 1,826
  • 3
  • 13
  • 14

1 Answers1

1

You could handle this by using delegation. Let's say container2 has a button that when pressed should initiate an update to container4.

In Container2, declare a delegate protocol like:

@protocol Container2Delegate
- (void) actionDidOccurInContainer2: (Container2) container2;
@end

and add a property to Container2 class:

@property (weak, nonantomic) id<Container2Delegate> delegate;

Then implement the delegate method in the superContainer, and set Container2s delegate to be the superContainer (in prepareForSegue if you're using storyboards).

In the method implementation, you can update Container4:

- (void) actionDidOccurInContainer2: (Container2) container2 {
     container4.imageView.image = <your image>
}

In container2 you'll have something like:

- (IBAction)doSomething: (id) sender
{
    [self.delegate actionDidOccurInContainer2: self];
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • Thanks for your answer. I update the question with a simple code. I'm doing it with Swift and all the views are load/displayed at the same time, so I don't understand when you say 'in prepareForSegue'. Could you fill the rest of my code? – elGeekalpha Dec 09 '14 at 11:56
  • It sounds like you need to do some reading (and understanding) of Apple's documentation before going any further. Did you look up the `prepareForSegue` method? Did you try it out to see what happened? Did you read about delegate protocols? Did you try to implement one as I described? (the language shouldn't matter, the principle is the same). Once you've experimented yourself, come back and ask a question with the code you've tried, let us know what worked and what didn't and you'll get more help. We're not here to code for you. – Ashley Mills Dec 09 '14 at 12:01
  • First of all sorry, it wasn't my intention. I know prepareForSegue it's used for passing data between views. I tried to follow your steps and I read about it before, but there's something I don't understand about delegates: Cloud you explain me this steep? 'Then implement the delegate method in the superContainer, and set Container2s delegate to be the superContainer (in prepareForSegue if you're using storyboards).' Anyway I'll try for myself. Thanks for your help. – elGeekalpha Dec 09 '14 at 12:24
  • 1
    OK, so in your storyboard, each sub-container view controller has an embed segue associated - your first step is to give each one a unique identifier (e.g. EmbedContainer1, etc). Then override the `prepareForSegue:` method in the main view controller, and you'll see a segue fired for each embedded controller. At that point you can call `segue.destinationViewController(setDelegate: self)` – Ashley Mills Dec 09 '14 at 12:33
  • I followed your instructions and researched a bit about segue and delegation (http://goo.gl/1SQq8M) but there is something that escapes myself: The func change() of Protocol should be implemented on my container2Controller or on the mainController, because If y use the containerDelegate I must implement it. I update the code but don't seems to work. – elGeekalpha Dec 10 '14 at 15:16