以編程方式創建您的看法,用你的UIViewController
- (void)viewDidLoad
{
[super viewDidLoad];
for(int i=0;i<50;i++){
//provide some initial frame, set the correct frames in viewWillLayoutSubviews:
CGRect frame = CGRectMake(0,i*10;100;5);
//create a new UIView, use your own UIView subclass here if you have one
UIView *view = [[UIView alloc] initWithFrame:frame];
//set it's backgroundColor in case you are copy&pasting this code to try it out, so you see that there are actually views added ;)
view.backgroundColor = [UIColor blueColor];
//add it to the viewController's view
[self.view addSubview:view];
//you might consider creating an NSArray to store references to the views for easier access in other parts of the code
}
}
viewDidLoad:
要創建一個像這裏所描述的,你在故事板使用的東西設計了一個觀點:https://stackoverflow.com/a/13390131/3659846
MyViewController *myViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyScene"];
[self.view addSubView:myViewController.theViewToAdd];
要創建一個nib
文件的視圖,使用這裏描述的方法:https://stackoverflow.com/a/11836614/3659846
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"yourNib" owner:nil options:nil];
UIView *view = [nibContents lastObject]; //assuming the nib contains only one view
[self.view addSubview:view];
如果您使用視圖50次,您確定要爲每個視圖使用IBOutlets?您可以創建一個自定義UIView,以編程方式在頁面上插入。 – LyricalPanda
我其實想編程50。我會怎麼做?我目前正在使用名爲CollapseClick的插件,我只知道如何通過上面的代碼添加視圖。我現在將研究自定義類。感謝您的意見。 –