How to update Superview from its Subview in iPad? - iphone

I have a UISegmentedController i have 3 UIViewControllers Like photos,frames,gallery.
I add those 3 Views in Superview Using 'addSubView'. In frames view i have added a subview that name as EditView. In EditView i have done some changes, i want to update these changes in frames view. But when am removing EditView from frames view any single method doesn't calling. Then how to update the changes from subview in superview. Tree: UISegmentedController -> Frames(Su) -> EditViews(Subview). Can any one help me..

I found the code to update something in superview from subview. Please use this code in your subview. It will call your superview viewWillAppear method. You can use another method instead of viewWillAppear. It works for me.
for (UIView* next = [self.view superview]; next; next = next.superview)
{
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
{
[(UIViewController*)nextResponder viewWillAppear:YES];
}
}
-Yuva.M

You could access the super view of any UIView by using the superview property of UIView.
The below statement will be in your EditView.
FrameView* mySuperFrameView = (FrameView*)self.superview;
and the next statement could be calling the super view function.
[mySuperFrameView updateMySuperView];
updateMySuperView is the part of your superview.

Removing a view from its superview releases it.
Submit your changes before removing the editView from its superview. You could overwrite removeFromSuperview in your editView, and do your data manipulation before calling super removeFromSuperview.
Or your view controller could take data from the editView before removing it.

Related

Best way to call method that exists in multiple classes

I have a main UIViewController where most of the users' interaction happens. In the main ViewController there are three subviews. The user can load separate ViewControllers into the UIView subviews.
Each of the subviews that are loaded deal with data entry. In turn, firstResponders are called. I would like to be able to dismiss the firstRespnders through the main ViewController, maybe with a 'Done' button.
I was thinking I could add a method in each of the separate subviews with one name ex;
-(void) methodToResignResponders {}
Then, in the main ViewController call this method to the view that is currently open to the user. In turn resigning the responders that are active in the subview.
Further Information:
This is how I set up each view as a subview of the main ViewController:
UIViewController *calcVC;
//set up the view to be added depending on the name of the view that was passed
if ([viewName isEqualToString:#"Tax"]) {
calcVC= [[TAXViewController alloc]initWithNibName:#"TAXViewController" bundle:nil];
}else if ([viewName isEqualToString:#"Rent"]){
calcVC= [[RENTViewController alloc]initWithNibName:#"RENTViewController" bundle:nil];
}else //continues with more views...
//Then add it to the subview
[firstView addSubview:calcVC.view];
Not sure if I've got the gist of this, mostly because it sounds like you've already solved it yourself. :)
But, from what I can see the ViewController you are talking about is always an UIViewController instance named calcVC. If it is always this viewController's view you are referring to you can simply call [calcVC.view resignFirstResponder];
You can make a basic protocol that all of your sub-view controllers implement that has does everything you need (resign first responder and anything else).
Not sure if this answers your question but you can loop through all the subviews and call it if it exists as follows:
for (UIView *subview in [self.view subviews]) {
if ([subview respondsToSelector:#selector(resignFirstResponder)]) {
[subview resignFirstResponder];
}
}

removefromsuperview problem

when I call removeFromSuperview from my view.m file( [self removeFromSuperview]; ), it seems working fine. but when I call that method from the view controller.m file([self.view removeFromSuperview]) it only returns error. I have no idea what is wrong about it.
I am guessing your view is one of the subviews in your controller's view. Calling [self.view removeFromSuperview] only tries to remove the controller's view from the parent view. This is probably not what you want to do. You need to find the child of self.view that is of your view's type ( or using a tag) and then remove it.
Without the exact code this is what you need to do.
Set the tag of the view that you want to be removed to 42 (myview.tag = 42 ) before/after adding it to the controllers view.
When you are ready to remove the view loop through the subviews of controllers view and if the subview's tag is 42 then do a removeFromSuperview on that view. So something like this
for (UIView *view in [self.view subviews] ) {
if (view.tag == 42 ) {
[view removeFromSuperview];
}
}

put subviews of a UIView in front of its CALayer?

Is this possible? Basically, I want to give my UIView a subview, and have that subview be in front of the view's layer (more specifically, in front of said layer's border).
Of course I could achieve the effect I want by making two subviews of the view's superview, one on top of the other. But I'd rather avoid that if possible.
I looked for this for a while myself; i don't believe it is possible. I solved the issue by, as you hint at, adding the subview and the parent to the same 'container' superview. Then its just a matter of ordering the two subviews, so that your subview is above the other and its border.
I solved this with a segue animation where i needed the sourceViewController to be in front of the destinationViewController. I had to remove the sourceViewController in order to re-nest it. My code looks like this:
- (void) perform {
UIViewController *sourceViewController = (UIViewController *) self.sourceViewController;
UIViewController *destinationViewController = (UIViewController *) self.destinationViewController;
UIView *parent = sourceViewController.view.superview;
[sourceViewController.view removeFromSuperview];
[parent addSubview: destinationViewController.view];
[parent addSubview:sourceViewController.view];
// Perform animation stuffs...
}

Calling method in parent UIViewController, after adding by addSubview

I have a UIViewController that is creating another view controller, and adding its view as a subview:
In the parent UIViewController:
SlateMoreView* subView = [[SlateMoreView alloc] initWithNibName:#"SlateMoreView" bundle:nil];
[self.view addSubview:subView.view];
I then need to call a method from the subview, in the parent view.
I have seen how to do this when I am adding the sub UIViewController using [self.navigationController pushViewController: subView animated: YES], because I can find the parent using this kind of code:
In the sub view UIViewController:
NSArray* viewControllerArray = [self.navigationController viewControllers]
int parentViewControllerIndex = [viewControllerArray count] - 2;
SlateView* slateView = [viewControllerArray objectAtIndex:parentViewControllerIndex];
...and then I can send messages to it. But since I added the sub view manually by using addSubView, I can't do this.
Can anyone think of how I can talk to my parent UIViewController?
Thanks!
UIViews have a superview property which seems to be what you are looking for.
In addition you probably don't want to nest UIViewController's view like that unless you are very deliberately building a custom contain view controller. See http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/
You might want to consider if your problem can be solved by using NSNotifications. You could post a notification from your subview when an event happens that interested listeners (your superview) need to know about . When the superview receives the notification, it can run whatever code you wish. All the while the subview never needs to know about the superview.
This is one way to make your classes less dependant on each other.
You could also use delegation as another option.
When you add your view as a subview to a view hierarchy, you put it in the responder chain. You can go up the responder chain to reach the view controller as a UIView controlled by a UIViewController has the UIViewController as its nextResponder.
id object = theSubview;
do {
object = [object nextResponder];
} while ( ![object isMemberOfClass:[YourViewController class]] );
// object has the view controller you need.

switch between uiviews with buttons and not uinavigation controllers

I have seen the post for How to switch views by buttons on iPhone? but this doesn't answer how to switch back and forth between views with buttons. The person that asked the question settled on the answer that they could switch between views with uinavigationcontroller.
I put the following code in an ibaction that kicks off when a button is pressed in the primary view.
PhoneNumberViewController *phoneNumberViewController1 = [[PhoneNumberViewController alloc] initWithNibName:#"PhoneNumberView2" bundle:nil];
self.phoneNumberViewController = phoneNumberViewController1;
[self.view removeFromSuperview];
[self.view insertSubview: phoneNumberViewController1.view atIndex:0];
When this code executes the whole view just goes blank. If I omit the removefromsuperview portion then the view disappears behind my button but the button still remains. I'm not sure if this is the right way to switch between buttons but if anyone knows how to do this please help. Also if anyone knows about any example projects that switch between views with buttons let me know.
Thanks a million!
You removed the view controller's view from it's superview and then added a subview to it. The view hierarchy is broken at the view controller's superview (likely your window). That is why you are getting a blank screen.
You'd likely want to keep a reference around to the original view and then swap it out to the new view by setting the view controller's view to the new view.
// origView is an instance variable/IBOutlet to your original view.
- (IBAction)switchToPhoneView:(id)sender {
if (origView == nil)
origView = self.view;
self.view = phoneViewController.view;
}
- (IBAction)switchToOriginalView:(id)sender {
self.view = origView;
}
The technique I usually use involves creating a superview class which contains a toolbar at the bottom, and a content view UIView class filling the rest of the screen.
I then add subviews to the content view to change the views based on button clicks. This approach makes it so the toolbar on the same is constant across all views. I start by defining a helper function like this:
-(void) clearContentView {
//helper to clear content view
for (UIView *view in [self.contentView subviews]){
[view removeFromSuperview];
}
}
My IBAction then looks like this:
-(IBAction) buttonClicked{
self.title = #"Images"; //change title of view
[self clearContentView]; //clear content view
[self.contentView addSubview:self.imagesViewController.view]; //add new view
[self.imagesViewController viewWillAppear:YES]; //make sure new view is updated
[self enableButtons]; //enable all other buttons on toolbar
self.imagesButton.enabled = NO; //disable currently selected button
}