我创建了自定义类LoginView.cs UIView为用户名和密码添加了loginButton。
现在我将UIView Call添加到我的LoginViewController.cs类
中当我构建并运行应用程序时,它会显示登录框,两个文本字段和一个按钮。文本字段上的操作无效。当文本框具有焦点时,键盘无法加载
这是我对loginview.cs的自定义View类
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 提前谢谢。
答案 0 :(得分:0)
可能是一个愚蠢的解决方案,但为按钮添加了一个轻击手势。
login_btn.UserInteractionEnabled = true;
login_btn.AddGestureRecognizer (new UITapGestureRecognizer(()=>{
//Action In HERE
}));
答案 1 :(得分:0)
键盘弹出是一个原生功能,所以我很敬畏你设法打破它。也许你应该尽量不要压缩你的代码。它减少了代码行数,但也增加了可读性和复杂性。另外,为什么不合并两个类?您已经有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
。
我希望这些信息有所帮助。祝你好运!