Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7159107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:10:32+00:00 2026-05-28T13:10:32+00:00

When I created a UIView in my superview, I want to add a button

  • 0

When I created a UIView in my superview, I want to add a button to this view that act as an exit button.
So when pressed, it will remove this UIView from my superview, how should I do that?
The View and button in that view is created programmatically when a button is pressed

- (IBAction)skillButtonPressed:(UIButton *)sender 
{
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(40, 130, 240, 232)];
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [cancelButton setFrame:CGRectMake(0, 0, 32, 32)];
    [cancelButton setTitle:@"x" forState:UIControlStateNormal];

    [myView addSubview:cancelButton];
    [self.view addSubview:myView];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:10:33+00:00Added an answer on May 28, 2026 at 1:10 pm

    In your class that creates the view, add a function like this:

    //tag for subview - make it unique
    #define SUBVIEW_TAG 9999 
    
    - (void) handleExit
    {
        UIView * subview = [self.view viewWithTag:SUBVIEW_TAG];
        [subview removeFromSuperview];
    }
    

    and when creating the subview do:

    - (IBAction)skillButtonPressed:(UIButton *)sender 
    {
        UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(40, 130, 240, 232)];
        UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [cancelButton addTarget:self action:@selector(handleExit) forControlEvents:UIControlEventTouchUpInside];
        [cancelButton setFrame:CGRectMake(0, 0, 32, 32)];
        [cancelButton setTitle:@"x" forState:UIControlStateNormal];
    
        [myView addSubview:cancelButton];
        [myView setTag:SUBVIEW_TAG];
        [self.view addSubview:myView];
    
        //don't forget to release if you are not using ARC!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a UIView through code and want to add some images and
How can i remove an ImageView added on a dynamically created UIView, so that
I created a subclass from UIView #import <UIKit/UIKit.h> @interface MeeSelectDropDownView : UIView { UILabel
I'm planning to create a UIView with UIButtons and I want that UIView to
I'm developing an iPhone application. I have a class that inherits from UIView .
Lets say that I have 4 view controllers (call them FirstView,SecondView,ThirdView,FourthView) which are created
I have created a UIView as a subview and to that subview I have
I've created a single UIView (We'll call it Person) in an UIViewController that accepts
I have created a custom subclass of UIView for an interface element that I
I created a subclass of UITableViewCell and added two identical UIView s that have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.