IOS-Runtime: shortcuts to page Jump, ios-runtime jump

Source: Internet
Author: User

IOS-Runtime: shortcuts to page Jump, ios-runtime jump
Preface

During the webpage jump, if the current class is not UIViewcontroller (which is represented by VC below), will you write a proxy or block it to the VC to pass the information, and then perform

/// Assume that targetVc is the page to jump to [self. navigationController pushViewController: targetVc animated: YES];

TableViewCell is used as an example. If there are many such operations in tableViewCell displayed on each page, many proxies or blocks will be written. If not, what should we do, the idea is to obtain the VC currently displayed on the top layer. There are many methods on the Internet. First, let's look at a common method.

- (UIViewController *)currentViewController {    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;    UIViewController *vc = keyWindow.rootViewController;    while (vc.presentedViewController) {        vc = vc.presentedViewController;                if ([vc isKindOfClass:[UINavigationController class]]) {            vc = [(UINavigationController *)vc visibleViewController];        } else if ([vc isKindOfClass:[UITabBarController class]]) {            vc = [(UITabBarController *)vc selectedViewController];        }    }    return vc;}

The above method may be able to obtain the top-level VC (I found one on the Internet and did not test it. I will only compare it here), but does it feel complicated, of course, you can also write it in a tool. Every time you use this method in the tool, you can also get it, but I always feel complicated, haha;

Using Runtime

Runtime is a fun thing. In the previous article, I briefly talked about some of its common functions. Here, we use Category to obtain the current VC requirements.

1. Create a new category based on UIApplication

 

Click Next.

2. Use Runtime to add attributes

In the UIApplication + CurrentViewController. h file, add

/// Obtain the current UIViewController @ property (nonatomic, weak) UIViewController * currentViewController;

In the UIApplication + CurrentViewController. m file, introduce the header file

#import <objc/runtime.h>

Get set Method of attribute using runtime

///set- (void)setCurrentViewController:(UIViewController *)currentViewController{    objc_setAssociatedObject(self, @selector(currentViewController), currentViewController, OBJC_ASSOCIATION_ASSIGN);}///get- (UIViewController *)currentViewController{    return objc_getAssociatedObject(self, _cmd);}
3. Implementation

Introduce the header file in the file that needs to obtain the current VC, or directly put the header file into the macro file.

#import "UIApplication+CurrentViewController.h"

In the viewWillAppear method of VC, add

[UIApplication sharedApplication].currentViewController = self;

In this way, we can get the current VC by adding the following code. After obtaining the VC, whether it is push, present or javasmseguewithidentifier, the page can be redirected.

UIViewController *viewVc = [UIApplication sharedApplication].currentViewController;
4. Extend

The above needs to be added in every VC

[UIApplication sharedApplication].currentViewController = self;

You can create a base class BaseViewController Based on UIViewController and add the above Code in the viewWillAppear method of BaseViewController. When creating a VC, you only need to inherit the BaseViewController!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.