以编程方式切换自定义视图(推送和弹出)

时间:2022-06-14 13:07:25

In iOS, my views work individually but I can't switch between them.

在iOS中,我的视图可以单独工作,但我不能在它们之间切换。

Now after a lot of google-ing around I've fond that the navigation based app would work great with the stack for views. The problem is all my views are nib/xib-less and custom tailored in the source code. Because of that I need my own UINavigationController and push and pop views by hand/code. Since every tutorial is either nib/xib and IB bound or just a mash of code snippets I need a concrete example how to do it.

在谷歌上搜索了很多次之后,我发现基于导航的应用程序在视图栈上运行得很好。问题是我的所有视图都是nib/xib,并且是在源代码中定制的。正因为如此,我需要我自己的UINavigationController,以及手动/代码的push和pop视图。由于每个教程都是nib/xib和IB绑定的,或者只是代码片段的组合,所以我需要一个具体的示例来说明如何实现它。

A simple example with 2 programmatically created view (can be empty just have to use loadView instead of initializing with a nib/xib) and a working stack (a push and a pop of the views like: load app,create some root view if needed, push one view and from that view push the second one and then pop them) would be awesome, or at least a tutorial in that way with the whole source of the project and not snippets.

与2以编程方式创建一个简单的示例视图(可以空只需要使用未经初始化的nib / xib)和一个工作堆栈(推和流行的观点:加载应用程序,创建一些根视图如果需要,推动一个视图,从这一观点推动第二个流行),然后将令人敬畏,或者至少一个教程与整个项目的来源而不是片段。

Thanks in advance.

提前谢谢。

EDIT: After some extra thinking, a little more clarification wouldn't be bad. My app will basically consist of 5 or 6 views which will be called form their respective previous view, i.e. a drill-down app.

编辑:经过一些额外的思考,再多做一点澄清也不错。我的app基本上由5到6个视图组成,这些视图将从他们之前的视图中被调用,也就是一个下钻应用。

1 个解决方案

#1


4  

Here's a brief code, only the essential parts:

这里有一个简短的代码,只有基本的部分:

CodeViewsPushingAppDelegate.m

CodeViewsPushingAppDelegate.m

#import "CodeViewsPushingAppDelegate.h"
#import "ViewNumberOne.h"
@implementation CodeViewsPushingAppDelegate

@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
    UINavigationController *navController = [[UINavigationController alloc] init];
    ViewNumberOne *view1 = [[ViewNumberOne alloc] init];
    [navController pushViewController:view1 animated:NO];
    [self.window addSubview:navController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

ViewNumberOne.h

ViewNumberOne.h

#import <UIKit/UIKit.h>

@interface ViewNumberOne : UIViewController
{
    UIButton *button;
}

- (void)pushAnotherView;

@end

ViewNumberOne.m

ViewNumberOne.m

#import "ViewNumberOne.h"
#import "ViewNumberTwo.h"
@implementation ViewNumberOne

- (void)loadView
{
    [super loadView];
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(110, 190, 100, 20);
    [button setTitle:@"Push Me!" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(pushAnotherView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)pushAnotherView;
{
    ViewNumberTwo *view2 = [[ViewNumberTwo alloc] init];
    [self.navigationController pushViewController:view2 animated:YES];
    [view2 release];
}

ViewNumberTwo.h

ViewNumberTwo.h

#import <UIKit/UIKit.h>

@interface ViewNumberTwo : UIViewController
{
    UILabel *label;
}

@end

ViewNumberTwo.m

ViewNumberTwo.m

#import "ViewNumberTwo.h"

@implementation ViewNumberTwo

- (void)loadView
{
    [super loadView];
    label = [[UILabel alloc] init];
    label.text = @"I am a label! This is view #2";
    label.numberOfLines = 2;
    label.textAlignment = UITextAlignmentCenter;
    label.frame = CGRectMake(50, 50, 200, 200); //whatever
    [self.view addSubview:label];
}

#1


4  

Here's a brief code, only the essential parts:

这里有一个简短的代码,只有基本的部分:

CodeViewsPushingAppDelegate.m

CodeViewsPushingAppDelegate.m

#import "CodeViewsPushingAppDelegate.h"
#import "ViewNumberOne.h"
@implementation CodeViewsPushingAppDelegate

@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
    UINavigationController *navController = [[UINavigationController alloc] init];
    ViewNumberOne *view1 = [[ViewNumberOne alloc] init];
    [navController pushViewController:view1 animated:NO];
    [self.window addSubview:navController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

ViewNumberOne.h

ViewNumberOne.h

#import <UIKit/UIKit.h>

@interface ViewNumberOne : UIViewController
{
    UIButton *button;
}

- (void)pushAnotherView;

@end

ViewNumberOne.m

ViewNumberOne.m

#import "ViewNumberOne.h"
#import "ViewNumberTwo.h"
@implementation ViewNumberOne

- (void)loadView
{
    [super loadView];
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(110, 190, 100, 20);
    [button setTitle:@"Push Me!" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(pushAnotherView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)pushAnotherView;
{
    ViewNumberTwo *view2 = [[ViewNumberTwo alloc] init];
    [self.navigationController pushViewController:view2 animated:YES];
    [view2 release];
}

ViewNumberTwo.h

ViewNumberTwo.h

#import <UIKit/UIKit.h>

@interface ViewNumberTwo : UIViewController
{
    UILabel *label;
}

@end

ViewNumberTwo.m

ViewNumberTwo.m

#import "ViewNumberTwo.h"

@implementation ViewNumberTwo

- (void)loadView
{
    [super loadView];
    label = [[UILabel alloc] init];
    label.text = @"I am a label! This is view #2";
    label.numberOfLines = 2;
    label.textAlignment = UITextAlignmentCenter;
    label.frame = CGRectMake(50, 50, 200, 200); //whatever
    [self.view addSubview:label];
}