【发布时间】:2014-07-10 23:27:36
【问题描述】:
我创建了自定义类 LoginView.cs UIView 为用户名和密码添加了 loginButton。
现在我将 UIView 调用添加到我的 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.
}
如果有什么问题,请告诉我。 这是正确的方法吗? 我不想在我的应用程序中使用故事板和 xib。
@全部 提前致谢。
【问题讨论】:
-
也许您想将问题重新表述为更具体的内容,因为此站点并非用于调试您的代码的地方。也许Code Review 是你要找的?
-
@MarcusWigert 我的问题对代码审查并不重要。创建自定义视图时遇到问题。在视图控制器中添加。在 iOS 中默认添加文本字段时,它将打开键盘侦听器。