IOS開發實現密碼產生器教程

來源:互聯網
上載者:User

通過仿密碼產生器軟體,練習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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.