スキップしてメイン コンテンツに移動

投稿

6月, 2013の投稿を表示しています

UIView

UIView UIViewは画面表示を管理するクラスです。 画面表示を管理するクラスなので、ラベルやボタンといった各種コントロールのクラスは全てこのUIViewを継承しています。 各種コントロール部品の最も基本的なクラスです。 UIViewのクラス階層 NSObject ↑ UIResponder ↑ UIView 生成 // 生成例 UIView *uv = [[UIView alloc] init]; // サイズを指定しつつ生成の例 UIView *uv = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)]; ※サイズに関しては、 サイズ指定 のCGRect欄をご覧ください。 UIViewのプロパティ 【UIViewの主要プロパティ】 プロパティ名/型 読専 説明 frame ( CGRect ) 位置とサイズを指定する (例)uv.frame = CGRectMake(0, 0, 100, 80); ※CGRectMakeについては サイズ指定 をご覧ください center ( CGPoint ) 中心位置を指定する (例)uv.center = CGPointMake(200, 300); ※CGPointMakeについては サイズ指定 をご覧ください bounds ( CGRect ) backgroundColor ( UIColor ) 背景色を設定する (例)uv.backgroundColor = [UIColor blueColor]; hidden ( BOOL ) 表示/非表示を指定する (例)uv.hidden = YES; //非表示にする (例)uv.hidden = NO; //表示する autoresizingMask (UIViewAutoresizing) 子要素の自動調整  UIViewAutoresizingFlexibleTopMargin:上を自動調整  UIViewAutoresizingFlexibleBottomMargin:下を自動調整  UIViewAutoresizingFlexibleRightMargin:右を自動調整  UIViewAutoresizingFlexibleLeftMar...

addChildViewController:

はじめに iOS5からUIViewControllerを自前のクラスにaddChildViewController:メソッドで追加できるようになった。 最近では、こういった自前コンテナは主にFacebookやPath2.0のスライドメニュー風のUI開発なんかに使われていると思う。 このaddChildViewController:メソッドの後には必ずdidMoveToParentViewController:メソッドを呼ぶという説明があるが、これの意味がよくわからなかったのでそのメモ。 例えば didMoveToParentViewControllerの必要性を疑うコード //自前コンテナ(self)にchildViewControllerを追加 [ self addChildViewController: childViewController ]; //その後didMoveToParentViewControllerを実行しなければいけない [ childViewController didMoveToParentViewController: self ]; この追加(add)したあとになぜ完了(didMove)を明示的に知らせないといけないのか謎という感覚。また、これがなくてもこちらの想定通り動くのでずっと疑問だった。 結論としては 自前コンテナでaddChildViewController:を実行した後は、やはり必ずdidMoveToParentViewController:を呼ぶ。 説明 UIViewControllerの追加サイクル そもそもUIViewControllerの追加には「開始」と「終了」がかならずセットで必要となる。 ・追加: addChildViewController: ・開始: willMoveToParentViewController:(add後に自動で呼ばれる) ・完了: didMoveToParentViewController:(任意のタイミングで呼ぶ) まず自前コンテナへの追加のためにaddChildViewController:した際に、ViewControllerの追加開始の合図であるwillMoveToParentViewControl...

setAutoresizingMask:

Determines how the receiver’s  resizeWithOldSuperviewSize:  method changes its frame rectangle. - (void)setAutoresizingMask:( NSUInteger ) mask Parameters mask An integer bit mask.  mask  can be specified by combining using the C bitwise OR operator any of the options described in  “Resizing masks” . Discussion Where more than one option along an axis is set,  resizeWithOldSuperviewSize:  by default distributes the size difference as evenly as possible among the flexible portions. For example, if  NSViewWidthSizable  and  NSViewMaxXMargin  are set and the superview’s width has increased by 10.0 units, the receiver’s frame and right margin are each widened by 5.0 units. Availability Available in OS X v10.0 and later. See Also – autoresizingMask – resizeSubviewsWithOldSize: – setAutoresizesSubviews: Related Sample Code Cocoa Autolayout Demos GLUT PhotoSearch TextLayoutDemo Tex...

CGRectInset

Returns a rectangle that is smaller or larger than the source rectangle, with the same center point. CGRect CGRectInset ( CGRect rect, CGFloat dx, CGFloat dy ); Parameters rect The source  CGRect  structure. dx The x-coordinate value to use for adjusting the source rectangle. To create an inset rectangle, specify a positive value. To create a larger, encompassing rectangle, specify a negative value. dy The y-coordinate value to use for adjusting the source rectangle. To create an inset rectangle, specify a positive value. To create a larger, encompassing rectangle, specify a negative value. Return Value A rectangle. The origin value is offset in the x-axis by the distance specified by the  dx  parameter and in the y-axis by the distance specified by the  dy parameter, and its size adjusted by  (2*dx,2*dy) , relative to the source rectangle. If  dx  and  dy  are positive values, then the rectangle’s size ...

@property nonatomic strong weak

@propertyの属性にcopyとかstrongとかweakとかあるけど、概念的な説明はどこでもあるけど、具体的な説明をしている記事が少ないので調査した。 Xcode4.3.2時点での内容です。 nonatomic 非スレッドセーフにする。マルチスレッドのものを使わない限りはどんどん指定しよう。 ちなみにatomicな場合は内部的にはgetterは以下のような処理になる模様。ロックが入ってしまう。 [_internal lock]; id result = [[value retain] autorelease]; [_internal unlock]; return result; getter/setter getterとsetterのメソッド名はデフォルトで、getterは変数名と同じ、setterはset[Capitalize変数名]だが、それを任意のものに設定することができる。 readonly時にsetterは指定できない(当たり前だが) 通常変更する必要はないが、isなんたら とかってgetterにしたい場合に使うといいらしい。 readwrite/readonly readwriteはデフォルトなので、単品で指定することはまずない。readonlyはその名の通りsetterが生成されなくなる。self.prop = 1;みたいなドットでのアクセスすらコンパイルエラーになるので、このままだと何もできない変数になる気がする。 .hではreadonlyにして、.mでreadwriteにして定義を上書きすることで、クラス内部では読み書きができ、外部では読み出ししかできないプロパティが出来上がる。 .h #import <Foundation/Foundation.h> @interface TestData : NSObject @property (nonatomic, readonly) NSString* title; - ( void )action; @end .m #import "TestData.h" // ここがないとactionではエラー @interface TestData () @property (nonatomic...

method整理方法

売れるiPhoneアプリを作ろうと頑張って開発途中、メソッドをいっぱい作りすぎてカオスになっていませんか? そんなとき、メソッドをカテゴリ別に整理する方法を紹介。 Xcodeで新規プロジェクトを作成するとき「Single View Application」を選択したときのViewController.mはこんな感じ。 ? ViewController.m 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 #import "ViewController.h"   @implementation ViewController   - ( void )didReceiveMemoryWarning {      [super didReceiveMemoryWarning];      // Release any cached data, images, etc that aren't in use. }   #pragma mark - View lifecycle   - ( void )viewDidLoad {      [super viewDidLoad];     // Do any additional setup after loading the view, typically from a nib. }   - ( void )viewDidUnload {      [su...