iOSは2ページ目を表示します

0.プロジェクトを作成し、シングルビューアプリを選択します

1.アイテム「新規ファイル...」を選択します。

[表示]、[次へ]、[作成]の順に選択すると、xibファイルが表示されます

2.新しいView2Controller.mファイルとView2Controller.hファイルを作成し、コンテンツを追加します

//View2Controller.h
#import <UIKit/UIKit.h>

@interface View2Controller : UIViewController


@end
//View2Controller.m

#import "View2Controller.h"


@implementation View2Controller

    

@end

3. xibファイルのコントローラークラスを設定し、xibファイルを選択して、クラスにView2Controllerを書き込みます。

 

4.ファイルの所有者とビューインターフェイスを関連付け、+をインターフェイスにドラッグします

 

5. 2番目のページを開きたい他のViewControllerクラスで、ボタンコントロールのイベントを実装します

- (IBAction)OnChangePageEvent:(id)sender {
    
    //initWithNibName后+xib文件名
     View2Controller *vc2=[[View2Controller alloc]initWithNibName:@"View2" bundle:nil];
    //设置页面的出现方式
    vc2.modalPresentationStyle= UIModalPresentationOverCurrentContext;
     //呈现出vc2
     [self presentViewController:vc2 animated:YES completion:^{
     }];
    
}


 

6. 2ページ目のコードを閉じて、2ページ目に実装します


#import "View2Controller.h"


@implementation View2Controller

- (IBAction)OnBackFirstPage:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:^{
          NSLog(@"%s","关闭第二页");
     }];
}


@end

 

おすすめ

転載: blog.csdn.net/m0_37981386/article/details/106791852