2

我希望用户输入 5 个关于他自己的详细信息,但我不想使用无聊的 5 个文本字段表单。我希望用户一次看到一个文本字段-> 单击按钮并移至下一个文本字段。我的问题是,我必须为此使用 5 个视图控制器吗?我试图找到一种更优雅的方式。

setHidden:YES为每个使用过的文本字段考虑了类似的东西,但它不是动画的,看起来有点难看,所以我正在寻找一个替代方案。

4

6 回答 6

1

我认为你应该只用一个UIViewController (DetailViewController)和一个UIView作为容器(DetailViewContainer),这个容器包含你的 5 个详细信息UIView (DetailView)。如果你想在每个之间实现特定的过渡动画,DetailView你可以在里面实现你的过渡动画DetailViewContainer

如果我应该做和你一样的任务,我会按照以下方式做:

typedef enum
{
    firstDetailView,
    secondDetailView,
        ...
}DetailViewType;

@protocol DetailViewDelegate <NSObject>

- (void)detailViewDidEndEditingWithValue:(NSString)value detailViewType:(DetailViewType)detailViewType;

@end

//

@interface DetailViewController : UIViewController <DetailViewDelegate>
@end

@implementation DetailViewController
- (void)detailViewDidEndEditingWithValue:(NSString)value detailViewType:(DetailViewType)detailViewType
{
    //save new detail to model here
}
@end

//

@interface DetailView : UIView
@property(nonatomic, weak) id<DetailViewDelegate> delegate;
@end

@implementation DetailView
- (void)saveDetailValue
{
    [delegate detailViewDidEndEditingWithValue:self.value detailViewType:self.type];
}
@end
于 2012-12-12T19:01:58.643 回答
1

So there are tons of ways you could approach this, but to specifically answer your question about having to use 5 view controllers... no. You wouldn't have to. You could create a say... "QuestionViewController" that handles displaying the question and then keeps pushing new instances of that controller onto the navigation stack with different questions.

.h

@interface QuestionViewController : UIViewController

@property (nonatomic, strong) NSString *question;

- (IBAction) nextQuestion:(id)sender;

@end

.m

#import "QuestionViewController.h"
#import "AppDelegate.h"


@implementation QuestionViewController

@synthesize question = _question;


- (IBAction)nextQuestion:(id)sender
{
    AppDelegate *delegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];

    QuestionViewController *vc = [[QuestionViewController alloc]     initWithNibName:@"QuestionViewController"
                                                                          bundle:nil];

    vc.question = [delegate getNextQuestion];

    [self.navigationController pushViewController:vc
                                         animated:YES];
}

@end

So in my example, the app delegate would manage which question comes next and then the question view controller would simply fetch the next question and push a new instance of itself onto the view stack. You could wire a button up to the nextQuestion:(id)sender action. This should be enough to get you going if you decide to stick with the "one question per controller" idea.

Like I said, I'm not weighing in on what type of design is the best. I'm just saying if you want to do it the way you described... this would keep you from having to create a bunch of similar controllers.

Hope this helps!

于 2012-12-12T19:11:09.017 回答
0

每个页面应该使用一个 ViewController
如果您的文本字段在同一页面上,那么您应该使用一个具有 5 个子视图的 viewController。

使用view setHidden,但如果你想要动画,那么简单的动画到完全透明的颜色(使用view.alpha = 0.0),然后隐藏。

于 2012-12-12T18:23:31.603 回答
0

你真的可以疯狂地使用它,并IBoutletCollection在你的 5中使用类似 a 的东西UITextFields。然后,您可以轻松管理UITextField要显示的正确内容(基于问题的索引):

Question 1 => UITextField (0)
Question 2 => UITextField (1)

然后,您可以使用 Wojtek 建议的内容:带有一些效果的漂亮动画。

于 2012-12-12T18:25:49.147 回答
0

A single text field and label (for the question) would do. Your single view controller could change the question and empty the text field every time when a new question is due. There is no need to hassle with 4 hidden views/pairs of views and 1 visible.

If you want to go for some basic animations "for free" then go for a UISlider with paging enabled, carrying 5 pairs of label and text field.

于 2012-12-12T19:45:13.087 回答
-1

如果你想要简单的淡入/淡出过渡,你可以这样做

    [UIView animateWithDuration:duration animations:^{
    if(something)
    //fade in
    view.alpha = 1.f;
    else {
    //fade out
 view.alpha = 0.f;
    view.hidden = YES;
}];
于 2012-12-12T18:22:45.610 回答