subview on main view(another view) by addSubView
非参考のお願い ... 内容不正確のため
非参考のお願い ... 内容不正確のため
非参考のお願い ... 内容不正確のため
subview on main view
will do
Set components on Main.storyboard as you want
Main.storyboard respond to TopViewController(.m | .h)
Create SubViewController Files
In this article
SampleCustomViewController(.m | .h | .xib)
will be created.
create from new File
- focus on group you want to create module
- click ‘File …’
select module
iOS > Source > Cocoa Touch Class
input items
- input class name (in this, SampleCustomViewController)
- SubClass is UIViewController
- check … Also create XIB file
- device is iPad
- Language is Objective-C
Code
SampleCustomViewController.h
#import <UIKit/UIKit.h> @interface SampleCustomViewController : UIViewController // creates view instance +(instancetype) create; @end
SampleCustomViewController.m
#import "SampleCustomViewController.h" @interface SampleCustomViewController () @end @implementation SampleCustomViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } + (instancetype) create { UINib *nib = [UINib nibWithNibName:@"SampleCustomViewController" bundle:nil]; SampleCustomViewController *viewController = [nib instantiateWithOwner:self options:nil][0]; return viewController; } @end
TopViewController.h
#import <UIKit/UIKit.h> @interface TopViewController : UIViewController @end
TopViewController.m
#import "TopViewController.h" // import subview SampleCustomView #import "SampleCustomViewController.h" @interface TopViewController () @end @implementation TopViewController - (void)viewDidLoad { [super viewDidLoad]; [self init]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (instancetype) init { self = [super init]; if(self) { SampleCustomViewController *sampleCustom = [SampleCustomViewController create]; // addSubView [self.view addSubview:sampleCustom]; } return self; } @end
If you’re stacked to error like below…
NSUnknownKeyException setValue:forUndefinedKey: this class is not key value coding-compliant for the key view.
- open subview’s xib
- click File’s Owner
- check CustomClass > Class … is not Empty
- if empty, fill the respond class(in this article, SampleCustomViewController)
Referred to
[iPhone] this class is not key value coding-com... | Apple Support Communities