简体   繁体   English

ios:如何从IB中的一个视图创建两个不同的视图?

[英]ios: How to create two differnent views from one view in IB?

I created a custom UIView in Intefacebuilder. 我在Intefacebuilder中创建了一个自定义UIView。 In my ViewController I want to create two instances of my UIViews. 我想在ViewController中创建两个UIView实例。

So here is some code: 所以这是一些代码:

@interface MeineFinanzenViewController : UIViewController <UIGestureRecognizerDelegate> {

UISwipeGestureRecognizer* swipeLeft;
UISwipeGestureRecognizer* swipeRight;

//... other stuff

SaldoView* ausgabenView;
SaldoView* einnahmenView;

}

//...other stuff
@property (strong,nonatomic) IBOutlet SaldoView* ausgabenView;
@property (strong,nonatomic) IBOutlet SaldoView* einnahmenView;
//...other stuff

So I connected the same view two times. 所以我两次连接了相同的视图。 Firstly to ausgabenView and secondly to einnahmenView. 首先是ausgabenView,其次是einnahmenView。 BUT after that the ausgabenView becomes the same object as einnahmenView. 但是之后,ausgabenView变成了与einnahmenView相同的对象。 Actually I would like to have two separated objects that are independent from each other. 实际上,我希望有两个彼此独立的独立对象。 Now I'm asking myself how to achieve this? 现在我问自己如何实现这一目标?

Any hints? 有什么提示吗?

You should load your view manually: 您应该手动加载视图:

- (SaldoView *)loadCustomView {

    NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"NibFileName" owner:nil options:nil];
    for (id object in nibViews) {
        if ([object isKindOfClass:[SaldoView class]]) {
            return (SaldoView *)object;
        }
    } 
    return nil;
}

And create two instance of views 并创建两个视图实例

    SaldoView* ausgabenView = [self loadCustomView];
    SaldoView* einnahmenView = [self loadCustomView];  

If you are creating your views by editing your nib files graphically - as it seems you are - then if you want two views, then you drag two views onto the editor from the library. 如果要通过图形化编辑nib文件来创建视图(看起来像是这样),则如果需要两个视图,则可以将两个视图从库中拖到编辑器中。 You then connect one to each outlet. 然后将一个连接到每个插座。

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

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