iPhone刷新AppDelegate

时间:2012-02-16 13:19:52

标签: iphone xcode

好的我有一个使用标签控制器的应用程序。然而,在我想要加载标签控制器之前,我希望用户进入一个屏幕,允许他们首先输入他们的电话号码,因为我需要保存用户电话号码以便在应用程序中使用。我将这个号码保存到应用程序中。

[[NSUserDefaults standardUserDefaults] setInteger:1234 forKey:@"PhoneNumber"];

基本上,当他们第一次进入应用程序时,会打开一个窗口,允许他们在文本字段中输入他们的号码并使用NSuserDefaults保存。

在appDelegate中,我有一个if语句来检查是否存储了一个值

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    NSInteger phoneNumber=[[NSUserDefaults standardUserDefaults] integerForKey:@"PhoneNumber"];

    if(phoneNumber == 0){
        //So this is the window which allow them to enter their phone number
        UIViewController *phoneNumberVC = [[PhoneNumberVC alloc] initWithNibName:@"PhoneNumber" bundle:nil];
        UIViewController *phoneNumberRootVC = [[UINavigationController alloc] initWithRootViewController:phoneNumberVC];
        self.window.rootViewController = phoneNumberRootVC;    
        [self.window makeKeyAndVisible];    
    }else{
        //Code here sets up tab controller and all the tabs etc
    }
}

现在代码工作正常,除了保存号码后,我必须退出模拟器,再次运行,然后出现标签屏幕。但是,我希望标签控制器能够在用户按下电话号码屏幕中的按钮后立即加载,以保存该号码。电话号码屏幕中的当前方法是:

-(IBAction)testing{
    NSString *st = [phoneTF text];
    NSInteger pn = [st intValue];
    NSString *display = [NSString stringWithFormat:@"%i", pn];
    [[NSUserDefaults standardUserDefaults] setInteger:pn forKey:@"PhoneNumber"];
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                     message:display
                                                    delegate:self
                                           cancelButtonTitle:@"Okay"
                                           otherButtonTitles:nil];
    [alert show];
}

警报仅用于测试目的。因此,按下按钮后,我希望选项卡控制器在应用程序委托中加载。无论如何我可以刷新应用程序或应用程序代表吗?

如果有人能指出我正确的方向,我将非常感激!

修改

尝试

[self.navigationController pushViewController:app.tabBarController animated:YES];

但由于tabBarController尚未初始化,因此无法运行

控制台错误

Application tried to push a nil view controller on target <UINavigationController: 0x8056d40>.

所以我需要再次运行AppDelegate,因此选项卡控制器及其所有视图都将被初始化

1 个答案:

答案 0 :(得分:1)

替换

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:display
                                            delegate:self cancelButtonTitle:@"Okay" otherButtonTitles: nil];
[alert show];

使用代码要求导航控制器切换到您想要的其他视图:

YourOtherViewController *yourOtherViewController = [[YourOtherViewController alloc] init];
[self.navigationController pushViewController:yourOtherViewController animated:YES];

修改

如果您想从phoneNumberVC访问AppDelegate,您可以执行以下操作:

if(phoneNumber == 0){
    UIViewController *phoneNumberVC = [[PhoneNumberVC alloc] initWithNibName:@"PhoneNumber" bundle:nil];
    phoneNumberVC.appDelegate = self; // add this property to PhoneNumberVC
    UIViewController *phoneNumberRootVC = [[UINavigationController alloc] initWithRootViewController:phoneNumberVC];
    self.window.rootViewController = phoneNumberRootVC;
} else {
    [self setUpTabViewController]; // add this function
}
[self.window makeKeyAndVisible];

在PhoneNumberVC中,例如:

[self.appDelegate setUpTabViewController];