سؤال

Here is my situation:
I have a TaskListViewController(UITableView) which need Internet.
So I used AFNetworking to do some login work(with demo account and password) before the TaskList is showed.

Now I need to let the user set their account and password.
So I just want to present a Modal View(ZTCUserSettingsViewController) before login.

And I cost a lot time, it still doesn't work.

Is there any way to solve this?

Thank you.

ZTCAPIClient : AFHTTPClient
ZTCTaskListViewController : UITableViewController

code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    [ZTCAPIClient login];


    UITableViewController *viewController = [[ZTCTaskListViewController alloc] initWithStyle:UITableViewStylePlain];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];


    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    return YES;
}
هل كانت مفيدة؟

المحلول

I don't see where your sample code attempts to present the modal view controller. Perhaps it happens inside of -[ZTCAPIClient login].

Generally speaking, you can't present a modal view controller until after the presenting view controller's view is in the view hierarchy. For your specific question, this means your program needs to present the modal sometime after the line that reads [self.window makeKeyAndVisible];. It will not work if your program tries to present the modal view controller before, in fact, you'll probably see an error message logged to the debug console.

On a side note, you should be careful not to make your program do too much in -application:didFinishLaunchingWithOptions:. If your program takes too long, the system may kill your app the iOS App Programming Guide states:

Your application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods should always be as lightweight as possible to reduce your app’s launch time. Apps are expected to launch and initialize themselves and start handling events in less than 5 seconds. If an app does not finish its launch cycle in a timely manner, the system kills it for being unresponsive. Thus, any tasks that might slow down your launch (such as accessing the network) should be executed asynchronously on a secondary thread.

When launching into the foreground, the system also calls the applicationDidBecomeActive: method to finish the transition to the foreground. Because this method is called both at launch time and when transitioning from the background, use it to perform any tasks that are common to the two transitions.

نصائح أخرى

Here is my final solution:

launch up:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];

UINavigationController *nav = [[UINavigationController alloc] init];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];

//important!
[ZTCAPIClient registerUserInfo];

return YES;
}

in ZTCAPIClient.m:

+ (void) registerUserInfo {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *account = [defaults stringForKey:@"account"];
if(!account) {
    // load default value
    [self performSelector:@selector(registerDefaultsFromSettingsBundle)];
    ZTCUserSettingsViewController *userSettingsView = [[ZTCUserSettingsViewController alloc] init];
    UINavigationController *usersSettingsNav = [[UINavigationController alloc] initWithRootViewController:userSettingsView];
    [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentModalViewController:usersSettingsNav animated:NO];
} else {
    DLog(@"**********************");
    if ([ZTCAPIClient loginWithAccount:[defaults stringForKey:@"account"] Password:[defaults stringForKey:@"password"] Mode:[defaults stringForKey:@"requestType"] BaseURL:[defaults stringForKey:@"url"]]) {
        DLog(@"Log in SUCCESS");
        UITableViewController *viewController = [[ZTCTaskListViewController alloc] initWithStyle:UITableViewStylePlain];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
        [[[[UIApplication sharedApplication] delegate] window] setRootViewController:nav];
    } else {
        DLog(@"Log in FAIL");
        ZTCUserSettingsViewController *userSettingsView = [[ZTCUserSettingsViewController alloc] init];
        UINavigationController *usersSettingsNav = [[UINavigationController alloc] initWithRootViewController:userSettingsView];
        [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentModalViewController:usersSettingsNav animated:NO];
    }
}
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top