加载时尚未注册的值

时间:2014-01-24 16:30:36

标签: ios ios7 appdelegate

我目前正在执行以下操作:在我的应用委托中使用此方法检查我正在使用的NSInteger的值。

- (void)applicationDidBecomeActive:(UIApplication *)application
{

    NSLog(@"Application did become active");
    TCAViewController *uiTCA = [[TCAViewController alloc] init];
    if(uiTCA.failed == 1){
    [uiTCA restart]; // this method in my view controller will trigger my viewDidLoad
     }
}

但是此时失败的变量为0,即使在应用程序进入后台之前将失败设置为1。应用程序完全加载后,我的失败整数的值为1。但显然在这一点上为时已晚。我该如何解决这个问题。我目前只是在全球范围内声明了NSInteger。

@interface TCAViewController () {

    NSInteger failed;

}

这是我的整个appDelegate.m

#import "TCAAppDelegate.h"

@implementation TCAAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [myApp sharedInstance];

    NSError *sessionError = nil;
    NSError *activationError = nil;

    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&sessionError];
    [[AVAudioSession sharedInstance] setActive: YES error: &activationError];
    // Override point for customization after application launch.
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{

    NSLog(@"Application did become active");

    if(!uiTCA)
    {
        uiTCA = [[TCAViewController alloc] init];
    }
    if(uiTCA.failed == 1){
        [uiTCA restart]; // this method in my view controller will trigger my viewDidLoad
    }


    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

这是我的appDelegate.h

#import <UIKit/UIKit.h>
#import "myApp.h"
#import <AVFoundation/AVFoundation.h>
#import "TCAViewController.h"

@interface TCAAppDelegate : UIResponder <UIApplicationDelegate>{

    TCAViewController *uiTCA;
}

@property (strong, nonatomic) UIWindow *window;

@end

2 个答案:

答案 0 :(得分:2)

这行代码:

TCAViewController *uiTCA = [[TCAViewController alloc] init];

创建一个新对象。这个新对象将所有类变量初始化为new。因此,您的failedFlag将为0.您将不得不在appDelegate.h中引用TCAViewController。如果您创建该对象,您将可以稍后在appDidBecomeActive中重复使用它,并且它将使您之前设置的值失败。

使用代码示例进行编辑:

在你的AppDelegate.h中,你会想要这样的东西:

@interface AppDelegate
{
    TCAViewController *uiTCA;
}

在你的.m之类的东西(实际上你在哪里创建TCAViewController的第一个实例):

- (void)applicationDidBecomeActive:(UIApplication *)application
{

    NSLog(@"Application did become active");
    if(!uiTCA)
    {
        uiTCA = [[TCAViewController alloc] init];
    }
    if(uiTCA.failed == 1){
        [uiTCA restart]; // this method in my view controller will trigger my viewDidLoad
     }
}

答案 1 :(得分:0)

由于我正在创建一个新实例,它将采用该整数的默认值(零) 所以在我的TCAViewController.m中,我只为该委托方法添加了一个观察者。

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(restart) name:UIApplicationDidBecomeActiveNotification object:nil];
相关问题