我如何回忆didFinishLaunchingWithOptions中的对象?

时间:2013-06-09 09:50:53

标签: ios

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    CGSize result = [[UIScreen mainScreen] bounds].size;
    if(result.height == 480)
    {
        // iPhone Classic
        NSLog(@"iPhone 4");
    }
    if(result.height == 568)
    {
        // iPhone 5
        NSLog(@"iPhone 5");
    }
}

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

SideMenuViewController *leftMenuViewController = [[SideMenuViewController alloc] init];



ContainerOfSideMenuByVeerViewController *container = [ContainerOfSideMenuByVeerViewController
                                                      containerWithCenterViewController:[self navigationController]
                                                      leftMenuViewController:leftMenuViewController];

self.window.rootViewController = container;
[self.window makeKeyAndVisible];

return YES;

}

我想在leftMenuViewController中有一些值,每当我更改我的控制器时,它只加载一次因为在应用启动时didFinishLaunchingWithOptions加载一次。 那我该怎么办?

1 个答案:

答案 0 :(得分:1)

将其存储为属性。

在AppDelegate.h文件中:

@property (nonatomic, strong) ContainerOfSideMenuByVeerViewController *container;

在AppDelegate.m文件中:

self.container = [ContainerOfSideMenuByVeerViewController
                  containerWithCenterViewController:[self navigationController]
                  leftMenuViewController:leftMenuViewController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];

然后当您想要更改leftMenuViewController时,您可以从任何您想要的地方调用以下内容:

AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[delegate.container setLeftMenuViewController:...someViewController];

Apple's Documentation中的属性有一个很好的介绍。

另外,在检查大小时,您应该使用if... else...而不是两个if语句:

CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480.0f)
{
    // iPhone Classic
    NSLog(@"iPhone 4");
}
else if(result.height == 568.0f)
{
    // iPhone 5
    NSLog(@"iPhone 5");
}