0

我对iOS开发相当陌生,我无法围绕一个概念来思考..

我有两个UIViewController班级 -ViewControllerSecondViewController. 最初,类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
4

2 回答 2

2

的工作loadView是设置self.view一个视图。当您收到loadView消息时,self.view为 nil。您的工作是从某处获取视图(通过在代码中创建视图或加载 nib)并将该视图存储在self.view.

loadView方法必须self.view在返回之前设置为视图,并且在设置之前不得调用self.viewgetter self.view。试试这个:

-(void)loadView {
    NSLog(@"loadView Called");
    self.view = [[ViewClass alloc] init];
}
于 2013-10-04T06:08:52.107 回答
0

还请在处理 UIViewController 时在 ViewDidLoad 中进行初始化,这将是一个很好的做法。

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self)
    {
        NSLog(@"init of ViewController called");

    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"view did load called");
  self.secondViewController = [[SecondViewController alloc] init];
}

也改变 loadView 方法,如 rob mayoff 解释的那样。

于 2013-10-04T06:16:54.413 回答