使用导航控制器推送和弹出 ViewController:实现
像许多其他人一样,我今天开始编写一个实验,我将有两个视图控制器并能够在它们之间切换。我使用导航控制器让它工作,但我对实现有疑问。
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了解决这个问题,您可以创建一个回调类型的方法,该方法使用将为视图控制器发送请求的类的委托。最好通过代码解释...
RootViewController.h
RootViewController.m
RootInterfaceView.h
RootInterfaceView.m
或者,如果级别 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
RootViewController.m
RootInterfaceView.h
RootInterfaceView.m
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.