TextField按钮动作不工作
本文关键字:工作 按钮 TextField | 更新日期: 2023-09-27 18:12:29
我创建了自定义类LoginView.cs UIView为用户名和密码添加了loginButton。
现在我添加了UIView Call到我的LoginViewController.cs类
当我构建并运行应用程序时,它显示登录框,两个文本字段和一个按钮。文本框上的操作不起作用。当文本框有焦点时键盘不会加载
这是我的自定义视图类loginview。cs
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace DemoHouse
{
class LoginView : UIView
{
UIView view_Login;
UITextField txt_username;
public readonly UITextField txt_password;
public readonly UIButton btn_Login;
public LoginView (string loginView){
float frame = UIScreen.MainScreen.Bounds.Width;
Add (view_Login = new UIView (new RectangleF (20, 60,frame-40, 200)) {
BackgroundColor = UIColor.Red,
});
float subFrame = view_Login.Bounds.Width;
view_Login.AddSubview(txt_username = new UITextField (new RectangleF (20, 20, subFrame-40, 31)){
BackgroundColor = UIColor.White,
});
view_Login.AddSubview(txt_password = new UITextField (new RectangleF (20, 70, subFrame-40, 31)){
BackgroundColor = UIColor.White
});
view_Login.AddSubview (btn_Login = new UIButton (new RectangleF (20, 120, 60, 31)) {
BackgroundColor = UIColor.Blue
});
}
}
}
这是LoginViewController.cs
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace DemoHouse
{
public partial class LoginViewController : UIViewController
{
LoginView loginView;
UIScrollView scrollView;
public override void DidReceiveMemoryWarning (){
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
#region View lifecycle
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
loginView = new LoginView("LoginView");
View.AddSubview (loginView);
// Perform any additional setup after loading the view, typically from a nib.
}
如果有什么问题请告诉我。这是正确的方法吗?我不想在我的应用程序中使用storyboard和xib。
@All
可能是一个愚蠢的解决方案,但添加一个点击按钮的手势。
login_btn.UserInteractionEnabled = true;
login_btn.AddGestureRecognizer (new UITapGestureRecognizer(()=>{
//Action In HERE
}));
键盘弹出是一个本地功能,所以我很敬畏你设法打破它。也许您应该尽量不要过多地凝聚代码。它减少了代码行数,但也增加了可读性和复杂性。另外,为什么不合并两个类呢?你已经有了ViewController
课程。试试这样做:
public partial class LoginViewController : UIViewController
{
private UIScrollView scrollView;
private UITextField txt_username, txt_password;
private UIButton btn_login;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
/*Depending on what else is on the screen, you can hard-code the
x, y, width and height, but in all other cases, use this:*/
scrollView = new UIScrollView (this.View.Bounds);
txt_username = new UITextField (new RectangleF (5, 5, 50, 25));
//Change other properties of the UITextfield to your liking here.
txt_password = new UITextField (new RectangleF (5, 35, 50, 25));
//Change other properties of the UITextfield to your liking here.
btn_login = new UIButton (new RectangleF (5, 65, 50, 25));
btn_login.TouchUpInside += Login;
//Change other properties of the UIButton to your liking here.
this.View.AddSubview (scrollView);
scrollView.AddSubviews (new UIView[]{txt_username, txt_password, btn_login});
}
void Login (object sender, eventargs e)
{
//Do something on button click
}
}
请记住,使用此解决方案,您实际上无法使用scrollView向下滚动,因为没有设置scrollView.ContentSize
。您可以实现一个方法,其中设置所有控件的框架大小。在改变方向时调用这个方法(在ViewDidRotate
方法中)。您也可以在此方法中添加scrollView.ContentSize
。