代码之家  ›  专栏  ›  技术社区  ›  Akusete

Cocoa Touch中的程序化视图切换

  •  7
  • Akusete  · 技术社区  · 17 年前

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

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

    一个简单的例子,想象一个带有单个按钮的应用程序,当按下按钮时,将显示一个新视图,或者根据某些内部状态变量,可能显示多个视图中的一个。

    我还没有看到任何尝试这样做的例子,而且我似乎对UIViewController/UIWiew对象之间的关系和初始化过程了解得不够,无法通过编程实现这一点。

    7 回复  |  直到 17 年前
        1
  •  8
  •   davidavr    17 年前

    我用 presentModalViewController:animated: 从主窗口打开设置视图 UIViewController 然后当用户在我调用的设置视图中按下“完成”时 dismissModalViewControllerAnimated: 从设置视图(返回父视图),如下所示:

    [[self parentViewController] dismissModalViewControllerAnimated:YES];
    
        2
  •  3
  •   Fahim Parkar    13 年前

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

        3
  •  0
  •   Bill Heyman Bill Heyman    17 年前

    将通用UIView推入UINavigationController怎么样?

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

        4
  •  0
  •   Akusete    17 年前

    我发现了一个示例程序,它使用ModalView显示视图,而不将其嵌套为NavigatorControlView。

    iPhone Nav Bar Demo

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

        5
  •  0
  •   Andy Andy    17 年前

    NavBarDemo很有趣,但据我所知,最终modalView只是另一个从堆栈中弹出的视图。

    对于像地图应用程序这样的情况,当用户开始键入地址时,地图视图会切换到列出搜索选项的表视图,该怎么办?这两个视图在启动时是否已经启动,搜索结果表视图是否刚刚设置为可见性0,当您键入时,它是否切换为1,或者它实际上是在文本字段的touchUpInside事件中加载的?

    我将尝试使用UIView addSubview来重新创建此操作并发布我的结果

        6
  •  0
  •   Wolfgang Schreurs    15 年前

    您可以使用以下方法:

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

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

    BaseView。男性

    @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
    

    最后,所有视图都需要一个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

        7
  •  0
  •   Ronaldo Faria Lima    14 年前

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

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