如何将 NavigationViewController 添加到使用 ViewController 模板创建的项目中

发布于 2024-09-26 23:30:38 字数 1454 浏览 2 评论 0原文

我使用 xCode 中的基于视图的应用程序模板创建了一个新应用程序。

我的第一个视图在加载应用程序时显示,是一个基于按钮的菜单。其中一个按钮 (Make) 应使用 NavigationViewController 加载新的导航堆栈。还有其他按钮在实现时会执行 NavigationViewController 范围之外的其他操作。

我希望能够单击“创建”并打开并显示一个新的导航控制器。

来自ViewController.m:

-(IBAction)makeStory:(id)sender{
NSLog(@"makeStory:");
navController = [[UINavigationController alloc] init];
makeStoryTableViewController = [[MakeStoryTableViewController alloc] initWithNibName:@"MakeStoryTableViewController" bundle:nil];

[navController pushViewController:makeStoryTableViewController animated:YES];

}

我在打开的ViewController.h文件中创建了一个NavigationViewController:

#import <UIKit/UIKit.h>
#import "MakeStoryTableViewController.h" 
@interface StoryBotViewController : UIViewController {
UINavigationController *navController;
MakeStoryTableViewController *makeStoryTableViewController;

}
- (IBAction)makeStory:(id)sender;
@end

我知道我错过了一些东西,因为当我调用pushViewController时什么也没有发生 - 我认为以某种方式我必须将NavigationViewController附加到makeStory:的ViewController上 作为参考

,我的应用程序委托标头在 @implementation 中声明视图控制器,如下所示:

    UIWindow *window;
    StoryBotViewController *viewController;

在应用程序委托的 .m 中使用适当的 @synthesize

@synthesize window;
@synthesize viewController;

如何从打开的视图控制器推送 NavigationStack? 如果问题有点模糊,请原谅我,如果您需要的话,我很乐意提供更多信息。这是我第一次在 stackoverflow 上提问,而且我显然是 iPhone SDK 的新手。

I have created a new application using the View Based Application Template in xCode.

My first view, which displays on loading the app, is a button based menu. One of the buttons (Make) should load a new navigation stack using a NavigationViewController. There are other buttons that when implemented will do other things outside of NavigationViewController's scope.

I would like to be able to click Make and open and display a new navigation controller.

From ViewController.m:

-(IBAction)makeStory:(id)sender{
NSLog(@"makeStory:");
navController = [[UINavigationController alloc] init];
makeStoryTableViewController = [[MakeStoryTableViewController alloc] initWithNibName:@"MakeStoryTableViewController" bundle:nil];

[navController pushViewController:makeStoryTableViewController animated:YES];

}

I have created a NavigationViewController in the opening ViewController.h file:

#import <UIKit/UIKit.h>
#import "MakeStoryTableViewController.h" 
@interface StoryBotViewController : UIViewController {
UINavigationController *navController;
MakeStoryTableViewController *makeStoryTableViewController;

}
- (IBAction)makeStory:(id)sender;
@end

I know I'm missing something because when I call pushViewController nothing happens - I think that somehow I have to attach the NavigationViewController to the ViewController that makeStory: is in.

For reference, my app delegate header declares the view controller in the @implementation as follows:

    UIWindow *window;
    StoryBotViewController *viewController;

with the appropriate @synthesize in the .m of the app delegate

@synthesize window;
@synthesize viewController;

How do I push a NavigationStack on from my opening view controller?
Please forgive me if the question is a little vague, I'm happy to provide more information if you need it. It's my first time questioning on stackoverflow and I'm obviously a bit of a newbie with the iPhone SDK.

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

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

发布评论

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

评论(1

晌融 2024-10-03 23:30:38

你几乎已经拥有了。但是您忘记将 NavigationController 添加到您的视图中。

[self.view addSubview:navController.view];

将其添加到您的 IBAction 中。


编辑:

但这很可能不是您想要的,因为您无法导航回第一个 viewController。如果您想导航回第一个 viewController,请像基于导航的模板一样设置您的项目。您可以通过编程方式实现以下目的:

将所有 UINavigationController 内容从视图控制器的头文件移动到应用程序委托头。并将您的应用程序委托实现更改为如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    // Override point for customization after application launch.
    navController = [[UINavigationController alloc] initWithRootViewController:viewController];
    // Add the view controller's view to the window and display.
    //    [window addSubview:viewController.view];
    [window addSubview:navController.view];
    [window makeKeyAndVisible];
    return YES;
}

确保在 dealloc 中释放 navController

然后将 IBAction 替换为如下所示:

- (IBAction)makeStory:(id)sender {
    makeStoryTableViewController = [[MakeStoryTableViewController alloc] initWithNibName:@"MakeStoryTableViewController" bundle:nil];
    [self.navigationController pushViewController:makeStoryTableViewController animated:YES];
}

如果您不喜欢在第一个视图控制器中显示导航栏,请隐藏它,但请确保取消隐藏它如果您将其他项目压入堆栈。 self.navigationController.navigationBar.hidden = ...

you had it almost. But you forgot to add the NavigationController to your view.

[self.view addSubview:navController.view];

Add this in your IBAction.


EDIT:

but much likely this is not what you want, because you can't navigate back to your very first viewController. If you want to navigate back to the first viewController set up your project like in the navigation-based template. You can to this programmatically to:

Move all UINavigationController stuff from the header file of the viewcontroller to the app delegate header. And change your app delegate implementation to something like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    // Override point for customization after application launch.
    navController = [[UINavigationController alloc] initWithRootViewController:viewController];
    // Add the view controller's view to the window and display.
    //    [window addSubview:viewController.view];
    [window addSubview:navController.view];
    [window makeKeyAndVisible];
    return YES;
}

make sure to release navController in dealloc

then replace your IBAction with something like this:

- (IBAction)makeStory:(id)sender {
    makeStoryTableViewController = [[MakeStoryTableViewController alloc] initWithNibName:@"MakeStoryTableViewController" bundle:nil];
    [self.navigationController pushViewController:makeStoryTableViewController animated:YES];
}

if you don't like to display the navigationbar in your first viewcontroller hide it, but make sure to unhide it if you push other items on the stack. self.navigationController.navigationBar.hidden = ...

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