Remove Buttons Dynamically with an Array - objective-c

I have a view that has labels being dynamically added by the user. If the use would like to edit any of the labels, they press a button and all the labels get highlighted with a delete button and move button. (Editing is another bridge I’ll cross later).
My issue is: What is the best way to turn the buttons on and off? I have a method that turns the buttons on… but I am at a loss as to how I turn them off when done editing. Do I need to tag my buttons and then just ‘hide them’? Or do I just remove them all totally? How do I parse all the buttons that are turned on then, turn them off. Do I need to put them in an array as well? The labels are tagged with unique numbers so I know which label is which.
Any thoughts? Guidance? If I am doing it all wrong please tell me.
Here is a couple methods I have:
- (void) showEditableText {
// Parse the array of labels
if(textArray.count > 0){
for(UILabel *label in textArray){
//Add Delete Button
UIImage * delButtonImage = [UIImage imageNamed:#"GUI_Delete.png"];
UIButton * delThisButton = [[UIButton alloc] initWithFrame:CGRectMake(label.frame.origin.x - delButtonImage.size.width, label.frame.origin.y - delButtonImage.size.height, delButtonImage.size.width, delButtonImage.size.height)];
[delThisButton setBackgroundImage:delButtonImage forState:UIControlStateNormal];
[delThisButton addTarget:self action:#selector(deleteThisLabel:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:delThisButton];
//Add a move button
UIImage * moveButtonImage = [UIImage imageNamed:#"GUI_Move.png"];
UIButton * moveThisButton = [[UIButton alloc] initWithFrame:CGRectMake((label.frame.origin.x + label.frame.size.width + moveButtonImage.size.width), label.frame.origin.y - moveButtonImage.size.height, moveButtonImage.size.width, moveButtonImage.size.height)];
[moveThisButton setBackgroundImage:moveButtonImage forState:UIControlStateNormal];
[moveThisButton addTarget:self action:#selector(moveThisLabel:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:moveThisButton];
//Make the text highlighed
label.highlighted = YES;
label.backgroundColor = [UIColor colorWithRed:203/255.0f green:230/255.0f blue:239/255.0f alpha:1];
label.highlightedTextColor = [UIColor redColor];
}
}
}
- (void) doneEditingText {
if(textArray.count > 0){
for(UILabel *label in textArray){
//THIS IS WHERE I AM STUCK? WHAT DO I DO?
label.highlighted = NO;
label.backgroundColor = [UIColor clearColor];
}
}
}

//inside your first method set the same tag to your all buttons
-(void) showEditableText {
........
.......
.......
delThisButton.tag = 10;
moveThisButton.tag = 10;
}
//inside your second method delete all the subviews using this tag as shown below..
-(void) doneEditingText {
if(textArray.count > 0){
for(UILabel *label in textArray){
..............................
//THIS IS WHERE I AM STUCK? WHAT DO I DO?
for (UIView *subview in [self.view subviews]) {
if (subview.tag == 10) {
[subview removeFromSuperview];
}
}
...............................
label.highlighted = NO;
label.backgroundColor = [UIColor clearColor];
}
}
}

Move all your buttons code to viewDidLoad, hide them (button.hidden = YES). When you start/finish editing your text, unhide and hide your buttons. You need to have an ivar to contain the buttons too. So add them in your .h file.

Try This
UIView * seletetButton = nil;
for (UIView * btn in self.view.subviews){
if ([btn isKindOfClass:[UIButton class]])
{
if (seletetButton.tag != btn.tag)
{
[btn removeFromSuperview];
}
}
}

Related

Add texfield in runtime in IOS

I want to make an "add" button to add as many textfields as i want in my scroll view.
I did this already
UITextField *text=[[UITextField alloc]initWithFrame:CGRectMake(50,50,200,200)];
[self.scrollView addSubview:text];
But is not working.
Thanks!
If this is your complete button code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
// add this
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
// continue
[self.mainScroll addSubview:button];
Then add this somewhere:
- (void) buttonPressed:(UIButton *)button {
UITextField *text=[[UITextField alloc]initWithFrame:CGRectMake(50,50,200,200)];
text.backgroundColor = [UIColor greenColor]; // just to make sure it's showing up.
[self.scrollView addSubview:text];
}
First create common function like this,
-(void)setTextfieldStyle:(UITextField *)pTmpTextField
{
pTmpTextField.borderStyle = UITextBorderStyleRoundedRect;
pTmpTextField.font = [UIFont systemFontOfSize:15];
pTmpTextField.placeholder = #"First Name";
pTmpTextField.autocorrectionType = UITextAutocorrectionTypeNo;
pTmpTextField.keyboardType = UIKeyboardTypeDefault;
pTmpTextField.returnKeyType = UIReturnKeyDone;
pTmpTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
pTmpTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
pTmpTextField.delegate = self;
}
and use this for creating UITextFiled,
int y = 0;
for(int i=0;i < 5;i++)
{
UITextField *txtTemp=[[UITextField alloc]initWithFrame:CGRectMake(0, y, 300, 31)];
txtTemp.tag = i;
[self setTextfieldStyle:txtTemp];
[self.view addSubview:txtTemp];
[txtTemp release];
y+=36;
}
This is UITextField's delegate method,
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"%#",textField.text);
}
Hope this works for you as this works for me. only keep this in mind that add the text fields to your scroll view.
In the example am adding them to my view!

Passing different UIButtons through a general Method. Are Selectors/Senders the answer?

I have about 20 buttons added from Interface Builder, and when pressed they change colour like so:
-(IBAction)button1:(id)sender
{
if(playerTurn == YES)
{
button1.backgroundColor = [UIColor redColor];
}
}
But to shorten things it seems like I could just have a general method, so that every button when pressed runs the method. Something like:
-(IBAction)button1:(id)sender
{
//Go to method and make this button red
}
-(void)changeColour
{
if(playerTurn == YES)
{
buttonThatWasSent.backgroundColor = [UIColor redColor];
}
}
Unfortunately I can't figure out how to do that. It seems selectors/senders are the answer? But I've not managed to make any tutorials I've found work.
You were pretty close!
- (IBAction)myActionWithAnArbitraryName:(UIButton *)sender
{
if(playerTurn == YES) {
[sender setBackgroundColor:[UIColor redColor]];
}
}
You can actually use a single IBAction for all your buttons and just cast the sender as a UIButton:
- (IBAction)buttonPressed:(id)sender {
UIButton *button = (UIButton *)sender;
if (playerTurn == YES) {
button.backgroundColor = [UIColor redColor];
}
}
You can link all the 20 buttons of a single action.
The sender knows which button was pressed, so directly channge the backgroundColor of sender.
-(IBAction)changeColour:(id)sender
{
if (playerTurn == YES)
sender.backgroundColor = [UIColor redColor];
}
Note:This is not tested code. But i guess it should work, if wont they try do this way:
UIButton *button=(UIButton *)sender;
button.backgroundColor = [UIColor redColor];

iOS edit UIButton properties

I am working on the basics of iOS development.
So far I have a button that on press, will show some text. But what I want to do is after it is pressed, I want it to then change the text of the button, so far this is what I have:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.backgroundColor = [UIColor redColor];
button.titleLabel.textColor=[UIColor blackColor];
button.frame = CGRectMake(25, 100, 275, 60);
[button setTitle:#"Press this button to reveal the text!" forState:UIControlStateNormal];
[button addTarget:self action:#selector(button_method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)button_method:(UIButton *)sender {
NSString *test = #"I am learning Objective-C for the very first time! Also, this is my first ever variable!";
// handle button press
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 25, 275, 60)];
label.text = test;
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
//label.lineBreakMode = NSLineBreakByWordWrapping; //iOS 6 only
[self.view addSubview:label];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
When I try adding [button setTitle:#"You pressed the button"]; to button_method
This doesn't work... why? And how would I make it work?
When I try adding [button setTitle:#"You pressed the button"]; to button_method, it doesn't work... why?
Because that method is nonexistent on UIButton.
And how would I make it work?
By using a method that UIButton actually responds to. For example:
[sender setTitle:#"You pressed the button" forState:UIControlStateNormal];
And read the relevant documentation, please.
Your code will not change the title of the button. But simply add a label as a subview for your view. Is this what you want?
If you want to change the text on the button you will want to do sender.title.text = test; in your button_method
I also recommend adding a NSLog(#"Button pressed"); to your button press method to double check that it is being called.

Failing to add a label to parentView via code

I just want to be able to add a label to parent view dynamically i.e just by pressing a button.Not a real world application just learning.
Here is cut down version of the method it executes but can not see the label.
- (IBAction)addLabelToParentView
{
NSLog(#"button click");
UILabel* label ;
[label setText:#"test"];
[[self view] addSubview:label];
}
You need to initialize your label with a frame rectangle. Try the code below.
- (IBAction)addLabelToParentView
{
NSLog(#"button click");
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 20.0, 280.0, 30.0)];
[label setText:#"test"];
[[self view] addSubview:label];
}

Can we disabled the navigation controller of leftBarButtonItem that is back button of the view controller in iPhone?

I want to disabled the default back button of navigation controller
self.navigationItem.rightBarButtonItem.enabled = NO;
// Below code does not work since leftBarButtonItem is always nil.
self.navigationItem.leftBarButtonItem.enabled = NO;
I have done it with manually shown below, But Is there any property to disabled the default back button with just single line?
backButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 100, 30)];
[backButton setBackgroundImage:[UIImage imageNamed:#"backbutton_100.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:#" All Customers" forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[buttonView addSubview:backButton];
UIBarButtonItem* leftButton = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
self.navigationItem.leftBarButtonItem = leftButton;
[leftButton release];
// Now it is working.
self.navigationItem.leftBarButtonItem.enabled = NO;
Its very easy ..... just try this out
self.navigationController.navigationBar.userInteractionEnabled = NO; //for disabling
self.navigationController.navigationBar.userInteractionEnabled = YES; //for again enabling
Using "hidesBackButton=YES" is really not an elegant solution, cause it HIDES the button which is not what we want. An acceptable work-around would be adding a UILabel to the window just over the back button at least disabling the touches on the button.
Add this method to your AppDelegate class:
- (void) disableLeftBarButtonItemOnNavbar:(BOOL)disable
{
static UILabel *l = nil;
if (disable) {
if (l != nil)
return;
l = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 160, 44)];
l.backgroundColor = [UIColor clearColor];
l.userInteractionEnabled = YES;
[self.window addSubview:l];
}
else {
if (l == nil)
return;
[l removeFromSuperview];
[l release];
l = nil;
}
}
You can call it like this from any view controller to disable:
MyAppDelegate *appDeleg = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDeleg disableLeftBarButtonItemOnNavbar:YES];
To enable:
MyAppDelegate *appDeleg = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDeleg disableLeftBarButtonItemOnNavbar:NO];
Call [self.navigationItem setHidesBackButton:YES]; for the view controller you do not want to have the back button. Then set the leftBarButtonItem as normal.
You can also use
[[_navigationController.topViewController.navigationItem leftBarButtonItem] setEnabled:NO]; // The top view controller on the stack.
[[_navigationController.visibleViewController.navigationItem leftBarButtonItem] setEnabled:NO];// Return modal view controller if it exists. Otherwise the top view controller.
you can use this when you want to disable or enable UIViewControler from Appdelegate, or any other viewcontroler.