我對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
之內包含所有視圖,就像我以前的方式一樣?
如果我的答案已解決,請不要忘記將upvote/mark標記爲最佳答案你的問題。否則留下一些額外的評論/問題 –