0

我正在尝试做一些不同的事情,混合 XIB 和 STORYBOARD。我的主要目标是为我的应用程序创建一个复杂的故事板,并使用 XIB 来查看比 iPhone 屏幕更大的视图,例如 iPhone 上的设置应用程序。

作为一个简单的例子,我使用 xib 接口创建了两个类视图 view1 和 view2。每个人都做简单的任务,比如显示许多标签“Hello World - View 1”和“Hello World - View 2”,直到覆盖正常 iPhone 屏幕面积的两倍。为此,XIB 上的视图被设置为“FreeForm”,并沿视图放置了 6 个标签。文件 .h 和 .m 写成:

查看.h

#import <UIKit/UIKit.h>

@interface View1 : UIView {
    UIView *view;

    UILabel *lab1;
    UILabel *lab2;
    UILabel *lab3;
    UILabel *lab4;
    UILabel *lab5;
    UILabel *lab6;
}
@property (nonatomic, retain) IBOutlet UIView *view;
@property (retain, nonatomic) IBOutlet UILabel *lab1;
@property (retain, nonatomic) IBOutlet UILabel *lab2;
@property (retain, nonatomic) IBOutlet UILabel *lab3;
@property (retain, nonatomic) IBOutlet UILabel *lab4;
@property (retain, nonatomic) IBOutlet UILabel *lab5;
@property (retain, nonatomic) IBOutlet UILabel *lab6;
@end

查看.m

#import "View1.h"

@implementation View1
@synthesize view, lab1, lab2, lab3, lab4, lab5, lab6;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [[NSBundle mainBundle] loadNibNamed:@"View1" owner:self options:nil];
        [self addSubview:self.view];
    }
    return self;
}
- (void) awakeFromNib
{
    [super awakeFromNib];    
    [self addSubview:self.view];
}
@end

之后,我使用 Storyboard 创建了一个名为 myViewController 的 ViewController,并在视图中放置了一个滚动视图。.h 和 .m 的代码如下所示:

视图控制器.h

#import <UIKit/UIKit.h>
@class View1, View2;

@interface ViewController : UIViewController {
    View1 *viewXIB1;
    View2 *viewXIB2;
}
@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;
- (IBAction)changeView1:(id)sender;
- (IBAction)changeView2:(id)sender;
@end

视图控制器.m

#import "ViewController.h"
#import "View1.h"
#import "View2.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.myScrollView.contentSize=CGSizeMake(320,755);    
    viewXIB1 = [[View1 alloc] initWithFrame:CGRectMake(0, 0, 320, 755)];
    viewXIB2 = [[View2 alloc] initWithFrame:CGRectMake(0, 0, 320, 755)];
    [self.myScrollView addSubview:viewXIB1];  
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)changeView1:(id)sender {
    [self.myScrollView addSubview:viewXIB1];
    [self.myScrollView setNeedsLayout];    
    [self.view reloadInputViews];
    [self reloadInputViews];   
}

- (IBAction)changeView2:(id)sender {
    [self.myScrollView addSubview:viewXIB2];
    [self.myScrollView setNeedsLayout];
}
@end

问题是代码上没有警告或错误,但不起作用。这段代码有什么问题???有人能帮我吗?

4

1 回答 1

0

一件事是错误的,您永远不会删除任何视图。代替

第一个IBAction

- (IBAction)changeView1:(id)sender {
    if (![self.myScrollView.subviews containsObject:viewXIB1]) {
        [viewXIB2 removeFromSuperView];
        [self.myScrollView addSubview:viewXIB1];
        [self.myScrollView setNeedsLayout];    
        [self.view reloadInputViews];
        [self reloadInputViews];
    }
}

第二个是模拟的IBAction

- (IBAction)changeView2:(id)sender {
    if (![self.myScrollView.subviews containsObject:viewXIB2]) {
        [viewXIB1 removeFromSuperView];
        [self.myScrollView addSubview:viewXIB2];
        [self.myScrollView setNeedsLayout];
    }
}

此外,您的自定义视图的初始化代码应该包含类似的内容。-

UIView *newView = [[[NSBundle mainBundle] loadNibNamed:@"View1" owner:self options:nil] objectAtIndex:0];
[self.view addSubview:newView];
于 2013-04-11T20:30:06.693 回答