SlideShare a Scribd company logo
最近ViewControllerを
どんな風に書いているか
@u16suzu
http://u16suzu.hatenablog.com/
自己紹介
• @u16suzu
• iOSを仕事で初めて1年と2ヶ月くらい
• Railsもやります. プログラミング大好き!
今日の内容
• xib, story boardを使わないでコードだけで iOSを
書くときに,どういう風にViewControllerをかいて
いるか
• 主にビューの描画部分について
- (void)viewDidLoad{
[super viewDidLoad];
!
UIButton*btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(100, 100, 100, 50);
[btn setTitle:@"Button" forState:UIControlStateNormal];
[btn sizeToFit];
[self.view addSubview:btn];
UILabel*l = [[UILabel alloc]initWithFrame:CGRectMake(100, 150, 100, 50)];
l.text = @"label";
[self.view addSubview:l];
}
昔
viewDidLoad にコントロールの初期化と位置指定
のコードが全て書いてある
今
コントロールの初期化と位置指定を分けている
@interface ViewController ()
@property (nonatomic) UIView*titleView;
@property (nonatomic) CustomView*customView;
@end
!
@implementation ViewController
- (void)loadView{
[super loadView];
[self.view addSubview:self.titleView];
[self.view addSubview:self.customView];
}
!
- (void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
CGRect titleViewFrame = CGRectMake(50, 50, 50, 50);
self.titleView.frame = titleViewFrame;
CGRect customViewFrame = CGRectMake(50, 100, 300, 100);
self.customView.frame = customViewFrame;
}
!
#pragma mark - Views
!
- (UIView*)titleView{
if(!_titleView){
_titleView = [UIView new];
_titleView.backgroundColor = [UIColor redColor];
}
return _titleView;
}
!
- (CustomView*)customView{
if(!_customView){
_customView = [CustomView new];
}
return _customView;
}
!
@end
プロパティで宣言
アクセッサで初期化
viewDidLayoutSubviews
で位置指定
loadViewでaddSubview
改善ポイント
• コントロールの位置の変更が容易
• ソースコードが読みやすくなった
• カスタムビューに分割しやすい
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self addSubview:self.firstButton];
}
return self;
}
!
#pragma - Views
!
- (void)layoutSubviews{
[super layoutSubviews];
[self.firstButton sizeToFit];
CGRect frame = CGRectMake(0, 0, 60, 30);
self.firstButton.frame = frame;
}
!
- (UIButton*)firstButton{
if(!_firstButton){
_firstButton = [UIButton buttonWithType:UIButtonTypeSystem];
[_firstButton setTitle:@"firstButton" forState:UIControlStateNormal];
}
return _firstButton;
}
今のカスタムビュー
layoutSubviewsで位置指定
initWithFrameで addSubview
あとは VCと同じ
以上です!

More Related Content

PDF
Auto Layout Tips
PDF
JavaScript + CSS3を活用して スマートフォンサイト/アプリに 動きを付けてみよう
PDF
introduction to Marionette.js (jscafe14)
PDF
XIBで作ったカスタムセルの扱い方
KEY
HTML5で作るスマホブラウザゲーム
PDF
実践Backbone.Marionette 現場の悩みと解決まで
PDF
レスポンシブWebデザイン【発展編】
PDF
公式page改ざんで学ぶjQuery入門 (jscafe7)
Auto Layout Tips
JavaScript + CSS3を活用して スマートフォンサイト/アプリに 動きを付けてみよう
introduction to Marionette.js (jscafe14)
XIBで作ったカスタムセルの扱い方
HTML5で作るスマホブラウザゲーム
実践Backbone.Marionette 現場の悩みと解決まで
レスポンシブWebデザイン【発展編】
公式page改ざんで学ぶjQuery入門 (jscafe7)

What's hot (12)

PDF
Enhancements with 3D Touch
PDF
絶望しない! コミケ
PPTX
jQueryで気をつけてほしいこと
PDF
Jsのビルド環境
PDF
Study Intro Backbone
PDF
Apple watch対応アプリのポイントや基本実装・連携tips
PPTX
Effective flutter
PDF
Alt01-LT
PDF
JavaScriptと共に歩いて行く決意をした君へ
PDF
SQLアンチパターン読書会 15章 ランダムセレクション 説明資料
PPTX
Excel 方眼紙撲滅委員会 活動報告 2012.11 #odstudy
Enhancements with 3D Touch
絶望しない! コミケ
jQueryで気をつけてほしいこと
Jsのビルド環境
Study Intro Backbone
Apple watch対応アプリのポイントや基本実装・連携tips
Effective flutter
Alt01-LT
JavaScriptと共に歩いて行く決意をした君へ
SQLアンチパターン読書会 15章 ランダムセレクション 説明資料
Excel 方眼紙撲滅委員会 活動報告 2012.11 #odstudy
Ad

Viewers also liked (20)

PDF
Alvaro Fides - Use of hierarchical model-view-controller architecture for use...
PPT
Wellington Residences - Lisa
PDF
Paper María Iglesia - CEIB: A R&D services in bioimaging oriented to integrat...
PDF
Registry of Course Trainers For 2013
PDF
Paper Florencio Cano - Patient data security in a wireless and mobile world
PPT
The Maori :background
PDF
Beni Gómez - APTIC a knowledge repository for parents and caregivers of chil...
PDF
عامر عرموش
PDF
Basic html
PDF
Alejandro Gonzalez - An iPhone-based application for promoting type 2 diabeti...
PPTX
Presentation finale3 sfr2011
PPT
Audience research
PDF
Mercedes Serrano - Guía metabólica: empowerment through health 2.0 tools in ...
DOC
Chapter13
PPTX
PPTX
Question 2 - Evaluation
PDF
Manuel Armañones - Mesa redonda: Health 2.0
ODP
New Year’s Eve in New York City
PPTX
Buildings by jihad awad
PPTX
Professional Innovator Development
Alvaro Fides - Use of hierarchical model-view-controller architecture for use...
Wellington Residences - Lisa
Paper María Iglesia - CEIB: A R&D services in bioimaging oriented to integrat...
Registry of Course Trainers For 2013
Paper Florencio Cano - Patient data security in a wireless and mobile world
The Maori :background
Beni Gómez - APTIC a knowledge repository for parents and caregivers of chil...
عامر عرموش
Basic html
Alejandro Gonzalez - An iPhone-based application for promoting type 2 diabeti...
Presentation finale3 sfr2011
Audience research
Mercedes Serrano - Guía metabólica: empowerment through health 2.0 tools in ...
Chapter13
Question 2 - Evaluation
Manuel Armañones - Mesa redonda: Health 2.0
New Year’s Eve in New York City
Buildings by jihad awad
Professional Innovator Development
Ad

よこへな3 15発表資料 最近ViewController をどんな感じで書いているか