i have success show subview from another NIB, i have 2 button and 1 UIView on MainView, when i klick button 1 will show NIB1 on UIView, and then i klick button 2 will show NIB2 on UIView, but when i klick again button 1 UIView not show NIB1 again, but still NIB 2, this is my Code :
Updated Solved
On .h file
IBOutlet UIView *bottomView;
On .m file
-(IBAction) handleButton1{
for (UIView *t in bottomView.subviews) {
[t removeFromSuperview];
}
if (![nib1 isViewLoaded]) {
nib1 = [[NIB1 alloc] initWithNibName:#"NIB1" bundle:nil];
}
[bottomView addSubview:nib1.view];
}
-(IBAction) handleButton2{
for (UIView *t in bottomView.subviews) {
[t removeFromSuperview];
}
if (![nib2 isViewLoaded]) {
nib2 = [[NIB2 alloc] initWithNibName:#"NIB2" bundle:nil];
}
[bottomView addSubview:nib2.view];
}
How i can refresh UIView with another NIB on my code ?
Thanks,
I have solve this case, i remove all subview on MainView from this http://stackoverflow.com/questions/3915729/refreshing-cell-content-after-deleting-cells
use :
for (UIView *t in bottomView.subviews) {
[t removeFromSuperview];
}
I hope this code help anyone :).
You have to remove the first view view [theViewYouDontWantToShow removeFromSuperview].
Or you can use [bottomView bringSubviewToFront:nib1/2.view].
What was appending here is only that you added a view (this works), added the other view on top of the first one (this works too), but then you added the first one again. The view doesn't change since it's already there.
Related
I have a UIView designed in XIB file. Basically with a navigation bar and a button to close the UIVIew. I want to show this UIView in several UIViewControllers across the app.
Below is the code I have imp[emnted so far.However the UIView doesn't show up.
My ViewControllers are designed in storyboard however I needed a simple UIView that is why I created a new custom subclass of UIView alongwith a xib file of the same name.The File's Owner is also set to custom subclass. What else is required here?
#interface BottomSlidingView : UIView //this is my UIView
{
}
#implementation BottomSlidingView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUserInteractionEnabled:YES];
NSLog(#"Bottom View Loaded"); //I get this log entry but the view doesn't showup.
}
return self;
}
- (BOOL)loadMyNibFile {
if (![[NSBundle mainBundle] loadNibNamed:#"BottomSlidingView" owner:self options:nil]) {
return NO;
}
return YES;
}
And this is how I call the custom UIView in my UIViewController.
-(void)shareButton:(UIBarButtonItem *)button
{
NSLog(#"share button clicked");
BottomSlidingView *bsv = [[BottomSlidingView alloc] initWithFrame:CGRectMake(20, 480, 280, 420)];
[bsv loadMyNibFile];
[self.view insertSubview:bsv belowSubview:self.optionsToolBar]; //this toolbar is a subview that I am adding to this view controller.
}
I see a couple problems with your approach:
Create the view instance from a nib file, rather than instantiating it with initWithFrame:. For example (if the custom view is the only root-level object in the nib file):
+ (instancetype)bottomSlidingView
{
return [[[UINib nibWithNibName:#"BottomSlidingView" bundle:nil] instantiateWithOwner:nil options:nil] lastObject];
}
Implement awakeFromNib or initWithCoder: instead of initWithFrame: (which isn't called when loading from nibs) in your UIView subclass.
In your view controller, you would use something like the following to create the view:
- (void)shareButtonPressed:(id)sender
{
BottomSlidingView *slidingView = [BottomSlidingView bottomSlidingView];
[slidingView setFrame:CGRectMake(20, 480, 280, 420)];
[[self view] insertSubview:slidingView belowSubview:[self optionsToolBar]];
}
Hope that helps.
Where ever you want to reuse a UIView of a UIViewController in another view controller you need to use this....
YourViewController * child = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];//View controller of view , you want to reuse
[self.view addSubView:child.YourView]; //YourView = Name of view, you want to reuse
[self addChildViewController:child];
[child release];
After adding childViewController All IBActions will be work in child.
And after removing this view You have to remove YourViewController from parentViewController other wise there will be memory issues.......
So i'm adding UIView with UIScrollView in it to another view by pressing segment tool:
UIView.h
#interface gettingELORolesViewController : UIViewController{
UIViewController *currentController;
NSMutableArray *viewControllers;
}
- (IBAction)SegmentToggle:(UISegmentedControl *)sender;
#property (strong, nonatomic) IBOutlet UIView *containerView;
UIView.m
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Storyboard_iPhone"
bundle:nil];
firstView *FirstView = [sb instantiateViewControllerWithIdentifier:#"firstOne"];
secondView *SecondView = [sb instantiateViewControllerWithIdentifier:#"secondOne"];
viewControllers = [NSMutableArray arrayWithObjects:FirstView,SecondView,nil];
currentController = FirstView;
[containerView addSubview:FirstView.view];//containerView = self.view
and then depends on what segment is chosen, it shows different view:
- (IBAction)SegmentToggle:(UISegmentedControl *)sender {
UIViewController *selectedView=nil;
if (sender.selectedSegmentIndex==0) {
selectedView= [viewControllers objectAtIndex:0]; // retrieve object at index 0 from viewControllers array
}
else if(sender.selectedSegmentIndex==1){
selectedView= [viewControllers objectAtIndex:1]; // retrieve object at index 1 from viewControllers array
}
[currentController.view removeFromSuperview]; // remove Current displaying view from superView
currentController=selectedView; // make selected View to be the current View
[containerView addSubview:selectedView.view]; // Add newly selected view on container View
}
secondView is tableViewController and everything is working great when i choose second segment. When i choose first one it shows up but scroll doesn't work and i don't know why.
firstView.m viewDidLoad method looks like this:
-(void)viewDidLoad{
[super viewDidLoad];
mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[mainScrollView setContentSize:CGSizeMake(320, 600)];
[mainScrollView setScrollEnabled:YES];
}
Paging is enabled in storyBoard. And it hooked up with .h file of firstView.
Please tell me what am i doing wrong.
When you call -SegmentToggle:,
you create a local variable called selectedView
you assign a view controller to selectedView depending on what segment is selected
the method ends, selectedView goes out of scope
In short, you're not doing anything. You should, at least, replace a view in the current view hierarchy with the view of the selected controller (and maybe stash a reference to the selected controller in an instance variable for later lookup?)
But taking a step back, it looks like you're trying to create a view controller that manages other view controllers. There are now very specific rules around that as of iOS 5. Please watch the WWDC 2011 video called "Implementing UIViewController Containment" for more information and examples.
I have UIView in my storyboard with outlet mynewView and i want it display with totally different content. And do it many times.
My UIView contains many objects, i load it from internet. But every time i do
NSArray *viewsToRemove = [view1 mynewView];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
//Let's try to remove all possible views..
viewsToRemove = [self.view subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
//here i create mynewView
mynewView = [self createnewRandomView];
//And finally add the View we have created.
[self.view addSubview:mynewView];
i monitor the memory consumption and i see it grows every time i add new UIView. Even when i remove all superviews. I guess they remain in memory.
Before i change the view i remove from superview all possible views, but they still stay in memory.
Xcode is set to use ARC so i can't release it.
Is there any good way to reuse a UIView?
Thanks
[self.view addSubview:mynewView];
Your newView was added as subview of self.view.
Your above code does not use for remove yourVewView in self.view.
Please try this code before adding new view:
for (UIView *view in self.view) {
if([view isKindOfClass:[YourNewViewClassName class]])
{
[view removeFromSuperView];
break;
}
}
As you remove the subviews, set them to nil as you go, like so:
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
v = nil;
}
For more information, see for example Arc: Setting references to nil, for multiple buttons
I have a ViewController with xib,.h,.m files. Now i want to display it as a popUpView in another viewController. so I did like this...
Second *view2 = [[Second alloc] initWithNibName:#"Second" bundle:nil];
[view2.view setFrame:CGRectMake(20, 20, 280, 400)];
{self.view addSubview:view2.view];
but the problem here I am able to set frame randomly, but the viewController contains a textField and button. They are not getting added at random positions on the view like some textField comes out of the frame of view added.
After some googling i find this, but this not enough to solve my issue.
Adding a ViewConroller's View as subview programmatically
How to solve it...
It seems that you are changing the frame of the view in your view controller but not handling the repositioning of the elements inside the view. What you can do is override the layoutSubviews method in the view so that when the frame changes the text fields reposition themselves.
You will have to create an actual UIView subclass which will layout the text fields,
#interface SecondView : UIView{
}
#implementation SecondView{
- (void) layoutSubviews{
//self.frame would be to most up-to-date frame that you just set
}
}
Your UIViewController will just create the view like so,
#interface Second : UIViewController{
}
#implementation Second{
- (void) loadView{
self.view = [[[SecondView alloc] init] autorelease];
}
}
In my application, I am trying to remove all existing subviews and add a new one created in Interface Builder. However, I don't seem to be able to connect the view.
When a button clicks, the following function is executed (inside a subclass of UIViewController):
// Display a list of settings to choose from
- (void) settings
{
SettingsRootController *settingsController = [[SettingsRootController alloc] initWithNibName:#"SettingsRootController" bundle:nil];
_settingsController = settingsController;
for (UIView *view in self.view.subviews)
{
[view removeFromSuperview];
}
[self.view addSubview:_settingsController.view2];
int a = [self.view.subviews count];
[self.view setNeedsDisplay];
......
}
#import <UIKit/UIKit.h>
#interface SettingsRootController : UIViewController
{
IBOutlet UIView *_view2;
}
#property(nonatomic, retain) IBOutlet UIView *view2;
Inside the Interface Builder, I created a new View-based xib. Set file owner to SettingsRootController. Randomly drag a UITextView into the xib. Connect the UITextView to view and view2 in SettingsRootController.
However, if the above line is:
[self.view addSubview:_settingsController.view2];
a would always be 0, and thus the new screen is empty.
But if change to:
[self.view addSubview:_settingsController.view];
I could see the UITextView.
When you create a view controller, its view will not be created at initialisation time. The view will be created at first access of the view property. Thats why it works when you use _settingsController.view. But when you access view2 the view will not be loaded. You could write a custom getter method like:
-(UIView*) view2 {
_view2 = self.view;
return _view2;
}
The instance var declaration is different from the property declaration! With and without underscore. Maybe thats why it doesn't work.