执行pushViewController 是否需要UINavigationController?

发布于 2024-12-05 10:35:38 字数 362 浏览 0 评论 0原文

我想我的问题的答案是“是”,但我只是想得到大家的确认。

我有一个 UITableView,当我选择一行时,它应该“滑动”到另一个 UIViewController。 我之前已经在其他应用程序中完成过此操作(并且它们包含了 UINavigationController,尽管在 UITabView 中)。

所以我想知道: 为了使用 self.navigationController PushViewController 是否需要 UINavigationController ?

如果是这样(我再次怀疑这是真的),我必须在哪里定义 UINavigationController? 在 AppDelegate 中?在主/主 UIViewController 中?

I think the answer to my question is "Yes", but i just would like a confirmation from everyone.

I have a UITableView which should "slide" to another UIViewController when i select a row.
I've done this before with other apps (and they've included the UINavigationController, though in a UITabView).

So im wondering:
Is a UINavigationController required in order to use the self.navigationController pushViewController?

If so (which again i suspect to be true), where must I define the UINavigationController?
In the AppDelegate? In the main/primary UIViewController?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价 2024-12-12 10:35:38

是的,您需要有一个 UINavigationController 才能执行 PushViewController。但是,没有任何地方必须定义它。如果整个应用程序中的每个视图控制器都是一个 UINavigationController 的一部分,那么将其放在 App Delegate 中是有意义的。但是,您可以随时分配和使用视图控制器。 (请注意,它是 UIViewController 子类。)您的应用程序中还可以有多个 UINavigationController(这很常见,例如,如果您有选项卡栏)。所以你可以随时创建 UINavigationControllers。

如果您正在寻找呈现其他视图控制器的替代方法,您确实有一些不同的选择。例如,

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated

将允许您从一个视图控制器转换到另一个视图控制器,并且可以在没有 UINavigationController 的情况下使用。但我认为导航控制器通常是从一个视图控制器移动到另一个视图控制器的最佳方式。

Yes, you will need to have a UINavigationController in order to do pushViewController. However, there is no one place where you must define it. If every view controller in your whole application is part of one UINavigationController, then it makes sense to put it in the App Delegate. You can, however, allocate and use a view controller at any time. (Notice that it is a UIViewController subclass.) You can also have multiple UINavigationControllers in your app (which is common, for example, if you have a Tab bar). So you can create UINavigationControllers at any time.

If you are looking for alternate ways of present other view controllers, you do have some different options. For example,

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated

will allow you to transition from one view controller to another view controller, and can be used without a UINavigationController. But I think the Navigation Controller is very often the best way to move from one view controller to another.

绅刃 2024-12-12 10:35:38

简短的回答是:是的。

UINavigationController 的设计目的是管理屏幕上和屏幕外的一堆视图控制器并对其进行动画处理(与其他一些界面元素(例如导航栏或工具栏)结合使用)。传统上,UINavigationController 由 App Delegate 强烈持有和初始化,因为它们被视为最高优先级的根对象。显示 UINavigationController 正确用法的示例类可能如下所示:

#import <UIKit/UIKit.h>

@interface CFIExampleAppDelegate : NSResponder <UIApplicationDelegate>

@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) UINavigationController *navigationController;

@end

@implementation CFIExampleAppDelegate

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:UIScreen.mainScreen.bounds];
    self.navigationController = [[UINavigationController alloc]initWithRootViewController:/*Some Controller Instance*/];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];

    return YES;
}

@end

与往常一样,长的答案是“否”。UINavigationController

可以轻松地重新实现(并且已经多次实现),从而达到很好的效果。它就像子类化 UIViewController 并固定在某种堆栈上(例如 NSMutableArray)一样简单。

The short answer is: Yes.

UINavigationController was designed with the purpose of managing and animating a stack of view controllers on and off the screen (in conjunction with a few other interface elements such as a navigation bar, or a toolbar). Traditionally, UINavigationControllers are strongly held and initialized by the App Delegate, as they are considered a top-priority root object. An example class showing the proper usage of a UINavigationController might look like this:

#import <UIKit/UIKit.h>

@interface CFIExampleAppDelegate : NSResponder <UIApplicationDelegate>

@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) UINavigationController *navigationController;

@end

@implementation CFIExampleAppDelegate

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:UIScreen.mainScreen.bounds];
    self.navigationController = [[UINavigationController alloc]initWithRootViewController:/*Some Controller Instance*/];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];

    return YES;
}

@end

The long answer, as always, is No.

UINavigationController can be trivially reimplemented (and it has, many times) to great effect. It's as simple as subclassing UIViewController and bolting on some kind of stack (like an NSMutableArray).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文