简体   繁体   English

使用情节提要创建和加载视图

[英]Creating and loading views with storyboard

I am quite new to iOS development and thus new to the concept of storyboard as well. 我对iOS开发非常陌生,因此对storyboard的概念也很storyboard As this seems to be the 'new thing', everyone should use, I thought I might give it a try as well. 因为这似乎是“新事物”,所以每个人都应该使用,我想我也可以尝试一下。

I got a project here, created with a Foo.xib file. 我在这里有一个使用Foo.xib文件创建的项目。

The xib file has several view objects included. xib文件包含多个view对象。

Then I have a class Foo.h and Foo.m class with following content: 然后,我有一个Foo.hFoo.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

In my ViewController I would then load all the views and make it scrollable, like this: 然后,在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

However, I have no clue, how to realize this with a storyboard. 但是,我不知道如何使用情节提要实现这一点。 It seems to me that I have to have a NavigationController which is then linked to the Master View Controller . 在我看来,我必须有一个NavigationController ,然后将其链接到Master View Controller And now I would have to add a new ViewController for each view? 现在我必须为每个视图添加一个新的ViewController吗? Or is there a way to include all views within one ViewController like I did 'the old way'? 还是有办法像我“旧方法”那样将所有视图包含在一个ViewController

There is a massive mis conception that when using a storyboard it limits you to what you can do. 有一个很大的误解,就是使用情节提要板会限制您只能做什么。 A storyboard is simply like an array of .xib files, it holds many screens in the one file so you can see the entire flow of you app in one place. 故事板就像一个.xib文件的数组,在一个文件中包含许多屏幕,因此您可以在一个地方查看应用程序的整个流程。 Inside a storyboard you can create a single viewController and assign a custom viewController class to it and then load / modify what ever you like inside the code of this viewController, as you have done above. 在情节提要板中,您可以创建一个单独的viewController并为其分配自定义viewController类,然后像上面所做的那样在此viewController的代码内加载/修改所需的内容。

However the benefit of using the storyboard is to have multiple viewController objects so you can design all the screens and navigation there were you can see it, aiding you in debugging and design work. 但是,使用情节提要的好处是拥有多个viewController对象,因此您可以设计所有的屏幕和可以看到的导航,从而帮助您进行调试和设计工作。

If you already have the app working without a storyboard and you simply want to use it because its new but keep the old style of coding, you are not going to see much of the benefits. 如果您已经在没有情节提要的情况下运行了该应用程序,并且只是想使用它,因为它是新的但保留了旧的编码风格,那么您将不会看到很多好处。 I would suggest following this example from the developer library on how to use a storyboard properly. 我建议遵循开发人员库中的此示例,了解如何正确使用情节提要。 After you complete this you will see the benefits of the new style and you can decide whether to do it in code or using the interface builder: http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/SecondiOSAppTutorial/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011318 完成此操作后,您将看到新样式的好处,并且可以决定是通过代码还是使用界面构建器来进行: http : //developer.apple.com/library/ios/#documentation/iPhone/Conceptual/ SecondiOSAppTutorial / Introduction / Introduction.html#// apple_ref / doc / uid / TP40011318

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM