Objective-C 视图和文本字段的基类

发布于 2024-12-06 05:47:19 字数 1148 浏览 0 评论 0原文

所以我正在开发一个程序,里面有多个小程序。主窗口显示所有小程序,目前只有一个在工作,但它们都有些相似,所以我单击“TimeEntry”按钮,这样我就会进入另一个视图控制器:

(IBAction)btnTe_clicked:(id)sender 
{

TELogin *ViewTELogin = [[TELogin alloc] init];


ViewTELogin.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;


[self presentModalViewController:ViewTELogin animated:YES];

[ViewTELogin release];

}

然后输入一些数据,验证并转到下一个视图控制器,如果我想返回,我使用:

- (IBAction)btnMenu_clicked:(id)sender

{

[[self parentViewController] dismissModalViewControllerAnimated:YES];

}

到目前为止一切顺利,迷你应用程序可以工作,但代码很难看,在每个视图控制器中我必须设置一个滚动视图,一些常量和 UITextField像这样的方法可以防止文本字段被键盘隐藏:

-(void)textFieldDidEndEditing:(UITextField *)textField
{

CGRect viewFrame =self.view.frame;
viewFrame.origin.y += animatedDistance; //animated distance is a CGFloat
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];

}

我很确定可以创建一个实现滚动视图和文本字段委托的基类,然后对于每个视图控制器,我只需导入基类并设计我的界面。但到目前为止我尝试的一切都只是浪费时间,这就是我寻求帮助的原因。

So I'm developing a program where there are multiple little program inside. The main window show all the mini program, for the moment there is only one working but they will all be somewhat similar, so i click on the 'TimeEntry' button wich brings me to another view controller that way:

(IBAction)btnTe_clicked:(id)sender 
{

TELogin *ViewTELogin = [[TELogin alloc] init];


ViewTELogin.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;


[self presentModalViewController:ViewTELogin animated:YES];

[ViewTELogin release];

}

and then enter some data, validate and go to the next view controller, if i want to go back i use:

- (IBAction)btnMenu_clicked:(id)sender

{

[[self parentViewController] dismissModalViewControllerAnimated:YES];

}

so far so good, the mini app works, but the code is ugly, in each view controller I have to set a Scroll View, some constant and UITextField method like that to prevent a text field to be hidden by the keyboard:

-(void)textFieldDidEndEditing:(UITextField *)textField
{

CGRect viewFrame =self.view.frame;
viewFrame.origin.y += animatedDistance; //animated distance is a CGFloat
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];

}

And I'm pretty sure it's possible to make one Base class which implements the scroll view and textfield delegate and then for each view controller i would just have to import the base class and design my interface. But everything i tried so far was just a waste of time that's why i'm calling for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美男兮 2024-12-13 05:47:19

您熟悉 MVC 文档?

如果没有,请尝试创建自己的 View 类和 Model 类。模型类将保存您的数据,也许还有其他属性(取决于您的设计),而视图类将保存 UITextField 和 ViewController 中已有的任何其他视图。

Are you familiar with the MVC documentation?

If not, try making your own View class and Model class. The model class would hold your data and maybe other properties (depending on your design) and the View class would hold the UITextField and any other views already inside the ViewController.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文