我对 iOS 开发很陌生,因此对 iOS 的概念storyboard
也很陌生。由于这似乎是“新事物”,每个人都应该使用,我想我也可以尝试一下。
我在这里有一个项目,用一个Foo.xib
文件创建。
该xib
文件包含几个view
对象。
然后我有一个班级Foo.h
和Foo.m
班级,内容如下:
Foo.h
#import <UIKit/UIKit.h>
@interface Foo : UIView
@property (strong, nonatomic) IBOutlet UIView *view01;
@property (strong, nonatomic) IBOutlet UIView *view02;
@property (strong, nonatomic) IBOutlet UIView *view03;
@property (strong, nonatomic) IBOutlet UIView *view04;
- (NSUInteger)viewCount;
@end
Foo.m
#import "Foo.h"
@interface Foo()
@property (nonatomic, strong) NSArray *views;
@end
@implementation Foo
- (id)init {
self = [super init];
if (self) {
self.views = [[NSBundle mainBundle] loadNibNamed:@"Foo" owner:self options:nil];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (NSUInteger)viewCount {
return [self.views count];
}
@end
然后我ViewController
会加载所有视图并使其可滚动,如下所示:
#import "ViewController.h"
#import "Foo.h"
@interface ViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) Foo *views;
@end
@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.views = [[Foo alloc] init];
CGSize fooSize = self.views.view01.bounds.size;
NSUInteger viewCount = [self.views viewCount];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, fooSize.width, fooSize.height)];
[self.scrollView setContentSize:CGSizeMake(viewCount*fooSize.width, fooSize.height)];
[self.scrollView setBounces:YES];
[self.scrollView setPagingEnabled:YES];
self.scrollView.delegate = self;
NSArray *views = @[ self.views.view01,
self.views.view02,
self.views.view03,
self.views.view04
];
for (int i=0; i<viewCount; i++) {
UIView *curView = views[i];
CGRect frame = curView.frame;
frame.origin.x = i*fooSize.width;
frame.origin.y = 0;
curView.frame = frame;
[self.scrollView addSubview:curView];
}
[self.view addSubview:self.scrollView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
但是,我不知道如何用故事板来实现这一点。在我看来,我必须有一个NavigationController
然后链接到Master View Controller
. 现在我必须ViewController
为每个视图添加一个新的?或者有没有办法ViewController
像我做“旧方式”一样将所有视图包含在一个视图中?