gpt4 book ai didi

ios - 不了解 NavigationController 和 UIViewControllers 在 iOS 中的工作方式

转载 作者:行者123 更新时间:2023-11-28 22:12:09 24 4
gpt4 key购买 nike

我有一个项目,我不太了解背后的 View 和导航。我从 AppDelegate (MAAppDelegate) 开始,我在其中定义属性:

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *detailViewController;

然后在 MAAppDelegate.m 中,我创建了一个 navigationController,并且

@implementation MAAppDelegate
@synthesize detailViewController;
@synthesize window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Init the navController for the Master Detail View of the grade cells

UINavigationController *navController = [[UINavigationController alloc] init];


detailViewController = [[UIViewController alloc] init];

UIViewController *viewController = [[MAController alloc] init];

navController = [[UINavigationController alloc] initWithRootViewController:viewController];

self.window.rootViewController = viewController;

[self.window makeKeyAndVisible];

return YES;
}

所以在这一点上,我想我有一个工作的 naviationController,我已经设置了一个自定义 UIViewController 的实例(自定义类 MAController) 并且我已将其设置为 rootViewController

然后,在我的 MAController 类中,我在该类中执行所有 我的 UI 工作(整个 UI 以编程方式完成,没有 nib 或 Storyboard)。这是 MAControllerviewDidLoad 的一部分:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.


[self.navigationController setNavigationBarHidden:YES]; // I commented this line out and realized it does nothing

我继续(在 viewDidLoad 中)向 self.view 添加一堆 subview ,就像这样

[self.view addSubview:self.backgroundImageView];

早些时候,我在 AppDelegate 类中创建了一个 viewController 并且它被称为 view,所以我认为它正在引用它,但现在因为我已经将它(在 AppDelegate 中)更改为 viewController,我想我想错了?

最后,我在“viewDidLoad”中创建了一个 UIView:

UIView *header = [[UIView alloc] initWithFrame:headerFrame];
header.backgroundColor = [UIColor clearColor];
self.tableView.tableHeaderView = header;

并开始向我创建的这个新 UIView 添加一堆 subview header

所以,简而言之,我不知道发生了什么。后来,当我尝试告诉(从 MAController 中的方法)self.navigationController(我假设它是 navigationController 负责我的一切项目 - 在 AppDelegate 开始时创建)到 pushViewController 一个新的 viewController,我打算将其用作表的 detailView,它变得奇怪了。

所以我只是想了解什么具有控制权,rootViewController 是什么,以及正在发生什么。

最佳答案

主窗口根设置为 View Controller 而不是导航 Controller

改变:

self.window.rootViewController = viewController;

到:

self.window.rootViewController = navController;

编辑:

您可以通过询问您的 appDelegate 从任何地方访问 navigationController。这通常不被认为是一种好的做法:

MAAppDelegate *delegate = (MAAppDelegate *)[[UIApplication sharedApplication] delegate];
UINavigationController *nav = delegate.navigationController;

不要忘记:

#import "MAAppDelegate.h"

关于ios - 不了解 NavigationController 和 UIViewControllers 在 iOS 中的工作方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22621843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com