Interface Builder中的多个视图状态 [英] Multiple view states in Interface Builder

查看:94
本文介绍了Interface Builder中的多个视图状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个有3种状态的屏幕:

I'm working on a screen that has 3 states:


  • 验证

  • 正在加载

  • 错误

前两个非常简单,因为只有标签文字更改。第三个是棘手的,因为我需要显示一条错误消息并在其上有一个重试按钮。

The first two are quite easy, since there is only a label text change. The third one is trickier tho, because I need to show an error message and have a Retry button on it.

另外,我想将所有这些都放在一个控制器下(我猜这很容易。)

Also, I would like to have all this under one Controller (which is the easy part, I guess.)

问题是:如何在Interface Builder中执行多种视图状态?有没有人这样做过?或者我是否以错误的方式解决这个问题?

The question is: how do I do multiple view states within Interface Builder? Has anyone done this before? Or am I going about this in the wrong way?

推荐答案

您可以尝试添加根据需要添加或删除的其他视图。在文件所有者,第一响应者,查看等窗口中,从库中添加三个UIView。您可能希望将其名称从查看更改为验证,加载和错误。

You might try adding additional views that you add or remove as required. In the window with "File's Owner", "First Responder", "View", etc, add three UIView's from the Library. You may want to change their names from "View" to "Validating", "Loading" and "Error".

现在打开每一个并通过添加按钮和标签以及其他类似的东西来自定义它。

Now open up each one and customize it as you like by adding buttons and labels and other such things.

返回XCode,声明新视图:

Back in XCode, declare the new views:

IBOutlet UIView *validView;
IBOutlet UIView *loadView;
IBOutlet UIView *errorView;

并确保在InterfaceBuilder中建立适当的连接。您希望从这些视图中链接的任何操作都应该很有效。

and be sure to make the appropriate connections in the InterfaceBuilder. Any actions that you wish to link up from any of these views should work great.

现在,切换,创建一个动作(或三个不同的动作)。这可以是IBAction或不是你喜欢的。在标题中:

Now, to switch, create an action (or three different ones). This can be an IBAction or not as you like. In the header:

-(void)showError;

现在执行,你可能需要这样的东西。

Now for the implementation, you may want something like this.

-(void)showError {
  // skip this if you always arrive from the validView
  if ([validView superview]) {
    [validView removeFromSuperview];
  } 
  [self.view addSubview:errorView];
}

如果您愿意,可以通过动画获得更多动力:

You can get fancier with animations, if you like:

    -(void)toggleErrorWithFlip {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.8];
    [UIView setAnimationTransition:([errorView superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight) 
                           forView:[self view]
                             cache:YES];
    if ([errorView superview]) {
        [errorView removeFromSuperview];
    } else {
        [[self view] addSubview:errorView];
    }
    [UIView commitAnimations];
}

重试按钮可能会触发以下行动:

The "Retry" button may trigger an action along the lines of the following:

-(IBAction)retryLoad {
  [errorView removeFromSuperview];
  [self.view addSubview:validView];
  // do some stuff that retries whatever was tried and failed
}

同样,这可以在有或没有动画的情况下发生。

Again, this can happen with or without animations.

如果有一个默认视图,一个你总是依赖的视图(例如validatingView),那么就把它作为原始的View,然后添加另外两个视图(例如loadView和errorView)。这可能会节省一些工作,具体取决于你想要的方式。

If there is a default view, one that you always fall back on (e.g. validatingView), then make that the original "View" and just add the other two views (e.g. loadView and errorView) on top of it. This may save a bit of work depending on how you want things to go.

这篇关于Interface Builder中的多个视图状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆