Husein Behboudi Rad
Husein Behboudi Rad

Reputation: 5462

Adding a uiview that contions some uibuttons not works correctly iphone monotouch

I'm using this codes to add 3 UIButtons in a UIView and then add them to my screen.

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 );

When I run my application. Buttons added in their correct place. but they do not include any Text on their TitleLable. While I set this Item. Also they look like some images so that i can not click on them and interact with them.

I set their Enabled field true but not any changes.

EDIT: According to answers I changed my code to this:

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 );

But not any changes. we do not able to click on buttons. Their look like images. I believe that it is related to buttonsContainerView and maybe its EnableInputClicksWhenVisible. When I debug the app this view, debugger have some exeption when wants to execute its value. I was able to copy just head of this:

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

I'm not sure how to fix it.

Upvotes: 0

Views: 740

Answers (3)

Husein Behboudi Rad
Husein Behboudi Rad

Reputation: 5462

I got it. The problem is that I set the hieght of buttonsContainerView( The view that contains buttons ) 40. but is set the Height position of buttons 350. When button's frame is outside the frame of its parent view it wouldn't respond to your touches.

For more info: UIButton inside UIView not clickable

Upvotes: 1

You need to use SetTitle(); function of the UIButton in order to make it work. Otherwise, the TitleLabel won't be set.

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

Upvotes: 0

Kyle Jurick
Kyle Jurick

Reputation: 242

This should probably be a comment, but I cannot comment yet. I find your implementation odd and I tend to think it would cause ARC to misbehave (I assume ARC since you do not have any release code). Why not just create 3 UIButtons instead of this loop and then add all of those to your UIView?

Upvotes: 0

Related Questions