以编程方式切换Cocoa Touch中的视图

时间:2008-12-15 23:23:43

标签: ios objective-c iphone cocoa-touch

如何在iPhone应用程序中以编程方式更改屏幕上的视图?

我已经能够创建导航视图并以编程方式推送/弹出它们以产生这种行为,但是如果我只想更改当前视图(不使用UINavigation控制器对象),那么实现这一目标的最佳方法是什么? ?

一个简单的例子,想象一个具有单个按钮的应用程序,按下时将显示一个新视图,或者可能是多个视图中的一个视图,具体取决于某个内部状态变量。

我还没有看到任何试图这样做的例子,我似乎对UIViewController / UIView对象之间的关系和初始化过程没有足够的理解来实现这一点。

7 个答案:

答案 0 :(得分:8)

我使用presentModalViewController:animated:从我的主窗口UIViewController调出设置视图,然后当用户在设置视图中按“完成”时,我从设置视图中调用dismissModalViewControllerAnimated:(回到父视图)这样:

[[self parentViewController] dismissModalViewControllerAnimated:YES];

答案 1 :(得分:3)

您需要探索-[UIView addSubview:]-[UIView removeFromSuperview]。您的基本窗口是UIView(后代),因此您可以添加和删除视图。

答案 2 :(得分:0)

如何将通用UIView推入UINavigationController?

当您想要显示一个特定视图时,只需将其作为子视图添加到之前推送的UIView中。如果要更改视图,请删除上一个子视图并添加新视图。

答案 3 :(得分:0)

我找到了一个示例程序,它使用ModalView来显示视图而不将它们嵌套为NavigatorControlView。

iPhone Nav Bar Demo

上面的示例链接使用modalView作为信息链接。并且还使用导航链接进行表格视图链接。

答案 4 :(得分:0)

NavBarDemo很有意思,但最后,modalView只是另一个从我所知道的内容推出并弹出堆栈的视图。

地图应用程序的情况如何,当用户开始输入地址时,地图视图会切换到列出搜索选项的表格视图?这两个视图在启动时是否已初始化,搜索结果表视图是否设置为可见性0,当您键入时,它切换为1,还是实际加载到文本字段的touchUpInside事件中?

我会尝试搞乱UIView addSubview来重新创建这个动作并发布我的结果

答案 5 :(得分:0)

您可以使用以下方法:

BaseView.h - 所有其他视图都从此视图继承:

@interface BaseView : UIView {}
@property (nonatomic, assign) UIViewController *parentViewController;
@end

BaseView.m

@implementation BaseView
@synthesize parentViewController;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

- (void)dealloc {
    [self setParentViewController:nil];
    [super dealloc];
}

@end

其他视图可以从BaseView继承。他们的代码应该类似于以下内容:

@implementation FirstView

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        [self setBackgroundColor:[UIColor yellowColor]];

        UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 30.0f)];
        [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button1 setBackgroundColor:[UIColor whiteColor]];
        [button1 setCenter:CGPointMake(160.0f, 360.0f)];
        [button1 setTitle:@"Show Second View" forState:UIControlStateNormal];
        [button1 addTarget:self action:@selector(switchView:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button1];
        [button1 release];      

        UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 30.0f)];
        [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button2 setBackgroundColor:[UIColor whiteColor]];
        [button2 setCenter:CGPointMake(160.0f, 120.0f)];
        [button2 setTitle:@"Show Sub View" forState:UIControlStateNormal];
        [button2 addTarget:self action:@selector(showView:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button2];
        [button2 release];              
    }

    return self;
}

- (void)showView:(id)sender {
    if (viewController == nil) {
        viewController = [[SubViewController alloc] initWithNibName:nil bundle:nil];        
    }

    [self addSubview:viewController.view];
}

- (void)switchView:(id)sender {
    SecondView *secondView = [[SecondView alloc] initWithFrame:self.frame];
    [self.parentViewController performSelector:@selector(switchView:) withObject:secondView];
    [secondView release];
}

- (void)dealloc {
    [viewController release];
    [super dealloc];
}

@end

最后,您需要为所有视图使用1个UIViewController。将调用switchView:方法来更改当前视图。 UIViewController的代码可能是这样的:

@implementation ViewController

- (void)loadView {
    FirstView *firstView = [[FirstView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];    
    [firstView setParentViewController:self];
    [self setView:firstView];
    [firstView release];
}

- (void)switchView:(BaseView *)newView {
    [newView setParentViewController:self];
    [self retain];
    [self setView:newView];
    [self release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}  

- (void)dealloc {
    [super dealloc];
}

@end

您可以在此处(下一小时内)下载示例应用:http://dl.dropbox.com/u/6487838/ViewSwitch.zip

答案 6 :(得分:0)

这个提示是在Apple's documentation上说的,但不重要。我发现自己很难理解它,这很简单。

只需将主窗口的rootViewController属性更改为拥有您要显示的视图层次结构的任何视图控制器。这很简单。一旦属性更新,整个窗口内容就会更改为新视图的内容。

相关问题