【问题标题】:TextField Button action is not workingTextField 按钮操作不起作用
【发布时间】: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 中默认添加文本字段时,它将打开键盘侦听器。

标签: c# ios xamarin


【解决方案1】:

可能是一个愚蠢的解决方案,但在按钮上添加一个点击手势。

login_btn.UserInteractionEnabled = true;
login_btn.AddGestureRecognizer (new UITapGestureRecognizer(()=>{
//Action In HERE
}));

【讨论】:

  • 它没有触发事件。
【解决方案2】:

弹出的键盘是本机功能,所以我很敬畏你设法打破它。也许你应该尽量不要压缩你的代码。它可以减少代码行数,但也会增加可读性和复杂性。另外,为什么不合并两个类呢?你已经有一个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

我希望这些信息对您有所帮助。祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多