在 iOS 的 Objective-C 中,可以通过以下步骤实现多层推送返回2层:

  1. 创建一个 UIViewController 的子类,用于表示第一层的视图控制器(例如,FirstViewController)。

  2. 在 FirstViewController 中,添加一个按钮或其他触发方法的控件,用于推送到第二层视图控制器。

  3. 在按钮触发方法中,创建第二层视图控制器的实例(例如,SecondViewController)。

  4. 使用导航控制器(UINavigationController)来管理视图控制器的堆栈。

  5. 在 AppDelegate.m 文件中的 application:didFinishLaunchingWithOptions: 方法中,创建导航控制器的实例,并将第一层视图控制器设置为其根视图控制器。

  6. 在按钮触发方法中,将第二层视图控制器推送到导航控制器的堆栈中。

以下是一个简单的示例代码:

FirstViewController.m:

#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation FirstViewController

- (IBAction)pushToSecondViewController:(id)sender {
    SecondViewController *secondViewController = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secondViewController animated:YES];
}

@end

AppDelegate.m:

#import "AppDelegate.h"
#import "FirstViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    FirstViewController *firstViewController = [[FirstViewController alloc] init];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
    
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
    
    return YES;
}

@end

这样,当点击第一层视图控制器中的按钮时,将会推送到第二层视图控制器。

iOS Objective-C 多层推送返回2层详解

原文地址: https://www.cveoy.top/t/topic/qosy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录