UIViewController multiple UIView - iphone

This is just to confirm my solution:
I have two UIView that I would like to swap between controlled by the UIViewController
In IB I have created 3 UIViews:
TopView
A_View
B_View
Then in ViewDidLoad I add [
[self addSubView] A_View];
Then in some IBAction method I do this
[A_View removeFromSuperview];
[self.view addSubview:B_View];
This is indeed swapping the two views, with TopView just sitting there. But my first thought was just to call [[self view] removeFromSuperView]]; and then add B_view.
This gives me an empty screen?
So is my solution the right way?
Thanks in advance
Regards
Christian

Looks fine to me. It's a pretty normal swapping style, just remove one and add the other.
You should always watch out with modifying the UIViewController's view itself. Your solution is fine.

That way is fine and perfectly straightforward.
If you're on iOS 4.0 or later, you can also use the transitionFromView:toView:duration:options:completion: method in UIView, which removes and adds for you, plus provides a transition animation.
The reason [[self view] removeFromSuperview] left you with a blank window is because you're removing your controller's base view object (including it's subviews, e.g. Top_View, etc) from it's superview (likely the window object). Your solution works when you remove A_View and add B_View, as both are being added or removed as subviews to your controller's view, i.e. self.view.

Related

Can a UIView know when it is on screen

I have a UIView with a lot of components enclosed in it and would like to update some of them if the view is removed or if its parent view controller is popped/pushed. Is it possible for a UIView to get this information
Similar to the method in UIViewController
-(void)viewWillAppear;
Would like something like
-(void)UIViewWillAppear;
Edit for some comments I saw:
I'll explain a bit more
But I have a special case where the UIView needed to add a "floating view" on top of itself (Imagine a zooming/panning/scrolling UISCrollView subclass with floater on top of itself) such that when it scrolled the floating view stayed in place relative to the superview. I tried recalculating the new origin of the "floater" inside of the -(void)layoutSubviews method but the re-placement was very choppy. In order to solve this choppyness problem, the custom UIView added the floating view (which in theory is its subview) as a subview for its superview (a bit of a tongue twister :) ).
Now arises a problem when the custom UIView is removed (or its containing view controller is pushed offscreen). How can the special UISCrollView remove the floating view from its superView.
You can override willMoveToSuperview: to find out when a view is inserted into a hierarchy and when it's removed. That's probably not what you want since the view can be part of a hierarchy and not be inserted by itself.
To find out if it's on screen use willMoveToWindow:. If the argument is non-nil the view just became part of a visible view hierarchy.
If you need to do something after the change use didMoveToWindow:.
UIView do not appear/disappear 'randomly' or when they want - your view controllers (or code) control this. So you should find out when you show them, and call code you need.
The UIView class reference has some kvo observing change.
By implementing -(void)willRemoveSubview:(UIView *)subview you could see the other way round.
UPDATE After reading the explanations:
I hope I understood correctly. I did something similiar time ago, but with a UITableView rather than a UIScrollView (but they are quite the same underneath).
It was like a popup detail view. I solved, as you already did, by adding the detail view to the UITableView superview, and then I added a close UIButton in the detail view, with a corresponding IBOutlet:
#interface CustomUIView : UIView
#property(nonatomic,weak) IBOutlet UIButtonView *closingButton;
-(void)closeDetail:(IBAction)action;
#end
and the action was just:
-(void)closeDetail:(IBAction)action {
// do your cleaning/change to the detail subviews
[self removeFromSuperview]; // <-- clsoe the detail view
}
sdsds

Not getting Button click event in View added in Container View

I have created one common view for the application where I have added container view.
This is a confusing structure also very common question for SO but I am stuck since a day..
In my case hierarchy is as
ViewController's View -> (within that) commonview's container view -> (within that)I am adding one another view(base view) with two views.(view 1,view 2)
[self.commonView.containerView addSubview:baseView];
[baseView addSubview:view1];
[baseView addSubview:view2];
[self.view addSubview:self.commonView];
For that I am getting click events for view1's buttons but not for the view2's buttons.
I have checked userInteraction and all other common things. Now I am not getting what is wrong. Also this structure is already used in application so I wont be able to change it.I just have to resolve this issue.
Please check, if you have applied Gesture to any view then make cancelsTouchesInView property of gesture to NO. By default, it is TRUE, so button inside that view may not get touch as gesture cancels its inner view's touch.
If this is not the case, then apply Gesture to your button as this can happen when there are many hierachy of controls.
I think you should try by bringing your subview(i.e. view1) to front, since it is getting hide by another subview (i.e. view2)
You should first try with [self.view bringSubviewToFront:self.commonView.containerView ]; only.
if this works then it's Good otherwise try with this both shown as following
[self.view bringSubviewToFront:self.commonView.containerView ];
[self.view bringSubviewToFront:view1];
Hope this is helpful to you!
Enjoy Programming!
you need to add in respective order.
First add base view1 & view2 into baseView
[baseView addSubview:view1];
[baseView addSubview:view2];
and then add baseView into ContainerView
[self.commonView.containerView addSubview:baseView];
at last add commonView in mainView
[self.view addSubview:self.commonView];
Note :-
If you are adding the viewcontroller into the current viewcontroller then you need to write below code.
[self addChildViewController: yourviewController];
Then your button click will be worked.

How to reload my UIViewController on click of some buttons?

I'm developing an iPhone app, i have a UIViewController class where some coverflow animations are there and according to design i have some 5 buttons on top of my view each button have an IBAction method on clicking a button i need to display different set of coverflow with different data and the count of coverflow also changes from 1 button action to another. My problem is how can i refresh my page(reload) on click of these buttons, as i know calling explicitly viewDidLoad is a very bad idea, so how can i accomplish this? Any help is appreciated in advance thank you.
The easiest way to get a view to re-draw itself is to call [UIView setNeedsDisplay] or by using [UIView setNeedsDisplayInRect]. This method tells the view that it needs to re-draw itself and it's subviews. However you shouldn't always have to call this. There are other methods that can cause views to redraw themselves. For instance if you add or remove a view to the view hierarchy using [UIView addSubview:] or [UIView removeFromSuperview].
These methods cause the system to call your view's drawInRect: method (assuming you overrode it with custom drawing code). If you did not override this method for custom drawing then it should cause your subviews to receive the same message so that they can get a chance to redraw.
Additional information here. Specifically read the section labeled "The View Drawing Cycle".
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html
Why do you want to reload the page?, if it is because you are changing your view and you want it to be redrawn, you can call:
setNeedsDisplay();
But that is often not needed, as whenever a views bounds change that is called automatically for you.
If there's too much other stuff rather than refreshing/reloading the part of your interest in your viewDidLoad method, you can extract the refreshing part into a private method, say methodA. Every time you click your button, you call that methodA. However I think methodA still need call setNeedsDisplay or re-draw the the part of your interest. Hope it helps.
If your point is to refresh "UIViewController", then:
[self viewDidLoad];
Source: How to refresh UIViewController programmatically?
To Refresh your UIViewController, When Clicking the button >>
Add the below code into your UIButton method,
-(IBAction)button:(id)sender{
[self viewDidLoad];
[self viewWillAppear:YES];
}
Now Build and Run Your Code.
Use function viewWillAppear().This function is called everytime the view is called.

Remove a UITextField from a view

how can I remove a UITextField from a View? I've assigned a tag to the UITextfield and I search it from the subviews in the main view. But when I found it, it do a strange thing: doesn't execute the after instruction, or exactly it execute the instruction, but do nothing. Instead if I hidden it, all works fine.
Why won't you use removeFromSuperview selector?
You should by able to completely remove the UITextField by calling - (void)removeFromSuperview on the main thread.
You can remove your views by using removeFromSuperview
and should be like ---> [myTextView removeFromSuperview]
I think there might be other views over UITextView.Since
[myTextView removeFromSuperview];
should work fine.You can remove any of the view which is in SuperView.
So, please make sure that UITextView is still there ....or from somewhere else still textView is allocated & added to subivew.
Either you can set the Different BackGround Colors for TextView so that you can easily trace whether what is removed from SuperView.

cocoa touch - problem updating subviews!

I have this UIViewController which contains an UIScrollView.
In the viewDidLoad method of the first controller, I create some UIViewControllers(defined through a NIB with an UILabel and an UIImageView inside,linking to IBOutlets done correctly), then I add them to the UIScrollView.
Problem is, I can see my views and scroll through them using the scroll view, but I seem to be unable to modify the text of the label and the content of the imageView using the methods [[UIViewController UILabel] setText] or [[UIViewController UIImageView] setImage]
I know it sound like a stupid question, but I can't get past it.
Any idea why?
Thank you guys.
You need to tag the Label and ImageView.
tag means assign a integer value to label and ImageView and you can access using the tag.
You can assign tags in xib.
Then you can access the view like this[someController viewWithTag:(int)];
I think I have resolved my problem. I was trying to modify the views before adding them to the UIScrollView.
If I call my methods after the [scrollview addSubView:myView], everything works fine.