Ios 在Cocoa Touch中以编程方式切换视图
如何在iPhone应用程序中以编程方式更改屏幕上的视图 我已经能够创建导航视图并以编程方式推/弹出它们以产生这种行为,但是如果我想简单地更改当前视图(不使用UINavigation控制器对象),那么实现这一点最简单的方法是什么 一个简单的例子,假设一个应用程序只有一个按钮,当按下按钮时将显示一个新的视图,或者可能显示多个视图中的一个,具体取决于某个内部状态变量Ios 在Cocoa Touch中以编程方式切换视图,ios,objective-c,iphone,cocoa-touch,Ios,Objective C,Iphone,Cocoa Touch,如何在iPhone应用程序中以编程方式更改屏幕上的视图 我已经能够创建导航视图并以编程方式推/弹出它们以产生这种行为,但是如果我想简单地更改当前视图(不使用UINavigation控制器对象),那么实现这一点最简单的方法是什么 一个简单的例子,假设一个应用程序只有一个按钮,当按下按钮时将显示一个新的视图,或者可能显示多个视图中的一个,具体取决于某个内部状态变量 我还没有看到任何尝试这样做的示例,而且我似乎对UIViewController/UIView对象之间的关系和初始化过程了解不够,无法通过
我还没有看到任何尝试这样做的示例,而且我似乎对UIViewController/UIView对象之间的关系和初始化过程了解不够,无法通过编程实现这一点。将通用UIView推入UINavigationController如何
当您想要显示一个特定视图时,只需将其作为子视图添加到先前推送的UIView。如果要更改视图,请删除以前的子视图并添加新的子视图。您需要浏览
-[UIView addSubview:][/code>和-[UIView removeFromSuperview]
。您的基本窗口是一个UIView
(子窗口),因此您可以向其添加和删除视图。我使用presentModalViewController:animated:
从主窗口的UIViewController
中调出设置视图,然后当用户按下“完成”时在设置视图中,我从设置视图(返回到父视图)调用dismissModalViewControllerAnimated:
,如下所示:
[[self parentViewController] dismissModalViewControllerAnimated:YES];
@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
我发现了一个示例程序,它使用ModalView显示视图,而不将视图嵌套为NavigatorControlView
上面的示例链接使用modalView作为信息链接。而且还为表视图链接使用导航链接。NavBarDemo很有趣,但从我所知,最后,modalView只是另一个被推下并从堆栈中弹出的视图
例如,当用户开始键入地址时,地图视图切换到列出搜索选项的表视图,这种情况又如何呢?这两个视图在启动时都已初始化,搜索结果表视图刚刚设置为可见性0,键入时切换为1,还是实际加载到文本字段的touchUpInside事件中
我将尝试使用UIView addSubview重新创建此操作并发布我的结果您可以使用以下方法:
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的代码可以如下所示:
[[self parentViewController] dismissModalViewControllerAnimated:YES];
@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
您可以在此处(在接下来的一小时内)下载一个示例应用程序:此提示已在上说明,但未给予任何重视。我发现自己很难理解它,它非常简单
只需将主窗口的rootViewController属性更改为拥有要显示的视图层次结构的任何视图控制器。这很简单。属性更新后,整个窗口内容将立即更改为新视图的内容。问得好。我一直在寻找这个答案。如果没有顶部的导航视图栏,是否可以这样做?如果可能的话,我希望能够用一个标准的UIView对象来实现这一点。实际上,我刚刚找到了一个这样做的例子,我正在来回答我自己的问题。谢谢。我一定错过了什么,因为每当我尝试使用该方法时,我都会遇到运行时异常。我会继续读下去。