Gabriel
Gabriel

Reputation: 328

Adding Custom UIButton To subView

I have subview which it initiated by touch on the UIbutton in the main view. Once the subview pops up it displays a label with the info I provided. This all works. I just need an UIbutton to show up so I can then set it up to dismiss the subview and go back to the main view. i have searched all over and have found nothing that helps.

Here is what my code in my .m file looks like:

////Button that Displays Subview with Its Label and Button
- (IBAction)showInfo:(id)sender
/////UI Subview

{UIView *mySubview = [[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 480)];

[self.view addSubview:mySubview];
mySubview.backgroundColor = [UIColor blackColor];
mySubview.alpha = .7f;

//////Label in SubView

{UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 50)];
label.text = @"Here is info";
label.textColor = [UIColor whiteColor];
label.backgroundColor= [UIColor clearColor];

[self.view addSubview:label];


////Button in Subview

//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"button-info-x.png"]     forState:UIControlStateNormal];

//set the position of the button
button.frame = CGRectMake(120, 252, 68, 68);


//add the button to the view
[self.view addSubview:button];

Now I just need to add an action to the UIButton to dismiss the Subview?

Upvotes: 0

Views: 8347

Answers (3)

Lineesh K Mohan
Lineesh K Mohan

Reputation: 1712

Add Target to the custom button

{

.....

    [button addTarget:self action:@selector(your dismiss:) forControlEvents:UIControlEventTouchUpInside];

.......

}
-(IBAction)your dismiss:(id)sender 
{

   // write the dismissal code here

}

Upvotes: 0

Apple
Apple

Reputation: 550

First Take IBOutlet of subView and add it to the main View.

 IBOutlet UIView *subView;

Then, add UIButton to the SubView like this:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

//set the position of the button
button.frame = CGRectMake(0, 0, 100, 30);

//set the button's title
[button setTitle:@"Click Me!" forState:UIControlStateNormal];

[btnMenu addTarget:self action:@selector(your MethodName:) forControlEvents:UIControlEventTouchUpInside];

//add the button to the view
[subView addSubview:button];

Then,

-(IBAction)your MethodName:(id)sender {


}

In Above Method Remove SubView from Main View.

Upvotes: 4

Madhu
Madhu

Reputation: 2384

If you're simply looking to set an image for a UIButton instead of a title, you're looking for the - (void)setImage:(UIImage *)image forState:(UIControlState)statemethod.

You should check the UIButton reference for more info.

Upvotes: 0

Related Questions