0

我正在使用此代码UIButtons在 a中添加 3 UIView,然后将它们添加到我的屏幕上。

UIView  buttonsContainerView = new UIView (new Rectangle (0,0,(int) this.View .Frame .Width ,40));
            UIButton  b;
            for(int i=0;i<3;i++){
                b= new RltTabBarButton ();
                b.ImageEdgeInsets = new UIEdgeInsets (5,5,5,100);
                if(i==0){
                    b.SetBackgroundImage (UIImage .FromFile ("ProjectRes/Images/Placeholder.png"),
                                          UIControlState.Normal );
                    b.TitleLabel .Text = "Details";
                    b.Frame = new RectangleF (0,350,108,40);
                    b.Enabled =true ;
                }else if(i==1){
                    b.SetBackgroundImage (UIImage .FromFile ("ProjectRes/Images/Placeholder.png"),
                                          UIControlState.Normal );
                    b.TitleLabel .Text = "Map";
                    b.Frame = new RectangleF (110,350,100,40);
                    b.Enabled =true ;
                }else if(i==2){


                    b.SetBackgroundImage (UIImage .FromFile ("ProjectRes/Images/Placeholder.png"),
                                          UIControlState.Normal );
                    b.TitleLabel .Text = "Agent";
                    b.Frame = new RectangleF (212,350,108,40);
                    b.Enabled =true ;
                }
                buttonsContainerView .AddSubview (b);

            }
this.View .AddSubview (buttonsContainerView );

当我运行我的应用程序时。添加到正确位置的按钮。但他们TextTitleLable. 虽然我设置了这个项目。而且它们看起来像一些图像,因此我无法单击它们并与它们进行交互。

我设置了他们的Enabled字段true,但没有任何更改。

编辑:根据答案,我将代码更改为:

UIView  buttonsContainerView = new UIView (new Rectangle (0,0,(int) this.View .Frame .Width ,40));

            UIButton  DetailsButton= new UIButton (UIButtonType.RoundedRect  );
            DetailsButton.ImageEdgeInsets = new UIEdgeInsets (5,5,5,100);
            DetailsButton.SetBackgroundImage (UIImage .FromFile ("ProjectRes/Images/Placeholder.png"),
                                  UIControlState.Normal );
            DetailsButton.SetTitle ("Details",UIControlState.Normal );
            DetailsButton.Frame = new RectangleF (0,350,108,40);
            buttonsContainerView .AddSubview (DetailsButton);

            UIButton  MapButton= new UIButton (UIButtonType.RoundedRect );
            MapButton.ImageEdgeInsets = new UIEdgeInsets (5,5,5,100);
            MapButton.SetBackgroundImage (UIImage .FromFile ("ProjectRes/Images/Placeholder.png"),
                                          UIControlState.Normal );
            MapButton.SetTitle ("Map",UIControlState.Normal );
            MapButton.Frame = new RectangleF (110,350,100,40);
            buttonsContainerView .AddSubview (MapButton);

            UIButton  AgentButton= new UIButton (UIButtonType.RoundedRect  );
            AgentButton.SetBackgroundImage (UIImage .FromFile ("ProjectRes/Images/Placeholder.png"),
                                          UIControlState.Normal );
            AgentButton.SetTitle ("Agent",UIControlState.Normal );
            AgentButton.Frame = new RectangleF (212,350,108,40);
            buttonsContainerView .AddSubview (AgentButton);

            this.View .AddSubview (buttonsContainerView );

但没有任何变化。我们无法点击按钮。它们看起来像图像。我相信它buttonsContainerViewEnableInputClicksWhenVisible. 当我在这个视图中调试应用程序时,调试器在想要执行它的值时会有一些异常。我能够复制这个的头:

MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[UIView enableInputClicksWh…

我不知道如何解决它。

4

3 回答 3

1

我知道了。问题是我将buttonsContainerView(包含按钮的视图)的高度设置为 40。但是设置了按钮 350 的高度位置。当按钮的框架位于其父视图的框架之外时,它不会响应您的触摸。

有关更多信息:UIView 内的 UIButton 不可点击

于 2013-03-05T08:48:59.903 回答
0

您需要使用 SetTitle(); UIButton 的功能,以使其工作。否则,不会设置 TitleLabel。

button.SetTitle("MyTitle",UIControlState.Normal);
于 2013-03-04T20:40:51.527 回答
0

这可能应该是一个评论,但我还不能评论。我发现你的实现很奇怪,我倾向于认为它会导致 ARC 行为不端(我假设 ARC,因为你没有任何发布代码)。为什么不直接创建 3 个 UIButton 而不是这个循环,然后将所有这些添加到您的 UIView 中?

于 2013-03-04T20:26:04.490 回答