我可以使用一个viewController而不是5吗?

时间:2022-03-30 13:46:09

I want the user to enter 5 details about himself but I don't want to use a boring 5 textfield form. I want the user to see one text field at a time -> click the button and move to the next text field. My question is, do I have to use 5 viewcontrollers for this? I'm trying to find a more elegant way.

我希望用户输入关于他自己的5个细节,但我不想使用枯燥的5文本字段表单。我希望用户一次看到一个文本字段 - >单击按钮并移动到下一个文本字段。我的问题是,我必须为此使用5个viewcontrollers吗?我正试图找到一种更优雅的方式。

I thought about something like setHidden:YES for every used textfield but it's not animated and looks kinda ugly so I'm looking for an alternative.

我想过像setHidden这样的东西:对于每个使用过的文本字段都是,但它不是动画的,看起来有点难看所以我正在寻找替代方案。

6 个解决方案

#1


1  

I think that you should use only one UIViewController (DetailViewController) with one UIView as container (DetailViewContainer), this container holds your 5 detailed UIView (DetailView). If you want specific transition animation between each DetailView you can implement your transition animation inside DetailViewContainer.

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

If I should to do same task as you I would do it in the following way:

如果我应该像你那样做同样的任务,我会用以下方式做:

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

#2


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.

所以有很多方法可以解决这个问题,但要专门回答你关于必须使用5个视图控制器的问题......没有。你不必。您可以创建一个说“... QuestionViewController”来处理显示问题,然后继续将该控制器的新实例推送到导航堆栈上,并提出不同的问题。

.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.

因此,在我的示例中,应用程序委托将管理下一个问题,然后问题视图控制器将简单地获取下一个问题并将其自身的新实例推送到视图堆栈。您可以将按钮连接到nextQuestion:(id)发送者操作。如果您决定坚持“每个控制器一个问题”的想法,这应该足以让您前进。

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!

希望这可以帮助!

#3


0  

You should use one ViewController per Page
If your textfields are on the same page, then you should use one viewController with 5 subviews.

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

Use view setHidden, but if you want ananimation, then simple animate to full transparent color (using view.alpha = 0.0) , then hide.

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

#4


0  

You can really go wild with this, and use something like a IBoutletCollection with your 5 UITextFields. You can then easily manage whats the correct UITextField to show (based on the index of the question):

你可以真的疯狂,并使用类似于你的5个UITextField的IBoutletCollection。然后,您可以轻松管理要显示的正确UITextField(基于问题的索引):

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

You can then use what Wojtek suggested: a nice animation with some effects.

然后你可以使用Wojtek建议的东西:带有一些效果的漂亮动画。

#5


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.

单个文本字段和标签(针对问题)可以。每当新问题到期时,您的单个视图控制器可以更改问题并清空文本字段。没有必要麻烦4个隐藏的视图/对视图和1可见。

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.

如果您想“免费”使用一些基本动画,那么请选择启用分页的UISlider,携带5对标签和文本字段。

#6


-1  

If you want simple fade-in/out transition you can do it this way

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

    [UIView animateWithDuration:duration animations:^{
    if(something)
    //fade in
    view.alpha = 1.f;
    else {
    //fade out
 view.alpha = 0.f;
    view.hidden = YES;
}];

#1


1  

I think that you should use only one UIViewController (DetailViewController) with one UIView as container (DetailViewContainer), this container holds your 5 detailed UIView (DetailView). If you want specific transition animation between each DetailView you can implement your transition animation inside DetailViewContainer.

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

If I should to do same task as you I would do it in the following way:

如果我应该像你那样做同样的任务,我会用以下方式做:

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

#2


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.

所以有很多方法可以解决这个问题,但要专门回答你关于必须使用5个视图控制器的问题......没有。你不必。您可以创建一个说“... QuestionViewController”来处理显示问题,然后继续将该控制器的新实例推送到导航堆栈上,并提出不同的问题。

.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.

因此,在我的示例中,应用程序委托将管理下一个问题,然后问题视图控制器将简单地获取下一个问题并将其自身的新实例推送到视图堆栈。您可以将按钮连接到nextQuestion:(id)发送者操作。如果您决定坚持“每个控制器一个问题”的想法,这应该足以让您前进。

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!

希望这可以帮助!

#3


0  

You should use one ViewController per Page
If your textfields are on the same page, then you should use one viewController with 5 subviews.

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

Use view setHidden, but if you want ananimation, then simple animate to full transparent color (using view.alpha = 0.0) , then hide.

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

#4


0  

You can really go wild with this, and use something like a IBoutletCollection with your 5 UITextFields. You can then easily manage whats the correct UITextField to show (based on the index of the question):

你可以真的疯狂,并使用类似于你的5个UITextField的IBoutletCollection。然后,您可以轻松管理要显示的正确UITextField(基于问题的索引):

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

You can then use what Wojtek suggested: a nice animation with some effects.

然后你可以使用Wojtek建议的东西:带有一些效果的漂亮动画。

#5


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.

单个文本字段和标签(针对问题)可以。每当新问题到期时,您的单个视图控制器可以更改问题并清空文本字段。没有必要麻烦4个隐藏的视图/对视图和1可见。

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.

如果您想“免费”使用一些基本动画,那么请选择启用分页的UISlider,携带5对标签和文本字段。

#6


-1  

If you want simple fade-in/out transition you can do it this way

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

    [UIView animateWithDuration:duration animations:^{
    if(something)
    //fade in
    view.alpha = 1.f;
    else {
    //fade out
 view.alpha = 0.f;
    view.hidden = YES;
}];