我对iOS开发相当陌生,我无法围绕一个概念来思考..
我有两个UIViewController
班级 -ViewController
和SecondViewController
. 最初,类xib
文件ViewController
被加载,其中仅包含一个UIButton
. 当这个按钮被点击时,SecondViewController
它开始发挥作用,它需要加载一个由我的UIView
类创建的视图,名为ViewClass
. 仅ViewClass
包含一个.UIButton
单击 ViewController xib 上的按钮时,我得到的只是一个黑色的空白屏幕,我不明白为什么 loadView 和 viewDidLoad 方法被调用了这么多次..(输出如下所示)!!
这是我的代码..
视图控制器.m
@implementation ViewController
@synthesize secondViewController = _secondViewController;
-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self)
{
NSLog(@"init of ViewController called");
self.secondViewController = [[SecondViewController alloc] init];
}
return self;
}
- (IBAction)buttonPressed:(id)sender {
[self presentViewController:self.secondViewController animated:YES completion:NULL];
NSLog(@"presented");
}
第二视图控制器.m
-(void)loadView
{
NSLog(@"loadView Called");
self.myView = [[ViewClass alloc] init];
[self.view addSubview:self.myView];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSLog(@"init of SVC called");
}
return self;
}
-(void)viewWillAppear:(BOOL)animated
{
NSLog(@"view WIll appear called");
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"view did load called");
}
视图类.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(@"init OF View Called");
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.text = @"Heyy";
[self addSubview:button];
}
return self;
}
这就是我在控制台上得到的..
2013-10-04 11:03:08.454 MVCPractice1[30957:a0b] Cannot find executable for CFBundle 0xa894b30 </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk/System/Library/AccessibilityBundles/CertUIFramework.axbundle> (not loaded)
2013-10-04 11:03:08.503 MVCPractice1[30957:a0b] init of ViewController called
2013-10-04 11:03:08.504 MVCPractice1[30957:a0b] init of SVC called
2013-10-04 11:03:09.773 MVCPractice1[30957:a0b] loadView Called
2013-10-04 11:03:09.774 MVCPractice1[30957:a0b] init OF View Called
2013-10-04 11:03:09.775 MVCPractice1[30957:a0b] view did load called
2013-10-04 11:03:09.776 MVCPractice1[30957:a0b] loadView Called
2013-10-04 11:03:09.777 MVCPractice1[30957:a0b] init OF View Called
2013-10-04 11:03:09.778 MVCPractice1[30957:a0b] view did load called
2013-10-04 11:03:09.779 MVCPractice1[30957:a0b] loadView Called
2013-10-04 11:03:09.779 MVCPractice1[30957:a0b] init OF View Called
2013-10-04 11:03:09.780 MVCPractice1[30957:a0b] view did load called
2013-10-04 11:03:09.781 MVCPractice1[30957:a0b] loadView Called
2013-10-04 11:03:09.781 MVCPractice1[30957:a0b] init OF View Called
2013-10-04 11:03:09.782 MVCPractice1[30957:a0b] view did load called
2013-10-04 11:03:09.783 MVCPractice1[30957:a0b] view WIll appear called
2013-10-04 11:03:09.783 MVCPractice1[30957:a0b] loadView Called
2013-10-04 11:03:09.784 MVCPractice1[30957:a0b] init OF View Called
2013-10-04 11:03:09.785 MVCPractice1[30957:a0b] view did load called
2013-10-04 11:03:09.785 MVCPractice1[30957:a0b] loadView Called
2013-10-04 11:03:09.786 MVCPractice1[30957:a0b] init OF View Called
2013-10-04 11:03:09.787 MVCPractice1[30957:a0b] view did load called
2013-10-04 11:03:09.787 MVCPractice1[30957:a0b] presented