How to dismiss a subView generated by code? - ios

In my project I want to show a first run view when starting the app for te first time. To do this I added a UIView to my mainViewCntroller.m. To dismiss this (overlayed) view I put a button on the view called acceptButton. What code do I have to add to the Button to remove this view from the stack?
What do I do wrong?
Here's my code:
- (void) firstRun {
if (((AppDelegate*)[UIApplication sharedApplication].delegate).firstRun)
{
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
NSLog(#"%f", height);
CGRect myFrame = CGRectMake(0, 0, width, height);
UIView *myView = [[UIView alloc] initWithFrame:myFrame];
myView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
myView.tag = 12345;
[self.view addSubview:myView];
// Create a ScrollView and a label
UIScrollView *discScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20.0f, 0.0f, self.view.frame.size.width - 20, self.view.frame.size.height -70)];
discScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UILabel *discLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
discLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSString *labelText = NSLocalizedString (#"InfoText",#"");
[discLabel setText:labelText];
// Tell the label to use an unlimited number of lines
[discLabel setNumberOfLines:0];
[discLabel setNumberOfLines:0];
[discLabel setBackgroundColor:[UIColor clearColor]];
[discLabel setFont:[UIFont boldSystemFontOfSize:17]];
discLabel.textColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:1.0];
//[infoLabel sizeToFit];
CGSize infoLabelSize = [discLabel.text sizeWithFont:discLabel.font
constrainedToSize:CGSizeMake(discScroll.frame.size.width, 5000)
lineBreakMode:UILineBreakModeWordWrap];
discLabel.frame = CGRectMake(0, 0, infoLabelSize.width, infoLabelSize.height);
discScroll.contentSize = infoLabelSize;
[discScroll addSubview:discLabel];
[self.view addSubview:discScroll];
UIButton *acceptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
acceptButton.frame = CGRectMake(110, [[UIScreen mainScreen] bounds].size.height - 74, 100, 44);
// position in the parent view and set the size of the button
[acceptButton setTitle:#"Click Me!" forState:UIControlStateNormal];
// add targets and actions
[acceptButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[self.view addSubview:acceptButton];
}
}
-(IBAction)buttonClicked:(id)sender
{
[[self.myView viewWithTag:12345] removeFromSuperview];
}
In my mainViewController.h is the following property:
#property (weak, nonatomic) IBOutlet UIView *myView;
EDIT I've post the complete code. Maybe something else is in the way.

Ive written it in Xcode, following Code works for me.
Ive opened a new XCode Project (Single View based), and just added following Code:
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
myView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
myView.tag = 12345;
[self.view addSubview:myView];
UIButton *acceptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
acceptButton.frame = CGRectMake(0, 0, 100, 44);
[acceptButton setTitle:#"Click Me!" forState:UIControlStateNormal];
[acceptButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[myView addSubview:acceptButton];
[myView release];
}
- (void)buttonClicked{
[[self.view viewWithTag:12345] removeFromSuperview];
}
This worked for me in iPhone Simulator.... Just this code in the MainViewController.m nothing else... Just give it a try, hope it works for you too :-)

[acceptButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
when your method is called acceptButton, change to
[acceptButton addTarget:self action:#selector(acceptButton:) forControlEvents:UIControlEventTouchUpInside];

your target is set to buttonClicked but you are using acceptButton
[acceptButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)buttonClicked:(id)sender
{
[self.myView removeFromSuperview];
}

The problem lies with the [acceptButton addTarget:self action:#selector(buttonClicked:) line. Change buttonClicked to your method acceptButton: in that call.

[acceptButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
The exception is clear, there is no function named "buttonClicked"
Just change the part of your code with below code;
[acceptButton addTarget:self action:#selector(acceptButton:) forControlEvents:UIControlEventTouchUpInside];

just give it a tag...
UIView *myView = [[UIView alloc] initWithFrame:myFrame];
myView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
myView.tag = 12345;
[self.view addSubview:myView];
[myView release];
-(IBAction)acceptButton:(id)sender{
[[self.view viewWithTag:12345] removeFromSuperview];
}
Also change
action:#selector(buttonClicked:)
to
action:#selector(acceptButton:)

Related

Can't touch UIButton in UIScrollView

I am programmatically creating buttons in a view inside UIScrollView. I also have programmatically created UITextField. I can select and enter data into the text fields all day long. But I cannot get any sign the buttons are being touched. I can add them to the main view and they work. But when added to the view inside the UIScrollView, they are lost in another dimension. I've read plenty of info about setting userInteraction, delayContentTouches, and intercepting via gestureRecognizer. The last might be related to my problem. I return NO via [touch.view isDescendantOfView:self.myScrollViewName]. I can't get it to trigger via [touch.view isKindOfClass:[UIButton class]]. Which makes me think I have a more fundamental error I am missing?
related:
https://stackoverflow.com/a/6825839/4050489
edited to add button code per request:
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.myButton.frame = myButtonFrame;
self.myButton.enabled = interface.isActive;
self.myButton.userInteractionEnabled = YES;
self.myButton.exclusiveTouch = YES;
self.myButton.backgroundColor = [UIColor greenColor];
//self.myButton. = YES;
self.myButton.layer.borderWidth=1.0f;
self.myButton.layer.borderColor=[[UIColor blackColor] CGColor];
self.myButton.layer.cornerRadius = 10;
[self.myButton setTag:interface.tag];
[self.myButton setTitle:interface.Name forState:UIControlStateNormal];
[self.myButton addTarget:self action:#selector(navigatePartButtons:) forControlEvents:UIControlEventTouchUpInside];
[self.myButton setEnabled:YES]; //error check
[self.partSetupView addSubview:self.myButton];
partSetupView is a UIView in the UIScrollView.
Try this sample Code:
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *myScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, 300, 300)];
myScroll.backgroundColor = [UIColor redColor];
[self.view addSubview:myScroll];
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)];
myView.backgroundColor = [UIColor yellowColor];
[myScroll addSubview:myView];
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
myButton.backgroundColor = [UIColor blackColor];
[myView addSubview:myButton];
[myButton addTarget:self action:#selector(btnClick) forControlEvents:UIControlEventTouchDown];
}
-(void)btnClick{
NSLog(#"Button Click");
}
#end

touch events not working on programatically created view

I want to create the view with button, textbox, label programatically in viewdidload
But touch events are not working on view and its subviews even though i have set userinteractionenabled to YES.
Here is my code:
CGRect bounds = [[UIScreen mainScreen] bounds];
//creating a view
UIView* mainView = [[UIView alloc] initWithFrame: bounds];
[mainView setUserInteractionEnabled:YES];
[mainView setBackgroundColor: [UIColor yellowColor]];
CGRect buttonFrame = CGRectMake( 100, 80, 100, 50 );
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
myButton.frame = buttonFrame;
[myButton setBackgroundColor:[UIColor greenColor]];
myButton.userInteractionEnabled=YES;
[myButton addTarget: self
action: #selector(buttonClicked:)
forControlEvents: UIControlEventAllTouchEvents];
[myButton setTitle: #"My Button" forState: UIControlStateNormal];
//adding button to myview
[mainView addSubview: myButton];
//adding label to myview
UILabel* myLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
myLabel.text=#"my text";
[mainView addSubview:myLabel];
//adding textbox to myview
UITextField* textbox=[[UITextField alloc]initWithFrame:CGRectMake(100, 210, 200, 50)];
textbox.text=#"my text in textfield";
textbox.userInteractionEnabled=YES;
[mainView addSubview:textbox];
//finally adding my view to self.view
[self.view addSubview:mainView];
try this
//creating a view
UIView* mainView = [[UIView alloc] initWithFrame: bounds];
[mainView setUserInteractionEnabled:YES];
[mainView setBackgroundColor: [UIColor yellowColor]];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tappedOnView:)]; //add the gesture to get the touch event
tapGesture.numberOfTapsRequired = 1;
[mainView addGestureRecognizer:tapGesture];
CGRect buttonFrame = CGRectMake( 100, 80, 100, 50 );
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
myButton.frame = buttonFrame;
[myButton setBackgroundColor:[UIColor greenColor]];
myButton.userInteractionEnabled=YES;
[myButton addTarget: self
action: #selector(buttonClicked:)
forControlEvents: UIControlEventTouchUpInside];
[myButton setTitle: #"My Button" forState: UIControlStateNormal];
//adding button to myview
[mainView addSubview: myButton];
//adding label to myview
UILabel* myLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
myLabel.text=#"my text";
[mainView addSubview:myLabel];
//adding textbox to myview
UITextField* textbox=[[UITextField alloc]initWithFrame:CGRectMake(100, 210, 200, 50)];
textbox.text=#"my text in textfield";
textbox.userInteractionEnabled=YES;
[mainView addSubview:textbox];
//finally adding my view to self.view
[self.view addSubview:mainView];
modify the line
//UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
to
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
and add the method in your code
-(void)buttonClicked:(UIButton *)button
{
NSLog(#"hai");
}
and select the checkbox enable user interaction in interface builder its working fine.

add subView to another SubView ios

i am trying to show comments list by using subview. I added subview to self.view, and i added a tableview to it in order to show comments list. now i want to enable user to add comment, i tried to add another subview to the first one with text field and button, but the view shown up and the text field and button do not. this is the code i use to do so:
GRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
// notification button
myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth,screenHeight )];
[myView setBackgroundColor:[UIColor whiteColor]];
commentView = [[UIView alloc] initWithFrame:CGRectMake(0,screenHeight-70,screenWidth,70)];
[commentView setBackgroundColor:[UIColor redColor]];
table = [[UITableView alloc] initWithFrame:CGRectMake(0,50, screenWidth, screenHeight-100)];;
table.dataSource=self;
table.delegate=self;
UILabel *currentdate = [[UILabel alloc] initWithFrame:CGRectMake(0,10,screenWidth-40,50)];
currentdate.backgroundColor = [UIColor clearColor];
[currentdate setTextColor: [UIColor blueColor]];
[currentdate setText:#"Comments"];
currentdate.textAlignment= UITextAlignmentCenter;
currentdate.font = [UIFont fontWithName:#"Helvetica" size:20.0];
commentFeild = [[UITextField alloc] initWithFrame:CGRectMake(50,screenHeight-50,screenWidth-50,50)];
commentFeild.placeholder=#"add comment";
commentFeild.backgroundColor = [UIColor whiteColor];
[commentFeild setTextColor: [UIColor blueColor]];
commentFeild.textAlignment= UITextAlignmentRight;
commentFeild.font = [UIFont fontWithName:#"Helvetica" size:15.0];
[commentFeild.layer setCornerRadius:14.0f];
[commentFeild setTag:2030];
//[commentFeild setDelegate:self];
UIButton *doneBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
doneBtn.frame = CGRectMake(screenWidth-45, 10,40, 30);
doneBtn.backgroundColor = [ UIColor clearColor];
[doneBtn setTitle:#"Done" forState:UIControlStateNormal];
[doneBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[doneBtn addTarget:self action:#selector(hide) forControlEvents:UIControlEventTouchUpInside];
UIButton *add=[UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(0,screenHeight-50,40,50);
add.backgroundColor = [ UIColor clearColor];
[add setTitle:#"add" forState:UIControlStateNormal];
[add setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[add addTarget:self action:#selector(addComment) forControlEvents:UIControlEventTouchUpInside];
if(![taskObject.status isEqualToString:#"doneTask"])
{
[commentView addSubview:commentFeild];
[commentView addSubview:add];
}
[myView addSubview:doneBtn];
[myView addSubview:currentdate];
[myView addSubview:table];
[myView addSubview:commentView];
[self.view addSubview:myView];
You are adding commentFeild in commentView.
commentView = [[UIView alloc] initWithFrame:CGRectMake(0,screenHeight-70,screenWidth,70)];
Height of commentView is 70 and y position of commentField is screenHeight-50 this y position is to large as compare to commentView height.
x and y position of child view is calculated from parents x and y.
The frame of the commentField is causing the problem as the y value is very large the comment textfield is not displayed in the commentview
Correct the frame and it will show up
commentFeild = [[UITextField alloc] initWithFrame:CGRectMake(50,0,screenWidth-50,50)];
note :frame of a view is depending only to its parent view not any supers of the parent,In your case commentField's frame depends on comment view dimensions and nothing to do with any other superview

How to programmatically add objects/views on a UIScrollView?

I've been using Interface Builder and storyboard to make my application, but for this signature capturing API, everything was done in code.
I'm trying to implement it to my application, but I can't figure out how to add an UIView and Buttons on to my scrollview.
I got it to appear in my view controller, but it isn't connected to the scrollview at all; it appears on top.
Here's my code.
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat padding = self.view.frame.size.width/15;
UIView *autoGraphView = [[[UIView alloc] initWithFrame:CGRectMake(padding, 300, 280, 160)] autorelease];
autoGraphView.layer.borderColor = [UIColor lightGrayColor].CGColor;
autoGraphView.layer.borderWidth = 3;
autoGraphView.layer.cornerRadius = 10;
[autoGraphView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:autoGraphView];
self.autoGraph = [T1Autograph autographWithView:autoGraphView delegate:self];
[autoGraph setLicenseCode:#"4fabb271f7d93f07346bd02cec7a1ebe10ab7bec"];
[autoGraph setShowDate:YES];
[autoGraph setShowHash:YES];
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// init withFrame:CGRectMake (50, 300, 200, 60)];
[clearButton setFrame:CGRectMake(padding, 480, 130, 30)];
[clearButton setTitle:#"Clear" forState:UIControlStateNormal];
[clearButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clearButton addTarget:self action:#selector(clearButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clearButton];
UIButton *autoDone = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// initWithFrame:CGRectMake (50, 300, 200, 60)];
[autoDone setFrame:CGRectMake(150 + padding, 480, 130, 30)];
[autoDone setTitle:#"Done" forState:UIControlStateNormal];
[autoDone setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[autoDone addTarget:self action:#selector(doneButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:autoDone];
}
You need to add the subviews to the scrollview, if the root view of your viewcontroller is not the scrollview, you need to get access to it one way or another, typically through an IBOutlet, and add them that way

Custom UIButton not working

After looking through the post on this simple topic I still can not figure out what I've done wrong here. I am trying to make the buttons loop through the same action in the same viewController.
When the view comes onto the screen the buttons do not click (or at least the NSLog does not write to the console which it does the first time it enters this code.)
- (IBAction)answer: (id) sender{
NSLog(#"The Number is: %d\n",qNumber);
qNumber+=1;
UIView *newView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[newView setUserInteractionEnabled:TRUE];
UIImage *buttonImage = [UIImage imageNamed:#"pink_button.png"];
UIImageView *buttonImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(23, 294, 72, 37)] autorelease];
[buttonImageView setImage:buttonImage];
UILabel* buttonLabel = [[[UILabel alloc] initWithFrame:CGRectMake(23, 294, 72, 37)] autorelease];
buttonLabel.text = #"newLow";
buttonLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size: 15.0];
buttonLabel.textColor = [UIColor blackColor];
buttonLabel.backgroundColor = [UIColor clearColor];
buttonLabel.textAlignment = UITextAlignmentCenter;
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[lowButton addSubview:buttonImageView];
[lowButton addSubview:buttonLabel];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
self.view = newView;
}
Thanks for any help you can provide...even if I missed something simple :-)
Latest Updated Code:
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[newView addSubview:buttonImageView];
[newView addSubview:buttonLabel];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
[self.view bringSubviewToFront:lowButton];
self.view = newView;
Still when the button is pressed it does not enter the action code.
You button needs a frame. You can't tap a frameless button.
lowButton.frame = CGRectMake(23, 294, 72, 37);
Also, when you add label and image to that button, their frame should have x and y 0.
UIImageView *buttonImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 72, 37)] autorelease];
...
UILabel* buttonLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 72, 37)] autorelease];
...
But may I ask why you are adding new UIImageView and UILabel to UIButton when it has it's label and background view properties?
Edit here is a cleaned-up code for you:
UIView *newView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
lowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[lowButton setImage:[UIImage imageNamed:#"pink_button.png"] forState:UIControlStateNormal];
[lowButton setTitle:#"newLow" forState:UIControlStateNormal];
[lowButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[lowButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0]];
[lowButton addTarget:self action:#selector(answer:) forControlEvents:UIControlEventTouchUpInside];
[newView addSubview:lowButton];
self.view = newView;
Notice I also removed [newView setUserInteractionEnabled:TRUE]; as it's not needed since it's enabled by default.
Your custom button's subviews are covering the button, so it does not get any clicks.
Try adding all the subviews to newView rather than to lowButton (including lowButton itself) and then call
[self.view bringSubViewToFront:lowButton];

Resources