I have a bunch of views, each with 2 labels in them that are exactly the same. the labels all are tagged 1 and 2 in each view.
Now i wasnt using an IBOutletCollection before and had a reference to each of these views (which was unnecessary) and was getting a reference to the labels in the views like so:
l1 = (UILabel*)[_labels4 viewWithTag:1]; //_labels4 is a UIView with 2 labels in it
l2 = (UILabel*)[_labels4 viewWithTag:2];
which worked fine, but the code was becoming bloated, so i wanted to start using an IBOutletCollection so i can cycle through all the views with a for loop
so when i did that it seemed to work fine, except the labels now were becoming UIViews instead of UILabels
for(UIView *view in self.labelViews){ //self.labelViews is the collection of UIViews aka _labels4 + others
UILabel *l1 = (UILabel*)[view viewWithTag:1];
UILabel *l2 = (UILabel*)[view viewWithTag:2];
l2.text = #"Reference"; //crash because unrecognised selector 'setText' sent to UIView
}
does anyone by any chance know why? you can check out some more detailed code over here
to help clear up misconceptions:
In your self.labelViews there is a UIView hiding in there that is not a UILabel
In the code you posted can you change the following:
#property (strong, nonatomic) IBOutletCollection(UIView) NSArray *labelViews;
Should read
#property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *labelViews;
Ok found the problem, and sorry, it wasnt anything anyone here could have spotted. Somewhere in this project having the views set as 0,1,2,3 etc causes them to get modified by something (not sure what yet, im doing maintenance on a project that is not my own) so setting the tags to something random like 900,901... etc seemed to fix it.
Related
I am creating a tableView with custom cells, with each cell being created with the following code:
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:kEditSymbolCellId];
I have returntableView.isEditing; set.
and i have the minus button visible from the get go. With the editing style set toUITableViewCellEditingStyleDelete
somewhere
(if (editingStyle == UITableViewCellEditingStyleDelete) { passes).
Where would i have to change the code to add the accessibility label.
I am creating the cell of a custom class- the .h has only this in it:
#interface WidgetEditCell : UITableViewCell
#property (retain, nonatomic) IBOutlet UILabel *symbolLabel;
#property (retain, nonatomic) IBOutlet UILabel *subtitleLabel;
With ainitWithStyle and asetSelected in the .m
nothing that changes the cursed minus image.
someone please help.
By default, there should be an accessibilityLabel built in that reads your label and places the message "delete" in front. I have tested a custom cell, see below:
If that does not suit your needs, I have these suggestions:
Add a UIAlertView to display a message when a person wishes to delete. This can have a voice message enabled, and, realistically is a pleasant way of going about business providing things aren't being deleted (my opinion).
Create your own custom delete function following this tutorial from Ray Wenderlich. I have used it and find it really practical for customisability.
I have not tried this, but create your accessibilityLabel whenever the edit option is used by creating a custom button, or UILabel that is set to Transparent.
UIButton *someButton = [[UIButton alloc] initWithFrame:CGRectMake(x, y, h, w)];
someButton.backgroundColor = [UIColor clearColor];
someButton.accessibilityLabel = #"SomeNSString";
Have it fill the area around the button perhaps and then have it perform the delete function if that's called so it appears seem less. It's probably not the greatest option on the planet but I have yet to see another way.
Image from Ray Wenderlich
Please give me an advice.
I create UILabels programmatically (dynamic).
Is there is a chance to add Event to them?
What I want by steps:
I create UILabel;
I set Event to it; (NSNotification?)
When I do some action (rotate, for example) I want that Label is changed or removed. An extended example: I create Labels and when I rotate device I want that part of them (which with attached Events) disappear in animation.
I create a lot of Labels, so I can't just set them global variables. And I can't set them tags unlimited. So UILabel *label = (UILabel*)[self.view viewWithTag:labelCount not a solution. Getting element by 'viewWithTag' has one more trouble - when set animation to that element and that element already in animation happens collision - they plays one over other...
I create Labels like this:
CGRect *labelFrame = CGRectMake(left, top, width, height);
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
label.text = #"Hi, I'm one of these army of labels";
[self.view addSubview:label];
PS: Sorry for English.
I assume you have a UIViewController that has a bunch of labels. I would recommend an IBOutletConnection for storing a reference to all of your labels (assuming storyboard).
//You will need to connect all of these labels through Interface Builder.
#property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *labels;
In one of the following rotation methods (Detect rotation changes in iOS) do your rearranging.
//Called whenever orientation changes
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
for (UILabel *label in self.labels) {
//Make each label disappear here.
}
}
Came up this question and did some quick experiments without no luck.
Basically, I made a simple single view project where the top view controller is a UITableViewController. For simplicity, I set the table view content to be "Static Cells". The table cell was a custom subclass of UITableViewCell, like this
#interface TopTableViewCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *label;
#property (strong, nonatomic) IBOutlet TableCellBottomView *bottomView;
#end
Both the properties were wired through control dragging. The TableCellBottomView is just a custom subclass of UIView like this
#interface TableCellBottomView : UIView
#end
Now I add a label inside this TableCellBottomView like the following picture showing
Can I wire this bottom label inside to my TableCellBottomView? Control dragging did not work for me here. I certainly could have added it programmatically inside TableCellBottomView.m. But if i could wire it here, it would be quite convenient, since I could also add a lot of other components and arrange them visually. Thanks!
You may set a tag for the label in Xcode and fetch the UILabel based on the tag wherever you need it:
If you use dynamic cells, you can do this in tableView:cellForRowAtIndexPath:. Alternatively wire the cell to a property and then use that property to fetch it:
((UILabel*)[cell viewWithTag:1]).text = #"Some text";
I don't know why Xcode won't allow you to drag from your label to the bottom view .h file, but you can do it another way. Add the IBOutlet property to the .h file, then drag from the littler open circle to the left of the #property to your label in the storyboard, and that should work.
I have created a custom UITableViewCell in Interface Builder and also created a class for it, and loaded it into my ViewController by registering its NIB.
Inside the custom cell I have an UIImageView, a UILabel and two IBOutlets for each of them in my customViewCell.h:
#property (strong, nonatomic) IBOutlet UIImageView *assignmentImageView;
#property (strong, nonatomic) IBOutlet UILabel *testLabel;
Now in the the cellForRowAtIndexPath method, I can write to the label outlet with:
cell.testLabel = #"text"
But I am unable to set the image property of the cell.assignmentImageView to some image I want to load. So the following doesn't do anything:
cell.assignmentImageView.image = [UIImage imageNamed:#"my.png"];
I also noticed that the completion feature of xcode doesn't show assignmentImageView as a property of cell, but it does show testLabel. So that leads me to believe that even though assignmentImageView has been set as an IBOutlet, it is for some reason not considered as such.
I tried also using the tag property of the UIImageView and retrieve a pointer to the UIImage in the cell using the viewWithTag method, but that didn't work either.
I know that if I have only a single UIImage in the cell, I can reference it with cell.imageView, which is strange to me, but I want to be able to have multiple UIImageView in the cell and reference them.
Can anyone explain me how to do this?
Thanks
-Malena
Check of your assignmentImageView outlet is wired with a correct view in XIB.
Check if the image you are trying to set is added to your project.
Set breakpoints or use NSAssert or NSParameterAssert statements to verify that UIImage and the assignmentImageView are not nil.
I need help on populating my uilabels. I have three uiviews and each have five labels. I need to change the labels data when the view changes, i am using uisegmentedbutton to change views..What would be the best approach? Thanks.
You do it in exactly the same way you got a reference to the views, e.g. viewPanel1. You need a property or instance variable for each one. Let's say it's a property. If the labels were created in the nib, you must make an outlet for that property. If the labels were created in code, you must assign each one to a property as you create it. Now you can use that property, which persists, to refer to the label it points to.
Let's say I have
#property (nonatomic, weak) UILabel* label1
Then later I can create the label and put it in the interface:
UILabel* lab = ...
self.label1 = lab;
[someView addSubview:lab];
Then even later I can refer to the label:
self.label1.text = #"Bart Simpson Was Here";
Let's do it with an array of labels. We have:
#property (nonatomic, strong) NSMutableArray* labels1;
Then later I must actually make the array:
self.labels1 = [NSMutableArray new];
Then still later I can create the label and put it in the interface, and add it to the array:
UILabel* lab = ...
[self.labels1 addObject:lab];
[someView addSubview:lab];
Then even later I can refer to the label:
self.labels1[0].text = #"Bart Simpson Was Here";
(because this is the first label added to labels1)