添加一个uiview contion一些uibuttons无法正常工作iphone monotouch

时间:2013-03-04 20:18:21

标签: ios iphone uiview xamarin.ios uibutton

我正在使用此代码在UIButtons中添加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 );

当我运行我的应用程序时。按钮添加在正确的位置。但他们Text上不包含任何TitleLable。我设置这个项目。它们看起来像是一些图像,因此我无法点击它们并与它们进行交互。

我设置了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 );

但不是任何改变。我们无法点击按钮。他们看起来像图像。我认为它与buttonsContainerView及其EnableInputClicksWhenVisible有关。当我调试应用程序这个视图时,调试器在想要执行其值时有一些例外。 我只能复制一下这个:

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

我不确定如何修复它。

3 个答案:

答案 0 :(得分:1)

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

了解更多信息:UIButton inside UIView not clickable

答案 1 :(得分:0)

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

答案 2 :(得分:0)

你需要使用SetTitle(); UIButton的功能是为了使它工作。否则,将不会设置TitleLabel。

button.SetTitle("MyTitle",UIControlState.Normal);