Introduction to the use of VTMagic (two)


The distance VTMagic has been officially open for a month, this short time gain of more than and 900 star, in the GitHub month list in the Objective-C language, the basic ranking has been maintained at three. The first is YAHOO’s AppDevKit, the third is well-known AFNetworking. Frankly, such a result is completely beyond my expectations, everyone’s recognition of VTMagic let me be full of joy at the same time also become somewhat uneasy. For the improvement of VTMagic, I slowly began to become cautious, every change needs to continue to test and verify, to ensure that it can run normally. Personal strength is small, I hope you can improve the VTMagic together with fellow rivers and lakes, participation in open source projects will be a very interesting thing!


In the article, has been initially introduced some of the features of VTMagic and the basic use, but also received a lot of feedback, this paper will further introduce some use VTMagic tips and techniques, and according to the feedback part to do some summary, hope you can help developers.

Reuse mechanism

The mechanism is the core of the entire framework, which ensures that even if there are hundreds of pages in a single module, the application memory is still controllable, there will be no sudden surge, or even forced to kill the system. Under the pre loading mechanism, VTMagic will load the current page and the next page by default, that is to say, the page loaded at the same time may have two or three, depending on the location of the current page. If the current page is on both sides of the edge, the loaded page has two, if in the middle there will be three pages loaded. This is also the reason why some developers feedback viewControllerAtPage: method will be called many times.

Other pages that have been loaded will be removed when they are not needed, and put into the internal cache pool to be reused. When a page is about to be reused, the vtm_prepareForReuse method is called, which belongs to the VTMagicReuseProtocol protocol, the developers need to manually override in each sub page, the page usually can remove old data, correct the page offset in this method.

It should be noted that the needPreloading can be set to NO to close the preload mechanism. In the case of the preload mechanism is closed, the VTMagic will only load the current page, instead of loading the next page, and when the menu item is switched, the page will no longer switch animation.

Life cycle method

When a sub page appears or disappears, VTMagic can accurately trigger its corresponding life cycle method (viewWillAppear:, etc.). If the page is reusable, it can be placed in the network request in the viewDidAppear: method, then the corresponding data to request the current menu item according to the menuInfo.menuId, as a necessary supplement, request should decide whether the page has the latest data; in contrast, we should cancel the request in viewWillDisappear: or viewDidDisappear: methods in the network, so as not to be reused after the page data confusion and unnecessary traffic overhead. If the page is unique, that does not have other pages to reuse the page, this case can also be placed in the network request in the viewDidLoad method, especially when the pre loading logic is closed, i.e. needPreloading NO.

When it comes to network requests will have to say that the problem of loading local data. For reusable pages, when the menuInfo changes (menuInfo setter method can be rewritten), we usually need to load the cache data for the page, to ensure that the sliding switch can have a good user experience. A cache the data can be read from the database, can also direct the page before the data will be saved to a variable for later use, and even can be obtained from the plist file, according to the specific circumstances of flexible selection.

Refresh and switch

  • The reloadData method will reload all pages;
  • The reloadDataToPage: method will focus on the specified page while reloading all pages;
  • The switchToPage:animated: method is used to switch to the specified page when needed.

The use of the above three scenarios are different, according to the specific circumstances of flexible use. Note that, as far as possible to avoid the method in viewWillAppear: and call the viewWillDisappear: method above, not life cycle approach, otherwise it will cause some pages accurately triggered, similar to the Unbalanced calls to console will output begin/end appearance transitions for < VTGridViewController: 0x7fc3e2d43a70> warning. This is because the currentViewController and viewDidAppear:, viewDidDisappear: in currentViewController is not the same, that is to say the life cycle of currentViewController did not complete the trigger. In viewDidLoad, viewDidAppear:, viewDidDisappear: or other places to call the above method is no problem.

Multilayer nesting

VTMagic support multiple nested, that is, in the sub page can be nested in a VTMagic, similar to the demand is generally common in the iPad project, the use of its principles and the use of a single VTMagic consistent, there is no other obvious difference.

Cross level business process

For the convenience of cross level processing business, VTMagic provides self.magicController method for direct access to the main controller in upper recently in the sub pages, you can even further by self.magicController. magicView to the current magicView. Of course, you can also get the current page index by self.vtm_pageIndex. Similarly, the main controller can be obtained by self.currentViewController and self.currentPage the current display of the page and index.

Custom components

VTMagic in almost all the components support custom, the framework itself mainly provides the core switching logic processing and good performance management, developers can according to the business needs of any component interface provides custom.

Custom menuItem

You can return any custom menuItem in the menuItemAtIndex: method, as long as it is a UIBUtton type. MenuItem is a single menu item in the top menu bar, which can support text, pictures, text, pictures, etc.. The color and font of the text of the menu bar can be achieved by modifying the text and color of the menuItem, and the VTMagic itself does not directly provide an interface for modifying text and fonts.

Custom menu bar

You can add any view on both sides of the top menu bar through the properties of leftNavigatoinItem and rightNavigatoinItem, the need to pay attention to is being added to the view, there should be determined by size, and then VTMagic will automatically complete the corresponding layout.

Custom sliderView

The needs are different, underline and bubble style provided by VTMagic may not be what you want, in this case you can use the setSliderView: method from the definition of any style of sliderView, such as demo display in a triangle logo.

Custom separatorView

If necessary, you can even customize the top navigation line by setSeparatorView:, but in general it should not be necessary.


  • VTMagic has a lot of interesting small feature, such as scrollEnabled, switchEnabled, switchEvent and navigationInset, and so on, see the VTMagicView.h file for details.
  • All of the important changes on VTMagic will be recorded on the GitHub change log, and I want to know which updates are available for each version.

Concluding remarks

Finally, according to the Convention, if you love this one, please leave a star, this is to encourage and support for the biggest, thanks!!! If you have a better idea or plan, also welcome to submit pull request!!! GitHub address

Previous article: introduction to the use of VTMagic