How to fix a button at the bottom of the tableview? - ios

In my App i want to fix a button at the bottom of the table view.
Here is my Initial Screen,
Button created at the footer section
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if(tableView == educateTbl)
{
float footerWidth = 150.0f;
float padding = 10.0f;
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, footerWidth, 50.0)];
footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIButton *addEdu = [[UIButton alloc]initWithFrame:CGRectMake(padding, 0, footerWidth - 2.0f * padding, 44.0f)];
addEdu.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
addEdu.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[addEdu setTitle:#"Add Education" forState:UIControlStateNormal];
[addEdu addTarget:self action:#selector(addEducation:) forControlEvents:UIControlEventTouchUpInside];
[addEdu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
addEdu.frame=CGRectMake(0, 0, 200, 30);
[footerView addSubview:addEdu];
return footerView;
}
return nil;
}
After that i am getting like this,
How can i fix that?

why you dont make something like this?
-(void)setTableFooter
{
float footerWidth = 150.0f;
float padding = 10.0f;
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, footerWidth, 50.0)];
footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIButton *addEdu = [[UIButton alloc]initWithFrame:CGRectMake(padding, 0, footerWidth - 2.0f * padding, 44.0f)];
addEdu.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
addEdu.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[addEdu setTitle:#"Add Education" forState:UIControlStateNormal];
[addEdu addTarget:self action:#selector(addEducation:) forControlEvents:UIControlEventTouchUpInside];
[addEdu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
addEdu.frame=CGRectMake(0, 0, 200, 30);
[footerView addSubview:addEdu];
tableView.tableFooterView = footerView;
}
and call this after you init the table

If you want section footer/header scroll with tableview, you need to use a grouped style table view.
[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped]
And a grouped style table view will have a default backgroundColor and section header/footer height. You need set these value explicitly.
If you have only one section, it is better to use tableFooterView instead of section footer view

While Scrolling that button visible means ,UITableview cell are reusable so that button is available ,what you have to do means .
1.Take number of row in UITableView ,
Then inside cell for row at indexpath :
Total number of row ==indexpath.row
{
//inside this button.hidden=no;
}
else
{
//button.hidden=yes;
}

-(void)CreateCustomFooter
{
float footerWidth = 150.0f;
float padding = 10.0f;
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, footerWidth, 50.0)];
footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIButton *addEdu = [[UIButton alloc]initWithFrame:CGRectMake(padding, 0, footerWidth - 2.0f * padding, 44.0f)];
addEdu.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
addEdu.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[addEdu setTitle:#"Add Education" forState:UIControlStateNormal];
[addEdu addTarget:self action:#selector(addEducation:) forControlEvents:UIControlEventTouchUpInside];
[addEdu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
addEdu.frame=CGRectMake(0, 0, 200, 30);
[footerView addSubview:addEdu];
tableView.tableFooterView = footerView;
}
Put above method in viewDidLoad() Method and after initializing tableview

Related

viewForFooterInSection not showing into the bottom of the screen

viewForFooterInSection not showing into the bottom of the screen(uitableview).
here I am using UItableview.
Note: in below screen vary bottom of the screen one more footer added.(white color view).
Redcolorview: footer (from below code)
blue color: table background color
white bottom:
Red color view I added programmatically, using below code:
- (CGFloat)tableView:(UITableView *)tableView
estimatedHeightForFooterInSection:(NSInteger)section
{
return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
NSLog(#"%f",tableView.frame.size.width);
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)];
view.backgroundColor = [UIColor redColor];
UIButton *cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 7, tableView.frame.size.width/2 - 60, 30)];
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancelButton setBackgroundColor:[ValidationsClass colorWithHexString:NSLocalizedStringFromTableInBundle(#"pothysapp_label_text_backgroundcolor",#"ApplicationSettings",[NSBundle mainBundle], nil)]];
[cancelButton addTarget:self
action:#selector(cancelDetails:)
forControlEvents:UIControlEventTouchUpInside];
cancelButton.titleLabel.font = [UIFont fontWithName:#"Roboto-Bold" size:15];
cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[view addSubview:cancelButton];
UIButton *saveButton = [[UIButton alloc]initWithFrame:CGRectMake(tableView.frame.size.width/2 + 30, 7, tableView.frame.size.width/2 - 60, 30)];
[saveButton setTitle:#"Save" forState:UIControlStateNormal];
[saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[saveButton setBackgroundColor:[ValidationsClass colorWithHexString:NSLocalizedStringFromTableInBundle(#"pothysapp_label_text_backgroundcolor",#"ApplicationSettings",[NSBundle mainBundle], nil)]];
[saveButton addTarget:self
action:#selector(saveDetails:)
forControlEvents:UIControlEventTouchUpInside];
saveButton.titleLabel.font = [UIFont fontWithName:#"Roboto-Bold" size:15];
saveButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[view addSubview:saveButton];
return view;
}
Try this instead of returning your custom view in viewForFooterInSection.
Add your custom view to table footer in ViewdidLoad()
self.tableview.tableFooterView =//Your CustomView;
I removed estimatedHeightForFooterInSection and viewForFooterInSection.
Instead of this i added view in viewdidload:
self.tableview.contentInset = UIEdgeInsetsMake(0, 0, -20, 0);
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 15, self.view.frame.size.width, 30)];
view.backgroundColor = [UIColor whiteColor];
self.tableview.tableFooterView = view;
UIButton *cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 7, self.view.frame.size.width/2 - 60, 30)];
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[view addSubview:cancelButton];
UIButton *saveButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 + 30, 7, self.view.frame.size.width/2 - 60, 30)];
[saveButton setTitle:#"Save" forState:UIControlStateNormal];
[saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[view addSubview:saveButton];

Displaying a custom line over view

Hi I want to show a line in over view like given image.
But when i am doing this i m getting line in complete view .
My code-
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(2,30, _MovetoSearch.frame.size.width, 1)];
lineView.backgroundColor = [UIColor whiteColor];
[_MovetoSearch addSubview:lineView];
How to make line like above image ?
When you set child view frame that time always set it using parent view frame so if in future you can change parent frame then no need to change child frame its automatically change as per parent view like I write as follow.
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(_MovetoSearch.frame.origin.x+2,_MovetoSearch.frame.size.height-5, _MovetoSearch.frame.size.width-30, 1)];
lineView.backgroundColor = [UIColor whiteColor];
[_MovetoSearch addSubview:lineView];
I assume your searchBtn is having similar kind of code
UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[searchBtn setFrame:CGRectMake(_MovetoSearch.frame.size.width-32, 0, 32, 32)];
[searchBtn setImage:[UIImage imageNamed:#"search.png"] forState:UIControlStateNormal];
[searchBtn setImage:[UIImage imageNamed:#"search.png"] forState:UIControlStateHighlighted];
[searchBtn addTarget:self action:#selector(searchBtnPressed) forControlEvents:UIControlEventTouchUpInside];
For your UITextField should be
CGFloat fieldHeight = searchBtn.frame.size.height-2;
CGFloat fieldWidth = _MovetoSearch.frame.size.width - (searchBtn.frame.size.width + 4);
UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(2, 0, fieldWidth, fieldHeight)];
[textfield setBackgroundColor:[UIColor clearColor]];
[textfield setBorderStyle:UITextBorderStyleNone];
[textfield setNeedsDisplay];
[_MovetoSearch addSubview:textfield];
for line view
CGFloat fieldHeight = searchBtn.frame.size.height-2;
CGFloat fieldWidth = _MovetoSearch.frame.size.width - (searchBtn.frame.size.width + 4);
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(2, fieldHeight+1, fieldWidth, 1)];
lineView.backgroundColor = [UIColor whiteColor];
[_MovetoSearch addSubview:lineView];
Hope it will work for you
I would suggest to use layer for such task and not overuse UIView.
CALayer * lineLayer = [CALayer alloc]init];
[lineLayer setFrame:CGRectMake(x,y,width,1)];
[lineLayer setBackgroundColor:[UIColor whiteColor].CGColor];
[self.view.layer addSublayer:lineLayer];

Adjusting the height of a uitableview section footer

I am just starting to use a UITableView section footer, but am having some problems. So I have a UITableView with an edit button attached to the bottom using a footer. My first problem is that when you click on that button, the footer should double in size because the edit button then becomes an add button and a done button, instead of just one button. What would be the best way to make the size of the footer bigger. I have tried changing the frame of the footer through self.tableView.tableFooterView.frame = CGRectMake(0, 0, 320, 88); but this does not work.
My second problem is that I would like the footer to always scroll with the table, like it is just another cell. It should just be located at the bottom of the table no matter what. If the user scrolls down, it should not stop at the bottom of the screen and stick, it should just keep scrolling with the rest of tableview. How would I fix this?
Here is how I am creating the footer:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 48;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
//view.backgroundColor = [UIColor grayColor];
self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.addButton.frame = CGRectMake(0, 0, 320, 44);
[self.addButton setImage:[UIImage imageNamed:#"Add_Class_Button"] forState:UIControlStateNormal];
[self.addButton setImage:[UIImage imageNamed:#"Add_Class_Button"] forState:UIControlStateHighlighted];
[self.addButton addTarget:self action:#selector(addSelected:) forControlEvents:UIControlEventTouchUpInside];
self.addButton.hidden = YES;
[view addSubview:self.addButton];
self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.editButton.frame = CGRectMake(0, 0, 320, 44);
[self.editButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateNormal];
[self.editButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateHighlighted];
[self.editButton addTarget:self action:#selector(editSelected:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:self.editButton];
self.editDoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.editDoneButton.frame = CGRectMake(0, self.addClassButton.frame.size.height-2, 320, 44);
[self.editDoneButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateNormal];
[self.editDoneButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateHighlighted];
[self.editDoneButton addTarget:self action:#selector(doneSelected) forControlEvents:UIControlEventTouchUpInside];
self.editDoneButton.hidden = YES;
[view addSubview:self.editDoneButton];
self.editLabel = [[UILabel alloc] init];
self.editLabel.frame = CGRectMake(10, 5, 100, 30);
self.editLabel.text = #"Edit";
self.editLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
[view addSubview:self.editLabel];
self.addLabel = [[UILabel alloc] init];
self.addLabel.frame = CGRectMake(10, 5, 100, 30);
self.addLabel.text = #"Add";
self.addLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
self.addLabel.hidden = YES;
[view addSubview:self.addLabel];
self.editDoneLabel = [[UILabel alloc] init];
self.editDoneLabel.frame = CGRectMake(10, self.addClassButton.frame.size.height + 5, 150, 30);
self.editDoneLabel.text = #"Done";
self.editDoneLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
self.editDoneLabel.hidden = YES;
[view addSubview:self.editDoneLabel];
self.theLine = [[UIView alloc] init];
self.theLine.frame = CGRectMake(0, 0, 320, .5);
UIColor *borderColor = [UIColor colorWithRed:193/255.5 green:193/255.0 blue:193/255.0 alpha:1.0];
self.theLine.backgroundColor = borderColor;
[view addSubview:self.theLine];
return view;
}
Edit
After the taking into account the answers bellow, this is what I have switched to, but now where my footer used to be, there is just a grey rectangle.
Here is the code that I am now using:
-(void)setUpFooter {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 48)];
view.backgroundColor = [UIColor redColor];
self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.addButton.frame = CGRectMake(0, 0, 320, 44);
[self.addButton setImage:[UIImage imageNamed:#"Add_Class_Button"] forState:UIControlStateNormal];
[self.addButton setImage:[UIImage imageNamed:#"Add_Class_Button"] forState:UIControlStateHighlighted];
[self.addButton addTarget:self action:#selector(addSelected:) forControlEvents:UIControlEventTouchUpInside];
self.addButton.hidden = YES;
[view addSubview:self.addButton];
self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.editButton.frame = CGRectMake(0, 0, 320, 44);
[self.editButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateNormal];
[self.editButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateHighlighted];
[self.editButton addTarget:self action:#selector(editSelected:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:self.editButton];
self.editDoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.editDoneButton.frame = CGRectMake(0, self.addClassButton.frame.size.height-2, 320, 44);
[self.editDoneButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateNormal];
[self.editDoneButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateHighlighted];
[self.editDoneButton addTarget:self action:#selector(doneSelected) forControlEvents:UIControlEventTouchUpInside];
self.editDoneButton.hidden = YES;
[view addSubview:self.editDoneButton];
self.editLabel = [[UILabel alloc] init];
self.editLabel.frame = CGRectMake(10, 5, 100, 30);
self.editLabel.text = #"Edit";
self.editLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
[view addSubview:self.editLabel];
self.addLabel = [[UILabel alloc] init];
self.addLabel.frame = CGRectMake(10, 5, 100, 30);
self.addLabel.text = #"Add";
self.addLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
self.addLabel.hidden = YES;
[view addSubview:self.addLabel];
self.editDoneLabel = [[UILabel alloc] init];
self.editDoneLabel.frame = CGRectMake(10, self.addClassButton.frame.size.height + 5, 150, 30);
self.editDoneLabel.text = #"Done";
self.editDoneLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
self.editDoneLabel.hidden = YES;
[view addSubview:self.editDoneLabel];
self.theLine = [[UIView alloc] init];
self.theLine.frame = CGRectMake(0, 0, 320, .5);
UIColor *borderColor = [UIColor colorWithRed:193/255.5 green:193/255.0 blue:193/255.0 alpha:1.0];
self.theLine.backgroundColor = borderColor;
[view addSubview:self.theLine];
view.userInteractionEnabled = YES;
self.classTableView.tableFooterView = view;
self.classTableView.tableFooterView.userInteractionEnabled = YES;
}
hope I can help.
To set the section footer height you also have to change the returned value in the method heightForFooterInSection. To reload the height you can call [tableView reloadData] or [tableView reloadSections:...].
A table section footer always stick to the bottom of the screen but there is a different view used to place things after the table view.
The tableView.tableFooterView is a view that is located after the table view. Change this to add elements after the table view.
Cheers!
There are two types of footers in a UITableView. There is the table footer at the bottom of the table, and there is the table section footer at the bottom of each section.
It sounds like you want to use table section headers. However, your code snippet is setting the frame of the table footer, not the table section footers.
self.tableView.tableFooterView.frame = CGRectMake(0, 0, 320, 88);
If you want to adjust the table section footer height, the tableView:heightForFooterInSection: method will need to return a different value.

Shrink and expand the uiview according to the scrollview content offset

hi in my app I want to implement animation like instagram navigationBar animation.. I done all the thing.. I shrink and expand the headerView according to the UITableview contentOffset.. it shrink well with its subview well but while expand the headerView the subview height and the y position changed..
headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0") ? 64 : 44))];
UIView *view1 = [[UIView alloc]initWithFrame:headerView.bounds];
view1.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
view1.backgroundColor = [UIColor blackColor];
view1.alpha = 0.1;
[headerView addSubview:view1];
headerView.autoresizesSubviews = YES;
//UIImageView..
headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake((320 - 190) / 2 , (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0") ? 28 : 8), 190, 28)];
headerImageView.image = [UIImage imageNamed:#"logo.png"];
headerImageView.contentMode = UIViewContentModeScaleAspectFit;
headerImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[headerView addSubview:headerImageView];
//Back button
backButton = [UIButton buttonWithType: UIButtonTypeCustom];
backButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[backButton setImage: [UIImage imageNamed: #"listLeftMenu.png"] forState: UIControlStateNormal];
[backButton setFrame: CGRectMake(10, (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0") ? 34 : 14), 18, 16)];
[backButton addTarget:self action:#selector(leftMenuAction) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:backButton];
[self.view addSubview:headerView];
Above code is my headerView initialization...
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat height = (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0") ? 64 : 44) - scrollView.contentOffset.y;
if (height < 0) {
height = 0;
}
[headerView positionAtX:0 andY:0 withHeight:height]; // This is I change the x, y and height of the headerview..
[postTable positionAtY:height];
}
I know miss anything to keep the subview frame while expand... please help me to fix this issue...

How to dismiss a subView generated by code?

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

Resources