Calling loadView method to reload the uiview - ios

I have a case where all my subviews of a viewcontroller (say A) are declared in the loadView method. When I remove the viewcontroller's view (say B) from the superview and add back viewcontroller A's view to the superview, how can I reload the view? Subview B is on top of subview A and when I remove B I should be looking at an Update subview A.

Simply removing the view doesn't destroy it. But if you have destroyed the view (by calling [viewcontroller setView:nil]), then simply calling [viewcontroller view] will reload the view. You should never call loadView - the system will do it for you.

Related

UIViewControllers in uiscrollview calling viewdidappear

I'm adding 5 viewcontrollers to a scrollview with a page control so I can swipe from one viewcontroller to another.
At initialization I'm loading 3 viewcontrollers (left, middle, right)
My problem is that my three viewcontrollers are firing a viewDidAppear but only the center viewController is visible...
Is there a way to avoid the view controllers that aren't visible to call viewdidappear?
I'm adding my viewcontrollers to my scrollview like so :
if (controller != nil){
[self addChildViewController:controller];
[controller didMoveToParentViewController:self];
}
[scrollView addSubview:controller.view];
No, the viewDidAppear method is not related to the visibility of the view, if you get the documentation you will see
Notifies the view controller that its view was added to a view hierarchy.
So this method will be called when the view is added to the hierarchy. So the view was loaded, and added to the hierarchy, even in a non visible space of your mainView, it will call viewDidAppear.
To achieve what you want, you should implement the delegate of the scrollView, check the offset, and see in which page you are, then you can call a method on your viewController to do the job you want.

Adding a child viewcontroller

I have 2 views, ParentViewController and ChildViewController; I want to nest ChildViewController inside ParentViewController. I have designed ParentViewController and ChildViewController in Storyboard. ParentViewController.m contains the logic for the parent and ChildViewController.m contains the logic for the child. In ParentViewController.m I add the child like so:
ChildViewController *childVC = [self.storyboard instantiateViewControllerWithIdentifier:#"ChildSBI"];
[self addChildViewController:childVC];
My Question: How can I position childVC within ParentViewController (i.e. set it's origin).
It seems like I want to do something like the following:
Alloc init child
Call (void)addSubview:(UIView *)view on the view of the child
How can I position childVC within ParentViewController (i.e. set it's origin).
Remember that a view controller doesn't have an origin -- only its view does. To make a view controller's content appear on screen, you need a view to draw it in. You can have one view controller's view appear in another view controller's view hierarchy, and it's quite easy to do with storyboards.
To set up a parent/child relationship between view controllers in a storyboard, do the following:
Add a container view to the parent view controller's view hierarchy.
Drag an "embed" segue from that container view to the child view controller.
You can use the usual -prepareForSegue:sender: method to let the parent controller pass data to the child.
You can also set things up programmatically, if you prefer, and the code you've got is a good start:
ChildViewController *childVC = [self.storyboard instantiateViewControllerWithIdentifier:#"ChildSBI"];
[self addChildViewController:childVC];
After that, your parent view controller should set the child's view's position and add it to the parent's view, like this:
UIView *childView = childVC.view;
childView.frame = CGRectMake(150, 300, 100, 100); //use whatever coordinates you want
[self.view addSubview:childView];
There's a lot more detail in Creating Custom Container Controllers, but the basic answer to your question is that the container controller should set the size and position of the contained controller's view.

Adjusting a Child ViewController inside another viewController

I am trying out an application using a child ViewController inside another viewController.
I have a VC and I am instantiating another VC with its own xib inside the outer VC.
I am adding it as a child using the new iOS 5 method addChildViewController and also I have added its view as a subView.
But how do I control its position and size inside the parent view controller ?
should I modify the frame of the child controller's view ?
or I have to adjust the freeform view in the xib itself ?
Also in my current implementation, the child view starts behind the status bar of the parent viewcontroller's view.
Any idea on how to systematically implemement something like this ?
#define SUBVIEWS_FRAME CGRectMake(0,20,100,100) // whatever frame you need
- (void)addChildViewController:(UIViewController *)childController{
[childController.view setFrame:SUBVIEWS_FRAME];
[super addChildViewController:childController];
}
Simply just add a view in parentViewController #synthesize that view like childView. now add your childViewController as a subView in childView of parentViewController. it will simple to adjust using parentViewController View on nib file. If you have a large childViewController then please use UIScrollView instad of UIView

adding subviews in uiviewcontroller make it goes will

I have a custom UIView that I need to add as a subview in a UIViewController.
But if I use [self.view addSubview:newView]; the app goes in a infinity loop and doesn't start. But if I use self.view = newView then it works. But I need it as a subview.
The UIView contains a grid layout of custom button.
I guess you do that in the loadView method. You must not call the view method of the view controller in loadView because that will itself call loadView! However calling setView: (e.g. self.view = newView) is okay.
My suggestion is to add the subview in viewDidLoad.

Accessing parent view of current view - iOS

When I want to access the parent UIView of current UIView I declare object of parent UIView in current UIView and access it by assigning the parent UIView object to current view's object property.
Is there any way to get rid of this and directly call the parent view's methods or properties?
I also tried (parentView *) self.view.superview but didn't help.
It gives error while calling function for this object as
[UIVIew unrecognized selector......
If you're calling this directly from a UIView (and not a UIViewController), it should be self.superview instead of self.view.superview.
# Naveed: This is the common code u can use to any view whether it is parent or child view, Just change button name which u want to press and the view name on which u want to go. For example on back button press u want to go on library view then write this code -
-(IBAction)backButtonPressed:(id) sender
{
llibraryView *libraryview=[[libraryView alloc] initWithNibName:nil bundle:nil];
libraryview.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self presentModalViewController:libraryview animated:YES];
[libraryview release];
}
Let me know whether ur problem is solved or not.
What I can think is that you want to call the viewcontroller's method from your view, which you added to the viewcontroller's view.
Now you have two options from here, either you set your view controller your view's delegate and then call your viewcontroller's method by [delegate performSelector:] approach.
The other approach is you access your view's superview, and then get it's controller. You can not do that directly, coz if you could do that it would defeat the entire purpose of MVC.
But, still there is a way out, here you go:-
Get to UIViewController from UIView?
Hope, this helps you.

Resources