通过仿密码生成器软件,练习IOS开发技术,加深对MVC设计模式的理解,对以前学习的点点滴滴复习+掌握。因为看到的例子是用拖拉界面实现的,而为了实现和更好地学习IOS开发,我采用纯编码的方式来开发,所以相对拖拉会比较慢。例子里面虽然有专门的布局方法,但是没有处理屏幕方向发生变化时的事件,所以横屏还是有问题的。此外,对于每个界面都有一个对应的控制类,在UIView类里面实现UI元素的添加布局,在控制器里面实现事件、逻辑的处理,以便符合MVC的设计模式。
MainView
)和帮助页面(InfomationView
),这两个视图都是以程序的子视图的方式显示,两者互斥出现页面切换时的动画采用UIView
动画,其基本用法形如下所示:
[UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1.0f]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES]; [self removeMainView]; [UIView commitAnimations];
一个UINavigationItem
分为三部分:左按钮,标题文字,右按钮,主要用法形如:
UINavigationBar* bar = [[UINavigationBar alloc] init]; [self.view insertSubview:bar aboveSubview:self.infomationViewController.view]; UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"About Password Generator"]; [bar pushNavigationItem:item animated:YES]; UIBarButtonItem* leftBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(SwitchView)]; UIBarButtonItem* rightBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(SwitchView)]; item.leftBarButtonItem = leftBarButton; item.rightBarButtonItem = rightBarButton;
由于UIView
的类里面初始化的时候只是添加了控件,而没有对控件的布局进行初始化,而布局初始化又是类方法里面的reLayout
,所以为了保证显示前能够正确调用布局,我在视图对应类的控制器类里面重写了方法- (void) viewWillAppear:(BOOL)animated
,在该方法里面调用了布局方法,如下所示:
- (void) viewWillAppear:(BOOL)animated { if(self.autolayout) { [self.view reLayout]; } [super viewWillAppear:animated]; }
程序主控制器
// // ViewController.m // PasswordGenerator // // Created by arbboter on 14/12/23. // Copyright (c) 2014??? arbboter. All rights reserved. // #import "ViewController.h" #import "MainViewController.h" #import "InfomationViewController.h" @interface ViewController () @property(nonatomic, retain) UIButton* infomationButton; @property(nonatomic, retain) MainViewController* mainViewController; @property(nonatomic, retain) InfomationViewController* infomationViewController; @property (nonatomic, retain) UINavigationBar* navagationBar; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self SwitchView]; } - (void) onInfomationView { InfomationViewController* viewController = [[InfomationViewController alloc] init]; self.infomationViewController = viewController; [self.view addSubview:viewController.view]; [viewController release]; UINavigationBar* bar = [[UINavigationBar alloc] init]; [self.view insertSubview:bar aboveSubview:self.infomationViewController.view]; self.navagationBar = bar; [bar release]; UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"About Password Generator"]; [bar pushNavigationItem:item animated:YES]; UIBarButtonItem* leftBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(SwitchView)]; UIBarButtonItem* rightBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(SwitchView)]; item.leftBarButtonItem = leftBarButton; item.rightBarButtonItem = rightBarButton; [item release]; [leftBarButton release]; [rightBarButton release]; } - (void) removeInfomationView { [_infomationViewController.view removeFromSuperview]; [_infomationViewController release]; _infomationViewController = nil; [_navagationBar removeFromSuperview]; [_navagationBar release]; _navagationBar = nil; } - (void) SwitchView { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1.0f]; if ([self.infomationButton superview]) { [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES]; [self removeMainView]; [UIView commitAnimations]; [self onInfomationView]; } else { [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES]; [self removeInfomationView]; [UIView commitAnimations]; [self onMainView]; } [self reLayout]; } - (void) removeMainView { [_infomationButton removeFromSuperview]; [_infomationButton release]; _infomationButton = nil; [_mainViewController.view removeFromSuperview]; [_mainViewController release]; _mainViewController = nil; } - (void) onMainView { UIButton* button = [UIButton buttonWithType:UIButtonTypeInfoDark]; [self.view addSubview:button]; [button addTarget:self action:@selector(SwitchView) forControlEvents:UIControlEventTouchUpInside]; self.infomationButton = button; MainViewController* viewController = [[MainViewController alloc] init]; [self.view insertSubview:viewController.view belowSubview:self.infomationButton]; self.mainViewController = viewController; [viewController release]; } - (void) reLayout { CGPoint origin = self.view.frame.origin; CGSize size = self.view.frame.size; CGFloat w = 40; CGFloat h = 40; CGFloat yMargin = 10; CGFloat xMargin = 10; CGFloat x = origin.x + size.width-2*xMargin-w; CGFloat y = origin.y + size.height - 2*yMargin - h; _navagationBar.frame = CGRectMake(origin.x, origin.y+20, size.width, 40); _infomationButton.frame = CGRectMake(x, y, w, h); } -(void) viewWillAppear:(BOOL)animated { [self reLayout]; [super viewWillAppear:animated]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void) dealloc { [self removeInfomationView]; [self removeMainView]; [super dealloc]; } @end
帮助页面视图类
// // InfomationView.m // PasswordGenerator // // Created by arbboter on 14/12/23. // Copyright (c) 2014年 arbboter. All rights reserved. // #import "InfomationView.h" @interface InfomationView () @property (nonatomic, retain) UILabel* logoLabel; @property (nonatomic, retain) UIImageView* bkImageView; @end @implementation InfomationView - (id)init { self = [super init]; if(self == nil) { return self; } UILabel* label = nil; label = [[UILabel alloc] init]; label.text = @"Copyright (c) 2014年 arbboter."; label.textAlignment = NSTextAlignmentCenter; self.logoLabel = label; [self addSubview:label]; [label release]; UIImageView* imageView = [[UIImageView alloc] init]; imageView.image = [UIImage imageNamed:@"bk.jpg"]; imageView.contentMode = UIViewContentModeScaleAspectFit; self.bkImageView = imageView; [self insertSubview:imageView belowSubview:self.logoLabel]; [imageView release]; return self; } - (void) reLayout { CGPoint origin = self.frame.origin; CGSize size = self.frame.size; CGFloat yMargin = 10; CGFloat xMargin = 10; CGFloat w = size.width-2*xMargin; CGFloat h = 40; CGFloat x = origin.x + xMargin; CGFloat y = origin.y + size.height - 2*yMargin - h; _bkImageView.frame = self.frame; _logoLabel.frame = CGRectMake(x, y, w, h); } - (void) dealloc { [_bkImageView release]; [_logoLabel release]; [super dealloc]; } @end
原文地址:http://blog.csdn.net/arbboter/article/details/42105099