LoginSignup
1
2

More than 5 years have passed since last update.

iOS 途中からUINavagationControllerを適用する。(Objective-C)

Last updated at Posted at 2016-12-14

タイトル画面→遷移→UINavigationControllerのように途中からナビゲーションバーを出す。(ヘッダーファイルはいじってないので省略)

ViewController.m

#import "ViewController.h"
#import "NextViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor grayColor];
    UILabel *label= [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMidX(self.view.bounds)-130, CGRectGetMidY(self.view.bounds)-20, 260, 40)];
    label.text = @"最初のビューコントローラーさん";
    label.textColor = [UIColor whiteColor];
    [self.view addSubview:label];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    NextViewController *next = [[NextViewController alloc]init];
    [self presentViewController:next animated:YES completion:nil];
}
@end

スクリーンショット 2016-12-14 20.07.10.png

NextViewController.m

#import "NextViewController.h"
#import "ThirdViewController.h"

@interface NextViewController ()

@end

@implementation NextViewController

- (void)viewDidLoad {
    self.view.backgroundColor = [UIColor yellowColor];
    UILabel *label= [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMidX(self.view.bounds)-140, CGRectGetMidY(self.view.bounds)-20, 280, 40)];
    label.text = @"ネクストビューコントローラーさん";
    label.textColor = [UIColor blackColor];
    [self.view addSubview:label];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    ThirdViewController *third = [[ThirdViewController alloc]init];
    UINavigationController *nv = [[UINavigationController alloc]initWithRootViewController:third];
    [self presentViewController:nv animated:YES completion:nil];
}

@end

スクリーンショット 2016-12-14 20.12.39.png

ThirdViewController.m

#import "ThirdViewController.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    self.title = @"サードビューコントローラーさん";
    // Do any additional setup after loading the view.
}

@end

スクリーンショット 2016-12-14 20.07.28.png

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2