将UIView添加到ComposeViewController中

时间:2014-03-31 02:07:34

标签: ios uiviewcontroller

我有一个UIView及其笔尖。我还有一个UIViewController及其笔尖。我想将UIView添加到UIViewController中。我该怎么做?

编辑: 我原来的问题不够明确,因为我对iOS不太了解。从现在开始,我想要的是创建一个UIView子类及其可以嵌入其他UIView / UIViewControllers的nib。给出原始问题的答案肯定会奏效。

3 个答案:

答案 0 :(得分:0)

例如:

@interface Controller {}
@property(retain) IBOutlet UIView *extraView;
@end

…

- (void) viewDidLoad // or anywhere else
{
    [[NSBundle mainBundle] loadNibNamed:@"extras" owner:self options:nil];
    NSAssert(extraView != nil, @"The extra view failed to load.");
    [[self view] addSubview:extraView];
}

或者只是添加行[self.view addSubview:extraView];如果你已经命名它,你需要的地方

编辑:

NSArray *myNibsArray = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil];
Y *extraView= [myNibsArray objectAtIndex:0];
[[self view] addSubview:extraView];

答案 1 :(得分:0)

试试这个。

NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
Y *extraView= [nibArray objectAtIndex:0];
[self.view addSubview:extraView];

答案 2 :(得分:0)

实际上有多种方法可以创建可嵌入的UIView。这是我采用的方法:

WidgetView是我想嵌入其他UIView / UIController的UIVIew,

@interface WidgetView : UIView
@property (weak, nonatomic) IBOutlet UILabel *label;
@end


@implementation WidgetView
// This method gets called whenever you load a nib during runtime
- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        UINib *nib = [UINib nibWithNibName:@"WidgetView" bundle:nil];
        NSArray *views = [nib instantiateWithOwner:self options:nil];
        [self addSubview:views[0]];
        self.label.text = @"Hell World.";
    }
    return self;
}
@end

//MyController.m
@interface MyController ()
@property (weak, nonatomic) IBOutlet WidgetView *customView;
@end

@implementation MyController
...
@end

然后在MyController.xib中,将UIVIew拖到nib中,并将CustomClass设置为WidgetView并将插座拖到MyController中