i have a uiTextField that i am validating input on, when i have invalid input what are some appropriate ways to show that the input is invalid? is there any built in mechanism for showing invalid input states?
It's pretty easy to add an 'warning' image to the left-hand side of a UITextField to indicate that the field needs a value.
Try this:
UITextField* field = .... your text field ...
if ( fails_validation ) {
field.leftViewMode = UITextFieldViewModeAlways;
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
imageView.image = [UIImage imageNamed:#"warning.png"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
field.leftView = imageView;
} else {
field.leftViewMode = UITextFieldViewModeNever;
field.leftView = nil;
}
The best two options I've found so far are TextFieldValidator and US2FormValidator. With the caveat that I have only used the former, here's my take on each.
TextFieldValidator
↑ Simple (just one class!)
↑ Offers default UI with additional customization possible
↑ Handles multiple validations on a single field
↓ Doesn't handle text views
↓ Slightly awkward name…ValidatedTextField, for example, would be more accurate :)
US2FormValidator
↑ Handles text fields and text views
↑ Importable as a framework, including CocoaPods support
↑ Handles multiple validations on a single field
↓ No default UI
If you just need something implemented quickly, TextFieldValidator may be all you need. If you must have validated UITextViews, US2FormValidator is the way to go.
Have a look at Managing Overlay Views section in UITextField docs
Put a check mark to the right of the UITextField when correct, Else put an X to the right of it. to make it look smooth: fade it using an animation
Related
So i have a bunch of dynamically loaded labels..
Each of them has the same name because there is no telling how many there will be..
I have another method (not the one that created the labels) changing the text for one of the labels, but when i run it only the last label that was created will change..
I need it to change the one that has a certain tag or something..
Help is much appreciated, this website is yet to let me down.
self.myLabel cannot be connected to multiple labels, so it will contain the reference of last created label, you will have to create new label every time, and you can't track them by class properties, you have to access label by their tag.
you can set tag for each label, below is sample code,
for(int i=0; i< numberOfLabels; i++)
{
UILabel *label = [[UILabel alloc] init];
label.tag = i; // do not use tag 0 here.. u can use i+1, or i+100.. something like this.
[self.view addSubview:label];
}
to access labels,
UILabel *label = (UILabel*)[self.view viewWithTag: labelTag];
Okay since you dont have any code to show i guess i have to speculate.
What i understood is that you are creating Dynamic UILabels in ur code and you want to access them. Since you have same name for all the UILabels you might me loosing the previous UILabel when every time you create a new UILabel. So in order to keep track of how many UILabel you created you must add them in an Array. Declare an NSMutableArray in your viewController.h file and make sure in the viewDidLoad u allocate it like
arrForLabels = [[NSMutableArray alloc]init];
Since it is an NSMutableArray you can add object to it.
So when u create a UILabel make sure you add the same UILabel in the Array as well
for Instance
[arrForLabels addObject:yourLabel];
you can try to NSLog your Array to see its content.
Now all youu got to do is to Create a weak link like that
UILabel *tempLabel = [arrForLabels objectAtIndex:1];
now tempLabel will be the UILabel to change text
tempLabel.text = #"My New Text";
It will work fine.
Feel free to ask for any issues in it.
My question is kind of general; in my project I have 10 UIButton objects
named Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9, and Button10
I also have UIImageview That are named exactly like the buttons from 1 to 10.
I want to write a code that will manipulate the image by the last character of the button (always a number from 1 to 10) and will affect the UIImageview the same way
Something like this
buttonlastcharacter = i;
if(sender.lastcharacternumber is:i){
Button%,i.frame = //Some manipulation
But basically all that I want is to have access to a certain object by string
How can I implement such a behavior?
There are a couple of better ways to do this. If these buttons are all static and in IB you can use an IBCollection array for image views and buttons to simply call them up by matching indexes.
Better yet just use the tag value for the buttons or image views.
It is maybe not the ideal solution in your case, but you can do it different ways:
using kvo
UIButton* myButton = [self valueForKey:[NSString stringWithFormat:#"button%i",i]];
or with selectors and properties
UIButton* myButton = [self performSelector:NSSelectorFromString([NSString stringWithFormat:#"button%i",i])];
Hm, you could use an array for your buttons and a tag for your UIImageView objects. They all inherit from UIView, which provides you with a .tag propterty. It is of type NSInteger* .
For convenience reasons I would suggest to name the buttons from 0 to 9. It does not really matter but the first index in the array would be 0 and therefore naming them accordingly just makes things easier.
Define
NSArray *buttonArray;
You may opt for NSMutableArray depending what else you may want to do with it.
In viewDidLoad code:
buttonArray = [NSArray arrayWithCapacity:10];
buttonArray[0] = button0;
..
buttonArray[9] = button9;
In your XIB file in Interface Builder, or whereever you may create the UIImages programmatically, add the tags accordingly.
image0.tag = 0;
...
image0.tag = 9;
assuming you name them image0 to image9.
In your appropriate action method code:
buttonArray[sender.tag] = someManipulation;
You can do it like this,
In your IBAction method:
- (IBAction)click:(id)sender
{
UIButton *but = (UIButton *)sender;
but.frame = your manipulation code;
}
or you can check the title like:
if([but.currentTitle isEqualToString:#"Button1])
{
//Manipulate button 1
}
if you have added tags for the buttons from 1-10 you can use,
if(but.tag == 2)
{
//Manipulate button 2
}
I am trying to implement a suggestion offered by already answered question, but I am stuck on getting it to work, so looking for some help.
Here is the post I am trying to implement: http://stackoverflow.com/a/3067579/589310
I am trying to use the solution offered by "ridale" on how to make a "detailLabel" editable as part of a TableView. I hope it will allow me to directly edit a cell and enter a number. It doesn't seem too common as a UI, but "SmartRecord" does it and I want to emulate it.
Here is the only line that gives me an error:
UITextField *tmpView = [self detailLabel:indexPath];
I get this error:
Instance method -detailLabel: not found (return type defaults to 'id')
I assume it is because my self is different than the original poster.
I added a TableView directly to my existing controller. It is not a TableViewController directly:
#interface EditViewController : UIViewController
{
IBOutlet UITableView *tableSettings;
}
I can fill the table and interact with it, so I know it works (at some level anyway).
I have tried changing self to my table control or the cell directly:
UITextField *tmpView = [tableSettings detailLabel:indexPath];
I can't find anything that responds to the "detailLabel" method.
I am also not sure if the proposed solution is complete or uses more code not shown.
This is the only error I get, so I am hopeful once I solve it, it will work ;-)
-(UITextField*)detailLabel:(NSIndexPath*)indexPath is not present in UITableView or any other classes provided by Apple.
The frame position is just for sample. Modify that such that it would come in place of your detailLabel.
You have to write your own method which returns a UITextField, something as following
-(UITextField*)detailLabel:(NSIndexPath*)indexPath
{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
textField.delegate = self;
//assuming myList is array of NSString
NSString* str = [myList objectAtIndex:indexPath.row];
textField.text = str;
return [textField autorelease];
}
Im creating an app for an internal process where our trainers will go out to clients and fill in a survey of the customer after the fact.
I want to create a flexible space with in the view. Example if they click a checkbox that the Customer is not happy than text fields will be shown to enter more information which will need to dynamically push all fields under it down to accomodate the new fields. If the user is happy no fields need to appear and no dynamic shifting is necessary.
Just wondering if anyone has done this before and if there is a clean way to execute it.
I don't think there is a control that will do this.
But it should not be that hard. This code is not tested but something like this
//views needs to be ordered in the subview list by y position
//can be in in uibuilder or on awakefrom nib..
-(void) addTextFieldAfterView:(UIView*) selectedView
{
CGRect f = CGRectMake(selectedView.origin.x,
selectedView.origin.y+5+selectedView.size.height,
selectedView.size.width,
150);
UITextField *tf = [[UITextField alloc] initWithFrame:f];
[self insertSubview:tf belowSubview:selectedView];
int start = [self.subviews indexOfObject:tf];
//move everything down
for(int i =start; i < [self.subviews count];i++)
{
UIView* v = [self.subviews objectAtIndex:i];
v.frame = CGRectMake(v.origin.x,
v.origin.y+150,
v.size.width,
v.size.height);
}
}
I like using UITableView for forms. This is what a lot of the forms in the Settings app uses. It's fairly simple to turn sections on and off -- just respond to the messages with the right number of sections and then fill them in as requested.
I have a UISegmentedControl and I have some photos, my initial aim is when ever I click on a particular segment one particular image should appear on the view.
For example: If i have four segments and four images then upon each segment I click I must see a image.
Here I have taken an NSArray and Uploaded all these images using this particular below code:
NSArray * images = [NSArray arrayWithObjects:[UIImage imageNamed:#"f.ppg"], [UIImage imageNamed:#"t.ppg"....etc ], nil];
and after this I want to attach each particular image to a segment index. So here is the code which I wrote.
-(IBAction) segmentActionChanged:(id)sender
{
int segment = mSegment.selectedSegmentIndex;
mImageView.image=[images objectAtIndex:segment];
}
While compiling the application I am not getting any Images displayed and it is quiting abruptly.
Any help regarding this.
To Answer questions 1 and 3 (I think).
to display an image, you will need a UIImageView somewhere. a simple way to do this would be to create an method such as this.
//somewhere in viewDidLoad perhaps?
//title array is an array of strings to use as the label on each section of your control
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];
//this sets the segmentAction: method as the selector for your particular UISegmentedControl
//(so when its value is changed, segmentAction: gets called)
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//the segmentAction: method
- (IBAction) segmentAction:(id)sender
{
//This assumes you have declared the imageView elsewhere, as well as the array and that you
//are using Interface Builder to create/hookup the segmentControl. Basically
myImageView.image = [myImageArray objectAtIndex:sender.selectedSegmentIndex];
}
A few Notes: this does not take advantage of lazy loading, since you are creating and holding all of your images in an array prior to showing them. I would suggest creating an instance variable for the UISegmentedControl so you can access it anywhere also.
Disclaimer: this code is not complete and assumes some initializations.
To answer question number two:A UIImagePickerController is used when dealing with the phone's/devices camera or saved photos album. It is covered in the documentation.
Looks like you're missing a retain.