Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

Application scenario: Electronic signature and page information update in landscape mode

Example: The iOS application enters the foreground (i.e. when switching the application to the current application) to actively refresh the payment information

I. Application cases

1.1 Landscape electronic signature

  • Pass the screen rotation property code for the child controller:

1.2 Update interface information (request interface, update model data)

UpdateAppCollectionInfo

/** 2, when switching to the current APP, the payment information interface, take the initiative to refresh */
+ (void)UpdateAppCollectionInfo{
    
    UIViewController *rootViewController =UIApplication.sharedApplication.delegate.window.rootViewController;
    
//NSStringFromClass
    
    if(! [NSStringFromClass(rootViewController.class) isEqualToString:@"HWTabBarController"] ){
        

// if(! [rootViewController isKindOfClass:HWTabBarController.class] ){
        return ;
        
        
        
    }
    
    HWTabBarController *tmpTabVC = rootViewController;
    
    UIViewController *tmpTabVCselectedViewController = tmpTabVC.selectedViewController;
    
    
    if(! [tmpTabVCselectedViewController isKindOfClass:HWNavigationController.class] ){return ;
        
        
        
    
    }
    
    HWNavigationController *nav = tmpTabVCselectedViewController;

    if(! [@"QCTCollectionInformationViewController" isEqualToString: NSStringFromClass(nav.visibleViewController.class)]){
        
        return;
    }

    
    // Update data
    
    [nav.visibleViewController performSelector:@selector(setupAppCollectionInfo)];
    

}

Copy the code

II. Knowledge reserve

  • Program into the front desk: applicationDidBecomeActive
  • The navigation bar is displayed a controller: nav. VisibleViewController
  • TabVC selected controller: selectedViewController

2.1 Listeners enter the foreground

Program into the front desk: applicationDidBecomeActive

#pragmaMark - The program enters the foreground to switch the application to the payment information interface of the current application, and refresh the payment information once
- (void)applicationDidBecomeActive:(UIApplication *)application {
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    [JPUSHService setBadge:0];

    
    //

    
    [ QCTCollectionInformationViewController UpdateAppCollectionInfo];

    
        
}

Copy the code

2.2 Obtaining the VC from the UIStoryboard

  • Initialization method through instantiateInitialViewController storyboard ViewController

  • According to the instantiateViewControllerWithIdentifier from get VC from UIStoryboard object

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"SKUViewController" bundle:nil];

    SKUViewController *VC = [mainStoryboard instantiateViewControllerWithIdentifier:@"SKUViewController"];
    

Copy the code

2.3 Obtaining the CURRENT VC

getCurrentVC

// Get the current VC
+ (UIViewController *)getCurrentVC
{
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    
    return currentVC;
}


+ (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
    UIViewController *currentVC;
    
    if ([rootVC presentedViewController]) {
        // The view is presented
        rootVC = [rootVC presentedViewController];
    }
    
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        // The root view is UITabBarController
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]) {// The root view is UINavigationController
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
    } else {
        // The root view is a non-navigational class
        currentVC = rootVC;
    }
    return currentVC;
}

Copy the code

III, see also

For more, check out # Applets: iOS Reverse, which presents valuable information only for you, focusing on the mobile technology research field.