IBOutletCollection NSMutable Array order in IB - objective-c

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

Related

Property for all buttons in view

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];
}

Change Label With Tag Only

I am creating an application with multiple buttons and corresponding labels.
The layout is in a nib (.xib) file which is connected to the class I have the code below in. I'm trying to avoid using an IBOutlet for every label, since this would create a lot of extra code.
I know there is a way to reference a label using the tag:
UILabel *countLabel = (UILabel *)[self.view viewWithTag:numOfTag];
I called this within my buttonPressed method. However, when I try to change the text
[countLabel setText:#"newLabel"];
the app crashes. Am I accessing it wrong? I'm new to XCode..Help!
Thanks :)
This is not the correct way of doing it. Firstly whenever you creating UILabel in your XIB file, you can name them there only so technically, there is no need for you to set the label values in the connected view controller. Secondly, even if you want to reset the labels at the later stage, connecting them using IBOutlets is the best bet. Scanning through all sub views and then finding a particular subview with its tag is more tedious and should be avoided in this case.
EDIT:
For your case where you need to create more than 50 buttons and labels, I would suggest you to create a custom UIView with UIButton and UILabel in it. And then in the view where you want to display them, create objects in a for loop and display them. And when a UIButton is tapped it will be easy for you to fetch the associated UILabel though the custom UIView object.
Your label must not be found with viewWithTag:, so it will be nil. And when you try to access property of nil control, it might give you a crash.
Check if your label is not nil and check if it is label using
if(countLabel != nil && [countLabel isKindOfClass:[UILabel class]])
[countLabel setText:#"newLabel"];
else
NSLog(#"countLabel is not found");
viewWithTag: will search the view in the receiver’s hierarchy whose tag property matches the value in the tag parameter. This will return first control it will encounter with tag in hierarchy. So make sure your tag needs to so unique in entire view. Just make sure you are giving immediate parent of Label instead of self.view.
Edit:
To can try this way also if you are not able to track issue.
for (UIView *child in self.view.subviews) {
if([child isKindOfClass:[UILabel class]] && [child tag] == numOfTag)
{
[child setText:#"newLabel"];
break;
}
}

Pass data from Viewcontroller to a Label [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Passing Data between View Controllers
iOS dev noob here..
I have a bunch of buttons on my main ViewController, what I want to happen is when the user clicks on one of those buttons, I want it to take them to a separate view that has a label and I want the text in that label to change according to which button they press.
So basically I want to be able to pass data from a button press on the main ViewController to a label in a second view.
That might be a bit confusing and I apologize, but any help would be greatly appreciated.
I would suggest you use a storyboard if possible. You then can ctrl drag from each button to a new viewcontroller that you drag out onto the storyboard. This will setup a segue and you can use the method prepareforseguie to pass the data you need to use in the new view controller.
Assuming your view controller with the buttons is in a navigation controller - presumably at the root...
What you would do is, add a target to each of your buttons, in code this can be done with the method addTarget:action:forControlEvents: of your UIButton
For example:
[myBtn addTarget:self action:#selector(tappedButton:) forControlEvents:UIControlEventTouchUpInside];
The method tappedButton will be messaged with the button that was tapped:
- (void)tappedButton:(UIButton*)sender{
// exploit the button
}
Inside this method you can get the title of the button - myBtn.titleLabel.text
You can then create a new view controller (let's keep things simple and say you have your own UIViewController subclass called MySimpleViewController.
In this class you have a cameFrom property which you can set the button's title on, and in viewDidLoad of MySimpleViewController, you would get the property value of cameFrom, this could be the method implementation.
- (void)tappedButton:(UIButton*)sender{
MySimpleViewController *detail = [[MySimpleViewController alloc] init];
detail.cameFrom = sender.titleLabel.text;
[self.navigationController pushViewController:detail
animated:YES];
[detail release];
}
So over in your MySimpleViewController's viewDidLoad, you create a UILabel, give the text property the value of self.cameFrom and add it to the view with addSubview:

Pulling out UI Slider Name

Suppose I have a UISlider interface element that I connected to a property in my view controller called alphaSlider. Is it possible to pull out the name of that view controller property at runtime?
The approach I tried was this:
NSString *objectName = [slider description];
Here is my approach in more detail
for (UISlider *slider in sliders)
{
NSString *objectName = [slider description];
// Do some stuff with the NSString
}
But I discovered that the description is not what I thought it was (it's just a listing of the slider properties).
Not sure if I understand what you are asking, but it might be slider.tag
So if I understand you correctly, you have a bunch of UISlider interface elements that are wired to a bunch of UISlider IBOutlets in your code, and you want to figure out which one is the one you have at the moment (in your loop).
Have you tried testing for equality with each of your IBOutlets to find the one that matches? You can do this using == because the pointers will be the same.
Otherwise, I don't believe it is possible since the mapping really only exists in the xib file and is accessed at runtime when the view is being built and the IBOutlets are being populated.

XCode/Objective-C, load buttons (and other UI elements) at runtime

Is it possibile to load buttons at runtime without using .xib files and connect the buttons to class properties anyway (without drag&drop)?
With the "load without .xib" statement do you mean "instantiate in the code", right? in such case the answer is "yes, of course":
// interface
#property (retain) UIButton *myButtonProperty;
// implementation
UIButton *tmpButton = [UIButton buttonWithType:...];
...
self.myButtonProperty = tmpButton;
Yes, it is. You can create button object and add it to a view. More details can be found here.