0

So, I have a view with the button. I tap on this button and modal view with style "Form Sheet" appears. There I make a change, and because of this change the background color of our first view with button (now behind of modal view) must be changed. Question is, how to do that?

Randex
  • 770
  • 7
  • 30

2 Answers2

1

The viewcontroller of the underlying page is still there and can still execute code on its view. The view may be unloaded because it's not on screen, but viewcontroller.view is a lazy contructor, so setting properties on it will cause it to be re-created anyway.

The trick is how to communicate with the underlying view controller. A good way to handle cross-viewcontroller communication is to post an NSNotification using NSNotificationCenter.

Set your background viewcontroller to observe a notification called something like "BackgroundColorChangedNotification" and then post a notification with that name from your modal form viewcontroller.

The advantage of this approach is that if any other controllers in the app need to know about this change, they can all just register for the notification, and none of these controllers need to know anything about each other, so your code is nicely decoupled.

Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103
0

Self.view.parentView will let you access the first view

You can then change its background using setBackgrouncolor method

Shubhank
  • 21,721
  • 8
  • 65
  • 83
  • There's no parentView property, but self.parentViewController. My parentViewController is UINavigationController. – Randex Jan 20 '12 at 15:37
  • Yeah..it can be parentViewController.. Actually I am on my iPhone and this code I mostly do with autocomplete feature of xcode so that's why the code mistake – Shubhank Jan 20 '12 at 15:39
  • 1
    Apple changed the way that the parentViewController property works between iOS4 and iOS5 - I'd be cautious about using this approach. – Nick Lockwood Jan 20 '12 at 15:42