在故事板上工作。
我创建了视图控制器(故事板),然后在内容中间从 xib 文件添加了子视图。
我想将 xib (UIView) 作为子视图添加到 ViewController 中,并发送带有数据的对象并将该数据打印到标签中,但我不知道如何。
这是我的代码。
视图控制器.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
UIView *subView;
}
@property (strong, nonatomic) IBOutlet UIView *subView; //connected over IB
@end
视图控制器.m
#import "OfferViewController.h"
#import "OfferLocation.h"
@interface OfferViewController ()
@end
@implementation OfferViewController
@synthesize subView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
OfferLocation *location = [[OfferLocation alloc] initWithFrame:CGRectZero];
[self.subView addSubview:location];
}
...
@end
这是子视图: OfferLocation.h
#import <UIKit/UIKit.h>
@interface OfferLocation : UIView{
UIView *view;
UILabel *locationLabel;// here is that label taht I want to print into
}
@property (nonatomic, retain) IBOutlet UIView *view;// connected over IB
@property (nonatomic, retain) IBOutlet UILabel *locationLabel;
@end
OfferLocation.m
#import "OfferLocation.h"
@implementation OfferLocation
@synthesize view, locationLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
locationLabel.text = @"some text"; //this is not working
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"OfferLocation" owner:self options:nil];
UIView *tempView = [subviewArray objectAtIndex:0];
[self addSubview:tempView];
}
return self;
}