0

標準をモーダルで提示しようとしてViewControllerいますが、その方法がわかりません。ビューコントローラには、最終的に却下アクションをトリガーするボタンがあります。そのため、ビューコントローラをで囲む必要はありませんNavigationController。また、.x​​ibsを使用せずに、これらすべてをプログラムで実行しています。

これが私が使用しているコードです:

- (void)viewDidAppear:(BOOL)animated {
    NSLog(@"view did appear within rootviewcontroller");
    WelcomeViewController *welcome = [[WelcomeViewController alloc] init];
    [self presentModalViewController:welcome animated:true];
    [welcome release];
}

問題は、WelcomeViewController'sビューを設定していないため、loadViewが実行されないことです。つまり、画面にコンテンツが描画されていません。

Appleを含め、私が見つけたすべての例では、.xibを使用してViewControllerを初期化するか、NavigationControllerを使用してRootViewControllerを追加するか、またはその両方を使用しています。私の理解では、これらのシナリオの両方でloadViewが自動的に呼び出されます。 http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW3

ビューはどこで構成しWelcomeViewController'sますか?alloc / initの直後ですか?WelcomeViewController'sinitメソッドの内部?

ありがとう!

4

2 に答える 2

3

WelcomeViewControllerのビューはどこで構成しますか?

loadViewサブクラスのメソッドをオーバーライドします。iOS用ViewControllerプログラミングガイドを参照してください。

于 2011-02-18T20:35:21.627 に答える
1

これは、NIBを使用せずにそれを実行する方法の簡単な例です。

AppDelegatedidFinishLaunchingWithOptions:で、カスタムView Controllerのインスタンスを作成し、それをウィンドウのサブビューとして追加します(かなり標準的です)。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    RootViewController *vc = [[RootViewController alloc] initWithNibName:nil bundle:nil];
    [self.window addSubview:vc.view];
    [self.window makeKeyAndVisible];
    return YES;
}

インスタンスを作成するときは、vcViewControllerの新しいインスタンスで呼び出される指定されたイニシャライザーを使用します。メソッド内でカスタム初期化を行うため、ペン先を指定しません。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self.view setBackgroundColor:[UIColor orangeColor]];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setNumberOfLines:2];
        [label setText:@"This is the vc view with an\norange background and a label"];
        [label setTextColor:[UIColor whiteColor]];
        [label setTextAlignment:UITextAlignmentCenter];
        [self.view addSubview:label];
        [label release];
    }
    return self;
}
于 2011-02-18T20:55:06.897 に答える