5

約 8 つのビューを持ち、ナビゲーション コントローラーを使用してナビゲートするアプリケーションを開発しています。最初のビューはメイン メニューです。

私が欲しいのは、ユーザーがホームボタンを押した場合に(各ビューの)メインビューにポップすることです(アプリはバックグラウンドに入りました)。

AppDelegate メソッドapplicationDidEnterBackgroundapplicationWillEnterForeground.

popToRootViewControllerAnimatedそして、ナビゲーションコントローラーから呼び出されるメソッドを知っています。

applicationDidEnterBackground で popToRootViewControllerAnimated を使用しようとしました。お気に入り:

[self.window.rootViewController.navigationController popToRootViewControllerAnimated:YES];

しかし、これはうまくいきません。

この仕事に最適な選択肢を教えてください。

4

4 に答える 4

10

私はあなたNSNotificationCenterがこのようにしようとすると思います:

applicationDidEnterBackgroundapplicationWillEnterForeground入れてこれを

[[NSNotificationCenter defaultCenter] postNotificationName:@"popToRoot" object:nil];

そしてrootViewController's viewDidLoad(アプリの起動時に常に表示される)にこれを追加します:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popToRootViewControllerAnimated) name:@"popToRoot" object:nil];

次に、にメソッドを作成しますrootViewController

- (void)popToRootViewControllerAnimated
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

アプリケーションが初めて起動するときはいつでも、NSNotificationCenter名前を初期化し、このpopToRootためのメソッドpopToRootViewControllerAnimatedを準備します。

そして、アプリケーションがバックグラウンドに移行すると、メソッドにNSNotificationCenterメッセージが渡さ@"popToRoot"れ、rootViewController's popToRootViewControllerAnimatedメソッドviewcontrollerにポップされますrootview

于 2013-09-11T07:12:23.833 に答える
2

このように試してみましたか:-

[self.navigationController popToRootViewControllerAnimated:YES];

ここで、navigationController 名を navigationController に置き換えます。

編集:-

AppDelegate.h ファイル内

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UINavigationController *navMain;
}

@property (nonatomic, retain) UINavigationController *navMain;

AppDelegate.m ファイル内

@synthesize navMain;

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.navMain = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = self.navMain;
    [self.window makeKeyAndVisible];
    return YES;
}

-(void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"applicationDidEnterBackground");
    [self.navMain popToRootViewControllerAnimated:YES];
}

編集したアンサーを試す

于 2013-09-11T06:55:35.047 に答える
0

AppDelegate クラスで UINavigationController のプロパティを作成します。applicationDidEnterBackground: メソッドで、UINavigationController プロパティを使用して popToRootViewController メソッドを呼び出します。あなたのプロパティ名がnavigationControllerであるとします

[self.navigationController popToRootViewControllerAnimated:YES];
于 2013-09-11T07:00:46.697 に答える
0

まず、rootviewcontroller が navigationController であるかどうかを確認する必要があります。self.window.rootViewController.navigationController は多くの場合 nil であるためです。なんで?これは、navigationController の navigationController が「nil」であるためです。ほとんどの場合、rootViewController を navigationController に設定します

第二に、アプリケーションが終了しようとしているときに、アニメーション化を行うべきではありません。アニメ化しないでやるべき

popToRootViewControllerAnimated:NO
于 2013-09-11T06:59:02.123 に答える