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)
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 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.
I have a scroll view in which i have a label. i have to update the label's text each time the user scrolls the scrollview. i want to update the label's value with pageindex.I am getting the page index in scrollviewdelegate method but unable to update it in label as the label gets loaded in viewDidload. Any way to refresh or update the label's text from scrollview delegate method?
HERE pageNumber is number which update and get when you scroll UIScrollView
Write this code in where your get pageNumber
Yourlbl.text = pageNumber;
Make the label a property and use the property to set the labels text.
Property (assuming arc enabled project):
#property (nonatomic, strong) UILabel *myLabel;
ViewDidLoad:
- (void)viewDidLoad {
self.myLabel = [UILabel new];
}
and update your label texts from inside the viewController like this:
self.myLabel.text = #"test";
take all the values in one array and assign the label text based on the pageindex like below
label.text=[titlesArray objectAtIndex: pageindex];
for achieving this one you can take label globally.
EDIT:-
for achieving this one you can do two ways.
1.take UILabel in Xib and connect the label with some name and you can set the labelname.text in scrollviewDidEnd method .
2.take uilabel programatically and define that one in .h file and change the textscrollviewDidEnd particular method.