2

Hi i am very new for ios and in my app i am loading UIView using .xib files

when i click first buttons i want to load FirstView and remove otherviews when i click second button i want to load SecondView and remove otherviews when i click third button i want to load ThirdView and remove otherviews

mycode:-

#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize leftView,rightView; - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)FirstAction:(id)sender { FirstView * test1 = [[FirstView alloc]initWithFrame:CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height)]; test1.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [rightView addSubview:test1]; } - (IBAction)SecondAction:(id)sender { SecondView * test2 = [[SecondView alloc]initWithFrame:CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height)]; test2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [rightView addSubview:test2]; } - (IBAction)ThirdAction:(id)sender { ThirdView * test3 = [[ThirdView alloc]initWithFrame:CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height)]; test3.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [rightView addSubview:test3]; } @end 
4
  • Try the solution provided by me Commented Feb 15, 2016 at 9:19
  • You said you are loading views using xib files. But you are creating them programmatically in your action methods. Do you have the xib files in your project? Commented Feb 15, 2016 at 9:26
  • yes i am having xib files in my project this is my sample app Commented Feb 15, 2016 at 9:29
  • drive.google.com/file/d/0B05NQoG9RAmwd2NnU3AzVlRzdjg/… Commented Feb 15, 2016 at 9:29

5 Answers 5

3

Try this piece of code: I have written code for view which will remove previous view before adding the new one.

#import "ViewController.h" @interface ViewController () { FirstView * test1; SecondView * test2; ThirdView * test3; } @end @implementation ViewController @synthesize leftView,rightView; - (void)viewDidLoad { [super viewDidLoad]; test1 = [[[NSBundle mainBundle] loadNibNamed:@"FirstView" owner:self options:nil] objectAtIndex:0]; test2 = [[[NSBundle mainBundle] loadNibNamed:@"SecondView" owner:self options:nil] objectAtIndex:0]; test3 = [[[NSBundle mainBundle] loadNibNamed:@"ThirdView" owner:self options:nil] objectAtIndex:0]; } - (IBAction)FirstAction:(id)sender { test1.frame = CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height); test1.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [self removePreviousView:test1 FromSuperView:rightView]; [rightView addSubview:test1]; } - (IBAction)SecondAction:(id)sender { test2.frame = CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height); test2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [self removePreviousView:test2 FromSuperView:rightView]; [rightView addSubview:test2]; } - (IBAction)ThirdAction:(id)sender { test3.frame = CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height); test3.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [self removePreviousView:test3 FromSuperView:rightView]; [rightView addSubview:test3]; } - (void)removePreviousView:(UIView*)previousView FromSuperView:(UIView*)view{ for (UIView *subView in view.subviews) { if (![subView isKindOfClass:[previousView class]]) { [subView removeFromSuperview]; } } } @end 
Sign up to request clarification or add additional context in comments.

6 Comments

please modified if there any mistacks ok brother?
did u check? brother
yeah I have checked you are doing several things in wrong way. Like you have set classes of the views in nib but you are not loading your view from nib.
can u send me that sample project if u modified please?
I have updated the code. Replace it with the code in your UIViewController and check out the results.
|
1

1) Make view global

FirstView * test1; SecondView * test2; ThirdView * test3; 

Remove from superview whenever you want:

[test1 removeFromSuperView]; 

2) Add tag to view

test1.tag = 10; 

Remove view using tag value:

[(UIView*)[rightView viewWithTag:10] removeFromSuperview]; 

5 Comments

sorry for any type in snippet.
Hi @KDeogharkar not adding xib UIview
so you are saying that view is not displaying?
first,second,third view is having xib?
1

Use this code. It contains a loadXib: call that loads the view from the nib with the given name and returns it.

@interface ViewController () { FirstView * test1; SecondView * test2; ThirdView * test3; } @end @implementation ViewController @synthesize leftView,rightView; - (void)viewDidLoad { [super viewDidLoad]; } -(UIView*)loadXib:(NSString *)name { UINib *nib = [UINib nibWithNibName:name bundle:nil]; if (nib != nil) { NSArray *items = [nib instantiateWithOwner:self options:nil]; if (items != nil && items.count == 1) { return (UIView*)items[0]; } } return nil; } - (IBAction)FirstAction:(id)sender { // test1 = [[FirstView alloc]initWithFrame:CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height)]; test1 = (FirstView*)[self loadXib:@"FirstView"]; if (test1 != nil) { test1.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [rightView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; [rightView addSubview:test1]; } } - (IBAction)SecondAction:(id)sender { // test2 = [[SecondView alloc]initWithFrame:CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height)]; test2 = (SecondView*)[self loadXib:@"SecondView"]; if (test2 != nil) { test2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [rightView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; [rightView addSubview:test2]; } } - (IBAction)ThirdAction:(id)sender { // test3 = [[ThirdView alloc]initWithFrame:CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height)]; test3 = (ThirdView*)[self loadXib:@"ThirdView"]; if (test3 != nil ) { test3.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [rightView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; [rightView addSubview:test3]; } } @end 

Comments

0

Below is method for your need.

/* Without tag */

- (void)removeSubviewsExpectView:(id)viewClass { for (UIView *subView in self.view.subviews) { if (![subView isKindOfClass:[viewClass class]]) { [subView removeFromSuperview]; } } } 

/* With tag */

- (void)removeSubviewsExpectView:(int)viewTag { for (UIView *subView in self.view.subviews) { if (subView.tag != viewTag) { [subView removeFromSuperview]; } } } 

Hope this helps you out.

Comments

0

Firstly you create UiView IBOutlets in the NSBundle then you choose this method

- (IBAction)FirstAction:(id)sender { NSArray *viewsToRemove = [rightView subviews]; for (UIView *v in viewsToRemove) { [v removeFromSuperview]; } UIView *firstViewUIView = [[[NSBundle mainBundle] loadNibNamed:@"Test1" owner:self options:nil] firstObject]; [rightView containerView addSubview:firstViewUIView]; } - (IBAction)SecondAction:(id)sender { NSArray *viewsToRemove = [rightView subviews]; for (UIView *v in viewsToRemove) { [v removeFromSuperview]; } UIView *secondView = [[[NSBundle mainBundle] loadNibNamed:@"Test2" owner:self options:nil] firstObject]; [rightView containerView addSubview:seconView]; } - (IBAction)ThirdAction:(id)sender { NSArray *viewsToRemove = [rightView subviews]; for (UIView *v in viewsToRemove) { [v removeFromSuperview]; } UIView *thirdView = [[[NSBundle mainBundle] loadNibNamed:@"Test3" owner:self options:nil] firstObject]; [rightView containerView addSubview:thirdView]; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.