Use of WMPageController in iOS development

Source: Internet
Author: User

Use of WMPageController in iOS development

This article records how to use the WMPageController control of a third-party library in iOS development. WMPageController is similar to the ViewPager control on the Android platform and is mainly used to display content by page, you can slide through gestures to switch pages, or click the title to switch pages, as shown in:

WMPageController's git address is: https://github.com/wangmchn/WMPageController

The following describes how to use the WMPageController control:

(1) create a project WMPageControllerTest and introduce WMPageController to the project through cocoapods. The content of the Podfile file is as follows:

 

platform:ios, '7.0'pod 'WMPageController', '~> 1.4.1'
(2) Add the following code to the ViewController. m file:

 

 

# Import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (50, 50,100, 50)]; label. text = @ "Sports News"; [self. view addSubview: label];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end
Here we only add a UILabel label to ViewController.

 

(3) create another ViewController2 inherited from UIViewController and add the following code to the ViewController2.m file:

 

# Import "ViewController2.h" @ interface ViewController2 () @ end @ implementation ViewController2-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (50, 50,100, 50)]; label. text = @ "Entertainment News"; [self. view addSubview: label];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end
(4) Open the AppDelegate. m file and add the following method to it:

 

 

# Pragma mark returns a WMPageController object-(WMPageController *) getPages {// The page array NSArray * controllers = [NSArray arrayWithObjects: [ViewController class], [ViewController2 class], nil]; // The title array of the WMPageController control NSArray * titles = [NSArray arrayWithObjects: @ "Sports News", @ "Entertainment News", nil]; // use the preceding two arrays to initialize the WMPageController object WMPageController * pageController = [[WMPageController alloc] handler: controllers andTheirTitles: titles]; // set the width of pageController for each title of WMPageController. menuItemWidth = 100; // sets the height of pageController In the title bar of WMPageController. menuHeight = 35; // set the color of the title pageController selected by WMPageController. titleColorSelected = [UIColor colorWithRed: 200 green: 0 blue: 0 alpha: 1]; return pageController ;}
Then add the following code in the-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions method of AppDelegate. m:

 

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    WMPageController *pageController = [self getPages];    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:pageController];    self.window.rootViewController = navController;    return YES;}
The above Code creates a WMPageController object, uses it to initialize a navigation bar, and finally sets the Root View Controller of the window as the navigation bar. Then we can run the program. The effect is as follows:

 


The preceding Code uses WMPageController. According to the demo provided on github, WMPageController can also be used in Storyboard. The following describes how to use the WMPageController control in storyboard:

(1) create a project WMPageControllerTest2

(2) Open Main. storyboard, select the ViewController view, and add a navigation bar to it, as shown in:


(3) create a new ViewController named NewsViewController. in the storyboard, you also drag a ViewController view to set the view to NewsViewController and set the StoryboardID to "viewController", as shown in:

Add a UILabel label to the newly created ViewController view, as shown in.

(4) Open the ViewController. h file and let ViewController inherit from WMPageController. The Code is as follows:

 

# Import
 
  
# Import "WMPageController. h" @ interface ViewController: WMPageController # pragma mark title array @ property (strong, nonatomic) NSArray * titles; @ end
 
(5) edit the ViewController. m file. The Code is as follows:

 

 

# Import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController # pragma mark initialization code-(instancetype) initWithCoder :( NSCoder *) aDecoder {self = [super initWithCoder: aDecoder]; if (self) {self. menuHeight = 35; self. menuItemWidth = 100; self. menuViewStyle = WMMenuViewStyleLine; self. titles = [NSArray arrayWithObjects: @ "Entertainment News", @ "Sports News", nil];} return self ;}- (void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} # pragma mark returns the title of the index-(NSString *) pageController :( WMPageController *) pageController titleAtIndex :( NSInteger) index {return [self. titles objectAtIndex: index] ;}# pragma mark returns the number of subpages-(NSInteger) numbersOfChildControllersInPageController :( WMPageController *) pageController {return [self. titles count] ;}# pragma mark returns the page corresponding to an index, which is obtained from the Storyboard-(UIViewController *) pageController :( WMPageController *) pageController viewControllerAtIndex :( NSInteger) index {UIStoryboard * storyboard = [UIStoryboard storyboardWithName: @ "Main" bundle: nil]; UIViewController * controller = [storyboard instantiateViewControllerWithIdentifier: @ "viewController"]; // The identifer here is the previously set StoryboardID return controller;}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end
You can run the program here. The effect is as follows:

 

Note that the title of WMPageController has several styles to choose from. The enumerated values of the styles are as follows:

 

Typedef NS_ENUM (NSUInteger, WMMenuViewStyle) {WMMenuViewStyleDefault, // WMMenuViewStyleLine by default, // underline (if the selected font size remains the same, set the selected and non-selected sizes to the same, // inbound effect (fill) WMMenuViewStyleFooldHollow, // inbound effect (hollow )};





 

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.