效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @property (strong, nonatomic) UIPageControl *pageCCustom; 5 6 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 - (void)loadNavigation; 6 - (void)pageControlDidChange:(UIPageControl *)sender; 7 - (void)addDidPush:(UIBarButtonItem *)sender; 8 - (void)delDidPush:(UIBarButtonItem *)sender; 9 @end 10 11 @implementation ViewController 12 13 - (void)viewDidLoad { 14 [super viewDidLoad]; 15 16 [self layoutUI]; 17 } 18 19 - (void)didReceiveMemoryWarning { 20 [super didReceiveMemoryWarning]; 21 // Dispose of any resources that can be recreated. 22 } 23 24 - (void)viewWillAppear:(BOOL)animated { 25 [super viewWillAppear:animated]; 26 [self.navigationController setNavigationBarHidden:NO animated:animated]; 27 [self.navigationController setToolbarHidden:NO animated:animated]; 28 } 29 30 - (void)layoutUI { 31 [self loadNavigation]; 32 33 _pageCCustom = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; 34 _pageCCustom.center = self.view.center; 35 _pageCCustom.backgroundColor = [UIColor brownColor]; 36 _pageCCustom.numberOfPages = 3; 37 _pageCCustom.currentPage = 0; 38 [_pageCCustom addTarget:self 39 action:@selector(pageControlDidChange:) 40 forControlEvents:UIControlEventValueChanged]; 41 [self.view addSubview:_pageCCustom]; 42 } 43 44 - (void)loadNavigation { 45 self.navigationItem.title = @"添加或删除屏幕中的翻页数目"; 46 self.view.backgroundColor = [UIColor whiteColor]; 47 UIBarButtonItem *barBtnBarAdd = 48 [[UIBarButtonItem alloc] initWithTitle:@"添加新页" 49 style:UIBarButtonItemStyleDone 50 target:self 51 action:@selector(addDidPush:)]; 52 UIBarButtonItem *barBtnDel = 53 [[UIBarButtonItem alloc] initWithTitle:@"删除旧页" 54 style:UIBarButtonItemStyleDone 55 target:self 56 action:@selector(delDidPush:)]; 57 [self setToolbarItems:@[barBtnBarAdd, barBtnDel]]; 58 } 59 - (void)pageControlDidChange:(UIPageControl *)sender { 60 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" 61 message:[NSString stringWithFormat:@"currentPage=%ld", (long)sender.currentPage] 62 delegate:self 63 cancelButtonTitle:nil 64 otherButtonTitles:@"确定", nil]; 65 [alert show]; 66 } 67 68 - (void)addDidPush:(UIBarButtonItem *)sender { 69 if (_pageCCustom.numberOfPages < 10) { 70 _pageCCustom.numberOfPages++; 71 } 72 } 73 74 - (void)delDidPush:(UIBarButtonItem *)sender { 75 if (_pageCCustom.numberOfPages > 1) { 76 _pageCCustom.numberOfPages--; 77 [self pageControlDidChange:_pageCCustom]; 78 } 79 } 80 81 @end
AppDelegate.h
1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 @property (strong, nonatomic) UIWindow *window; 5 @property (strong, nonatomic) UINavigationController *navigationController; 6 7 @end
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "ViewController.h" 3 4 @interface AppDelegate () 5 @end 6 7 @implementation AppDelegate 8 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 10 _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 11 ViewController *viewController = [[ViewController alloc] init]; 12 _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 13 _window.rootViewController = _navigationController; 14 //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无 15 [_window makeKeyAndVisible]; 16 return YES; 17 } 18 19 - (void)applicationWillResignActive:(UIApplication *)application { 20 } 21 22 - (void)applicationDidEnterBackground:(UIApplication *)application { 23 } 24 25 - (void)applicationWillEnterForeground:(UIApplication *)application { 26 } 27 28 - (void)applicationDidBecomeActive:(UIApplication *)application { 29 } 30 31 - (void)applicationWillTerminate:(UIApplication *)application { 32 } 33 34 @end
时间: 2024-11-12 14:23:02