ラベル UIViewController の投稿を表示しています。 すべての投稿を表示
ラベル UIViewController の投稿を表示しています。 すべての投稿を表示

2014年4月13日日曜日

デバイスの回転対応

iOS7以降でデバイスの回転に対応するには、
XcodeのTARGETS->GeneralでDeployment Info->Device Orientation(info.plistのSupported interface orientations)でサポートする向きを設定しておいて、
ViewControllerに以下の処理を入れる。

// 回転するかどうか
- (BOOL)shouldAutorotate
{
    // 回転したときのViewの配置等の処理を記述

    // 回転する場合YES, しない場合NO
    return YES;
}

// サポートするインターフェースの向き
- (NSUInteger)supportedInterfaceOrientations
{
    // UIInterfaceOrientationMaskPortrait : 縦(ホームボタン下)
    // UIInterfaceOrientationMaskLandscapeLeft : 横(ホームボタン左)
    // UIInterfaceOrientationMaskLandscapeRight : 横(ホームボタン右)
    // UIInterfaceOrientationMaskPortraitUpsideDown : 縦(ホームボタン上)
    // UIInterfaceOrientationMaskLandscape : 横(両方)
    // UIInterfaceOrientationMaskAll : 4つ全て
    // UIInterfaceOrientationMaskAllButUpsideDown : 縦(ホームボタン上)以外の3つ
    return UIInterfaceOrientationMaskLandscape;
}

// インターフェースの向き(初期状態)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    NSLog(@"preferredInterfaceOrientationForPresentation");
    
    // UIInterfaceOrientationPortrait : 縦(ホームボタン下)
    // UIInterfaceOrientationPortraitUpsideDown : 縦(ホームボタン上)
    // UIInterfaceOrientationLandscapeLeft : 横(ホームボタン左)
    // UIInterfaceOrientationLandscapeRight : 横(ホームボタン右)
    return UIInterfaceOrientationLandscapeRight;
}

2014年1月29日水曜日

Storyboardでレイアウトを作成したUIViewControllerをコードで生成する

Storyboardでレイアウトを作成したUIViewControllerをコードで生成する。
Storyboardで生成するUIViewControllerを選択して、Identity inspectorでIdentityのStoryboard IDを設定しておく。
以下の例では、Storyboard ID: MyViewController としている。
MyViewController *myViewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"MyViewController"];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MyViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

2013年8月4日日曜日

UIViewController : segueを使わずにコードで画面遷移する - モーダル表示 -

Storyboardで遷移先のUIViewControllerにIDをつけておく。
NextViewController *nextViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NextVC"];
[self presentViewController:nextViewController animated:YES completion:nil];

UIViewController : segueをコードで実行する

UIViewControllerの performSegueWithIdentifier:sender: でsegueを実行する。
[self performSegueWithIdentifier:@"showNext" sender:self];

UIViewController : Strotyboardを使用して画面遷移する - モーダル表示 -

StoryboardでUIViewController(A)とUIViewController(B)を作成し、(A)にnextボタン、(b)にcloseボタンをつける。
nextボタンタップで(B)をモーダル表示し、closeボタンタップで(B)を閉じて(A)に戻る。

・(A)のnextボタンタップで(B)をモーダル表示
  1. Controlキーを押しながらUIViewController(A)のnextボタンからUIViewController(B)にドラッグ&ドロップする。
  2. Action Segue で push/modal/custom からmodalを選択する。(pushはUINavigationControllerにpushするときに使う。)
  3. segueができるので、クリックしてAttributes Inspector でsegueのIdentifierを入力する。
  4. (A)から(B)へデータを引き渡す時は prepareForSegue で行う。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showNext"]) {
        NextViewController *nextViewController= [segue destinationViewController];
        nextViewController.data = data_;
    }
}

・(B)のcloseボタンタップで(B)を閉じて(A)に戻る
  1. Controlキーを押しながらUIViewController(B)のcloseボタンから.hファイルにドラッグ&ドロップしてActionをつける。(Name:didTapClose, Type:id, Event:Touch Up Inside, Arguments:Sender)
  2. .mファイルのdidTapCloseで dismissViewControllerAnimated:completion: をコールする。
- (IBAction)didTapClose:(id)sender {
     [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}