- (id)init
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
UITabBarItem *tbi = [self tabBarItem]; # ココ
[tbi setTitle:@"Hypnosis"];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Loaded the view for HypnosisViewController");
[[self view] setBackgroundColor:[UIColor orangeColor]];
}
設定方法は2つあります。
- ビューをプログラムで生成する方法
- XIBファイルを生成する方法
いつどちらの方法を選択するかは、どうやって決めたらよいでしょうか?簡単な目安として、ビューにサブビューがなければプログラムで生成し、サブビューがあればXIBファイルを生成することを推奨します。
このあたりのフックポイントは、Xcodeのテンプレートが吐いてくれてる(HypnoTimeViewController.m参照)。
UIAccelerometer *accelerometerはreleaseしなくてよいぽい。
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSLog(@"Monitoring accelerometer"); UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer]; [accelerometer setUpdateInterval:0.1]; [accelerometer setDelegate:self]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[UIAccelerometer sharedAccelerometer] setDelegate:nil]; }
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { NSLog(@"%f, %f, %f", [acceleration x], [acceleration y], [acceleration z]); HypnosisView *hv = (HypnosisView *)[self view]; [hv setXShift:10.0 * [acceleration x]]; [hv setYShift:-10.0 * [acceleration y]]; [hv setNeedsDisplay]; }
float filteringFActor = 0.1; lowPassed = newValue * filteringFactor + lowPassed * (1.0 - filteringFactor);
float filteringFactor = 0.1; lowPassed = newValue * filteringFactor + lowPassed * (1.0 - filteringFactor); highPassed = newValue - lowPassed;
- (id)initWithFrame:(CGRect)frame { self = [self initWithFrame:frame]; (以下略) }
通知センタ: NSNotificationCenter
通知内容オブジェクト: NSNotification
便利。
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIDevice *device = [UIDevice currentDevice];
[device beginGeneratingDeviceOrientationNotifications];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:device];
}
...
- (void)orientationChanged:(NSNotification *)notification
{
NSLog(@"orientationChanged: %d", [[notification object] orientation]);
}
[view setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleHeight];