【发布时间】:2012-05-03 05:32:08
【问题描述】:
处理故事板。
我创建了视图控制器(故事板),然后在内容中间从 xib 文件添加了子视图。
我想将 xib (UIView) 作为子视图添加到 ViewController 中并发送带有数据的对象并将该数据打印到标签中,但我不知道如何。
这是我的代码。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
UIView *subView;
}
@property (strong, nonatomic) IBOutlet UIView *subView; //connected over IB
@end
ViewController.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;
}
【问题讨论】:
-
我创建了新的 UIVIewController (OfferLocation) 而不是 UIView,然后只编辑这个新视图控制器的顶部并作为子视图添加到 ViewController.h 的中间。那是对的吗? OfferLocation (UIVIewController) 是:dl.dropbox.com/u/77033905/subview%20ss.png 我使用绿色部分作为子视图。
-
这不太好,我想出正确的方法。在 IB 中,我刚刚删除了默认视图并从对象库中添加了 UIView,现在一切正常。我有人在这个问题上需要帮助,请随时提出,我会提供帮助。
标签: objective-c xcode storyboard subview