使用导航控制器推送和弹出 ViewController:实现

发布于 2024-11-10 03:54:21 字数 1499 浏览 2 评论 0原文

像许多其他人一样,我今天开始编写一个实验,我将有两个视图控制器并能够在它们之间切换。我使用导航控制器让它工作,但我对实现有疑问。

在我的 TwoViewsAppDelegate 中,我定义了导航控制器和 rootViewController。

@interface TwoViewsAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    UINavigationController *navigationController;
    RootViewController *rootViewController;
}

并按如下方式设置它们:

- (BOOL)application:(UIApplication *)application     didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    rootViewController = [[RootViewController alloc] init];
    navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    [window setRootViewController:navigationController];
    [self.window makeKeyAndVisible];

    return YES;
}

然后在我的 rootViewController 中,我定义了我的 level2ViewController 我要切换到,以及一个我要按下的按钮 切换发生:

@interface RootViewController : UIViewController {

    UIButton *theButton;
    Level2ViewController *level2ViewController;
}

这是对 RootViewController.m 中按下按钮的响应:

-(void)level1ButtonPressed:(id)sender
{
    if (level2ViewController == nil)
    {
        level2ViewController = [[Level2ViewController alloc] init];
    }

    [self.navigationController pushViewController:level2ViewController animated:YES];
}

问题是,如果有一个 level3ViewController, 它必须被定义为 level2ViewController 的成员,等等。 对于我想推入堆栈的许多视图控制器。
如果能够在一个视图控制器中定义所有视图控制器就好了 地方,最好是应用程序委托。这可能吗?

Like many others I started to code an experiment today where I would have two view controllers and be able to switch between them. I got this to work using a navigation controller, but I have a question about the implementation.

In my TwoViewsAppDelegate, I define the navigation controller and the rootViewController.

@interface TwoViewsAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    UINavigationController *navigationController;
    RootViewController *rootViewController;
}

and set them up as follows:

- (BOOL)application:(UIApplication *)application     didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    rootViewController = [[RootViewController alloc] init];
    navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    [window setRootViewController:navigationController];
    [self.window makeKeyAndVisible];

    return YES;
}

Then in my rootViewController, I define the level2ViewController that I
am going to switch to, and a button that I'm going to press to make the
switch happen:

@interface RootViewController : UIViewController {

    UIButton *theButton;
    Level2ViewController *level2ViewController;
}

Here's the response to the button being pressed in RootViewController.m:

-(void)level1ButtonPressed:(id)sender
{
    if (level2ViewController == nil)
    {
        level2ViewController = [[Level2ViewController alloc] init];
    }

    [self.navigationController pushViewController:level2ViewController animated:YES];
}

The problem is that if there was going to be a level3ViewController,
it would have to be defined as a member of level2ViewController, etc.
for however many view controllers i wanted to push onto the stack.
It would be nice to be able to define all the view controllers in one
place, preferably the app Delegate. Is this possible?

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

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

发布评论

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

评论(1

不喜欢何必死缠烂打 2024-11-17 03:54:21

为了解决这个问题,您可以创建一个回调类型的方法,该方法使用将为视图控制器发送请求的类的委托。最好通过代码解释...

RootViewController.h

#import "RootInterfaceView.h"
// all the other VC imports here too
@interface RootViewController : UIViewController <RootInterfaceViewDelegate>
{
    RootInterfaceView *interface;
}

RootViewController.m

-(void)rootInterfaceView: (RootInterfaceView*)rootInterfaceView didSelectItem:(NSUInteger)itemTag
{
    switch (itemTag)
    // then create the matching view controller
}

RootInterfaceView.h

// imports here if required
@protocol RootInterfaceViewDelegate;

@interface RootInterfaceView : UIView <RootInterfaceItemViewDelegate>
{
    id <RootInterfaceViewDelegate> delegate;
}

@property (nonatomic, assign) id delegate;

@end

@protocol RootInterfaceViewDelegate <NSObject>

@optional
-(void)rootInterfaceView: (RootInterfaceView*)rootInterfaceView didSelectItem:(NSUInteger)itemTag;

@end

RootInterfaceView.m

// remember to synthesize the delegate
-(void)rootInterfaceItemSelected: (RootInterfaceItemView*)rootInterfaceItemView
{
    NSUInteger theTag = rootInterfaceItemView.tag;
    if ([self.delegate respondsToSelector:@selector(rootInterfaceView:didSelectItem:)])
        [self.delegate rootInterfaceView:self didSelectItem:theTag];
}

或者,如果级别 2 的唯一选项要么返回 root/pop 一个 VC,要么推送控制器 3,那么就可以了级别 2 将导入 3 以允许其创建。

To solve this, you can create a callback-type method which uses the delegate of the class that'll be sending the requests for the view controllers. Best explained through code...

RootViewController.h

#import "RootInterfaceView.h"
// all the other VC imports here too
@interface RootViewController : UIViewController <RootInterfaceViewDelegate>
{
    RootInterfaceView *interface;
}

RootViewController.m

-(void)rootInterfaceView: (RootInterfaceView*)rootInterfaceView didSelectItem:(NSUInteger)itemTag
{
    switch (itemTag)
    // then create the matching view controller
}

RootInterfaceView.h

// imports here if required
@protocol RootInterfaceViewDelegate;

@interface RootInterfaceView : UIView <RootInterfaceItemViewDelegate>
{
    id <RootInterfaceViewDelegate> delegate;
}

@property (nonatomic, assign) id delegate;

@end

@protocol RootInterfaceViewDelegate <NSObject>

@optional
-(void)rootInterfaceView: (RootInterfaceView*)rootInterfaceView didSelectItem:(NSUInteger)itemTag;

@end

RootInterfaceView.m

// remember to synthesize the delegate
-(void)rootInterfaceItemSelected: (RootInterfaceItemView*)rootInterfaceItemView
{
    NSUInteger theTag = rootInterfaceItemView.tag;
    if ([self.delegate respondsToSelector:@selector(rootInterfaceView:didSelectItem:)])
        [self.delegate rootInterfaceView:self didSelectItem:theTag];
}

Alternatively, if the only options from level 2 were either back to root/pop one VC or to push controller 3, then it'd be fine for level 2 to be importing 3 to allow for it's creation.

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