I have an application for the iPad and I have a lot of buttons. After clicking on a place, I need to set all buttons property that will be the same for all, I wonder how to do it? For example, all buttons in the view will have a black layer. Thank you for your help
If you're using Storyboard, assign all your buttons to a Reference Outlet Collection.
this will give you an NSArray with all the buttons you've assigned to it, then you can use
for..in loop to affect change to each button.
hope this helps.
Without knowing the setup of your app, the most generic answer would be:
Add all buttons you will want to change to an array.
When you click on "the place" as described in your question, iterate through that array.
Make the change to each of the buttons.
I can't give you any specific code, but it could look something like:
NSMutableArray *arrayOfButtons = [NSMutableArray array];
//add all your buttons to the array
//user clicks "the place" run code:
for (UIButton *oneOfTheButtons in arrayOfButtons) {
oneOfTheButtons.backgroundColor = [UIColor blackColor];
}
Related
here is the low down:
-(IBAction)button1click:(id)sender;
{
label1.hidden=YES;
textfield1.hidden=YES;
label2.hidden=NO;
textfield2.hidden=NO;
-(IBAction)button2click:(id)sender;
{
label1.hidden=NO;
textfield1.hidden=NO;
label2.hidden=YES;
textfield2.hidden=YES;
the is issue is that when i first open my screen all 4 labels are visible. By default button1 radio is checked but label2 and textfield 2 are visible when they shouldnt be. if i press button1 even though it is already selected the items with hide and then all is good. My issue is having them hidden when the screen first opens up.
Thanx all for you help
You can, in your viewDidLoad method:
-(void) viewDidLoad
{
[super viewDidLoad];
[self button1click:nil]; //nil or the instance of button1 if you need it
}
In this way, you will execute the same code when you press button1 without duplicate your code.
You can take one of two approaches to hide the label.
a) in Interface builder you can click the check box for hidden in the attributes inspector. If you do that the default behavior will always be hidden when the app launches then you can make it visible in code like your example shows
b) add your existing code to hide the label to your view controllers - (void)viewDidLoad method.
both methods work equally well.
When you create that objects you can set foo.isHidden = YES
Suppose I have a Storyboard containing a view that contains a button. When the user presses this button, a popover comes up.
Thus, I need to set an anchor by dragging the segue to the button using Xcode (and then do performSegueWithIdentifier:).
So, my question is: is there a way to set this "anchor" programmatically?
Thank you.
In my case I've added programmatically several UIBarButtonItem.
The problem of only using an invisible view as an archor is that, if like in my case, the size of the UIBarButtonItem is changing it's size, the arrow of the popover doesnt appear centered, and althought it works, looks a bit strange.
How to solve it.
Create a small view in storyboard ( the size doesnt really matter ), make it invisible, and link it.
In my case this is called invisibleViewAsArchor
Connect the UIBarbutton item with the follow action.
-(IBAction) showMyPopover:(id)sender {
if([self.popoverController isPopoverVisible])
{
[self.popoverController dismissPopoverAnimated:YES];
}else{
self.invisibleViewAsArchor.frame = CGRectMake([sender view].frame.origin.x,
[sender view].frame.origin.y-50,
[sender view].frame.size.width,
[sender view].frame.size.height);
[self performSegueWithIdentifier:#"segue_to_something" sender:self];
}
}
as you can see before it shows the popover (with performSegueWithIdentifier), I'm changing the frame
of the Archor with the values from the button that has fired the event.
Hope it helps.
In the storyboard anchor the popover to some arbitrary button. Don't worry too much about which one as it will get overridden in the code.
In the view controller method prepareForSegue, add the code:
let dest = segue.destinationViewController
dest.popoverPresentationController?.barButtonItem = <your bar button here>
or if you want to anchor to a view instead
dest.popoverPresentationController?.barButtonItem = nil
dest.popoverPresentationController?.sourceView = <your view here>
You can't programmatically create segue's as explained here: Creating a segue programmatically, however, you can configure which destination controller you want to display at run-time. This is explained in the apple documentation here: Configuring the Destination Controller When a Segue is Triggered.
Hopefully this helps!
I had the same problem where I was creating a BarButtonItem programmatically. You may also be able to get around it by creating an invisible, disabled button which you can set as the anchor in IB.
I have linked a set of buttons to and IBOutletCollection NSMutableArray;
I want to be able to make each button play a specific sound;
However after linking them I realised they were not added in any particular order.
I think the interface builder doesn't take into account the order of the added buttons.
Is there any way I could possibly access the interface button id through code and cast it as an integer so I could programatically assign the right sound to the right button?
Don't think is necessary but here is my code:
for(int i=0;i<48;i++){
UIButton *Pad=[UIButton alloc];
[Pads addobject:Pad];
[Pad release];
[[Pads objectAtindex:i] addTarget:self action:#selector(play:) forControlEvents:UIControlEventTouchUpInside];
((UIButton*)[Pads objectAtIndex:i]).tag=i;
}
To be more specific. In the interface builder in the atributes inspector each button has a label and an id. Would it be possible to reorder my array programatically using that id(or label for that matter)?
FIXE by sorting by TITLE [button currentTitle]
Simply set the tag property of each IBOutlet to i when adding it to Pads.
IBOutletCollection do not support ordering. Here's a similar question with an answer which orders collection contents by their position:
IBOutletCollection set ordering in Interface Builder
I have a set of UIButtons (defined in a xib) who have labels that need to be updated periodically. In the ViewDidLoad method of the view controller of those buttons' superview, I have an update method that does, for each button:
button.titleLabel.text = #"Relevant Text";
[button setNeedsDisplay];
and when you tap a button, another method runs which pops up a UIAlertView, which in turn calls back a method on the view controller which does much the same thing as the initial text setting method:
button.titleLabel.text = #"New Text";
[button setNeedsDisplay];
however, this code simply isn't working, the button label's text doesn't get updated in either method, it remains a blank white button. In the xib I don't define any text on the buttons - there's no point, the button text doesn't make sense unless it's set at runtime. Anyway, on a lark, I decided to set the text of one of the buttons to "test test test".
Now, when I tap that particular button, it pops up the UIAlertView but in the background changes the text of the button to "test test test test". And this time, the UIAlertView callback does what I expect it to and sets the text for only that button. When I hit it again, the text goes back to "test test test test" until I dismiss the UIAlertView, which again will run the callback method and set the button text to whatever the method should.
I have no idea what's going on here, or why setting the text initially in the xib has any relation to whether or not I can set that text later programatically. Obviously this isn't the behavior I want, I want to know how to for sure set the text on the buttons.
Edit: SVD's advice about setTitle:ForState: solved my problem, thanks. I'm still curious though as to why the title label set in the .xib shows up, but only when I have a UIAlertView pop up.
You may need to use [setTitle: forState:] to set the button title for normal and highlighted (or selected) state.
(And do make sure the button is connected to the outlet, as jtbandes points out).
There sure are a lot of UIButton questions here, and I was hoping to find the answer to this, but nothing quite like this particular issue.
I have a few buttons, and I can call button.highlighted = YES; for any button when the program runs and it shows up highlighted.
I thought I could then use this same technique to set a button's highlight state to YES after it pressed, and then set it to NO after another button is pressed. This way, the current selection remains highlighted.
For example:
-(IBAction) buttonPressed:(UIButton *)button
{
if (button.tag==1)
{
self.button1.highlighted=YES;
self.button2.highlighted=NO;
// do other program stuff here
}
if (button.tag==2)
{
self.button2.highlighted=YES;
self.button1.highlighted=NO;
// do other program stuff here
}
}
Even though the highlights work fine if I place the highlighted = YES; code inside viewDidLoad. But the above code does not work. The highlight doesn't stick. The buttons works, and does the other stuff it needs to do, but the highlights fail to stick.
I would think this should be pretty basic. Is iOS somehow automatically setting all button highlights to NO on its own after any button operation?
May be you should use other means to present the highlight status, because of the statement from UIControl's documentation:
By default, a control is not
highlighted. UIControl automatically
sets and clears this state
automatically when a touch enters and
exits during tracking and when there
is a touch up.
You might try using the 'selected' property instead of the 'highlighted' property.