[英]How to load .xib UIviews on ViewController in ios
嗨,我是ios的新手,在我的應用中,我使用.xib文件加載UIView
當我單擊第一個按鈕時,我想加載FirstView並刪除otherviews當我單擊第二個按鈕時,我想加載SecondView並刪除otherviews,當我單擊第三個按鈕時,我想加載ThirdView並刪除otherviews
#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
嘗試這段代碼:我已經為view編寫了代碼,它將在添加新視圖之前刪除先前的視圖。
#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
1)將視圖設為全局
FirstView * test1;
SecondView * test2;
ThirdView * test3;
隨時從超級視圖中刪除:
[test1 removeFromSuperView];
2)添加標簽以查看
test1.tag = 10;
使用標簽值刪除視圖:
[(UIView*)[rightView viewWithTag:10] removeFromSuperview];
使用此代碼。 它包含一個loadXib:
調用,該調用從具有給定名稱的筆尖加載視圖並返回視圖。
@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
以下是您需要的方法。
/ *不帶標簽* /
- (void)removeSubviewsExpectView:(id)viewClass {
for (UIView *subView in self.view.subviews) {
if (![subView isKindOfClass:[viewClass class]]) {
[subView removeFromSuperview];
}
}
}
/ *帶標簽* /
- (void)removeSubviewsExpectView:(int)viewTag {
for (UIView *subView in self.view.subviews) {
if (subView.tag != viewTag) {
[subView removeFromSuperview];
}
}
}
希望這可以幫助你。
首先,您在NSBundle中創建UiView
IBOutlets ,然后選擇此方法
- (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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.