0

私は 2 つのビュー コントローラーを持っています。1 つ目はストーリーボード (これはルート) で、2 つ目はニブレスです。ルート ビュー コントローラーのボタンを押すと、2 番目のコントローラーが呼び出されます。

ここに私の2番目のView Controllerのコードがあります:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UILabel *sampleLabel = [[UILabel alloc] initWithFrame: CGRectMake(0,0,100,100)];
        UIImageView * basketItem = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"B.jpg"]];
        [self.view addSubview:sampleLabel];
        [self.view addSubview:basketItem];
        NSLog(@"%@",self.view.subviews);
        sampleLabel.text = @"Main Menu";
    }
    return self;
}

self.view.sebviews クエリは、label オブジェクトと imageView オブジェクトの 2 つのオブジェクトが存在することを示していますが、実際には黒い画面しか表示されません。

移行方法はこちら

- (void)transitionToViewController:(UIViewController *)aViewController
  withOptions:(UIViewAnimationOptions)options
{
      aViewController.view.frame = self.containerView.bounds;
      [UIView transitionWithView:self.containerView
                  duration:0.65f
                   options:options
                animations:^{
                    [self.viewController.view removeFromSuperview];
                    [self.containerView addSubview:aViewController.view];
                }
                completion:^(BOOL finished){
                    self.viewController = aViewController;
                }];    
}
4

2 に答える 2

2

コードをに移動しますviewDidLoad。ここで、ビューがメモリにロードされているため、さらにカスタマイズできることを確認できます。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UILabel *sampleLabel = [[UILabel alloc] initWithFrame: CGRectMake(0,100,100,100)];
    UIImageView * basketItem = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"B.jpg"]];
    [self.view addSubview:sampleLabel];
    [self.view addSubview:basketItem];
    NSLog(@"%@",self.view.subviews);
    sampleLabel.text = @"Main Menu";    
}

ARCを使用していない場合は、メモリリークに注意してください。

ノート

これについては、Appleのドキュメントを読むことをお勧めします。あなたは物事がどのように機能するかを理解する必要があります。お役に立てば幸いです。

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html

編集

何が問題なのかわかりません。loadViewそれを機能させるには、次のように(in MenuViewController)メソッドをオーバーライドしてみてください。

- (void)loadView
{
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame];
    contentView.backgroundColor = [UIColor redColor]; // red color only for debug purposes
    self.view = contentView;
}

viewDidLoad私が書いたようにメソッドを残して、何が起こるかを見てください。

ビューコントローラを作成するときは、initメソッドのみを使用してください。

MenuViewController *vc = [[MenuViewController alloc] init];
于 2012-12-29T16:42:22.470 に答える
1

あなたUILabelframesize.width=0:

CGRectMake(0,100,0,100)

B.jpgがプロジェクトに追加されていない場合、あなたUIImageViewも空になります。

また、UIViewControllersecond に XIB がない場合は、- (id)init代わりにメソッドを使用して初期化し- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNilます。

于 2012-12-29T16:37:38.120 に答える