In my project i have created textfield using actionsheet button.how can i disable the userinterface after first creation.
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0)
{
text1 = [[UITextField alloc] initWithFrame:CGRectMake(x2,y2+3, 300, 25)];
text1.backgroundColor = [UIColor whiteColor];
text1.clearButtonMode = UITextFieldViewModeWhileEditing;
text1.font = [UIFont systemFontOfSize:15.0f];
text1.placeholder=#"ENTER HOME LOAN INTEREST";
[text1 setKeyboardType:UIKeyboardTypeNumberPad];
text1.textAlignment=NSTextAlignmentCenter;
text1.userInteractionEnabled=YES;
[scrollview addSubview:text1];
scrollview.contentSize=CGSizeMake(self.view.frame.size.width,self.view.frame.size.height+to);
to+=to;
y2+=30;
img6.frame=CGRectMake(0, y2+4, 320, 60);
y2=y2+62;
img7.frame=CGRectMake(0, y2+5, 320, 60);
y2=y2-62;
}
else if (buttonIndex==1)
{
text2 = [[UITextField alloc] initWithFrame:CGRectMake(x2, y2+3, 300, 25)];
text2.backgroundColor = [UIColor whiteColor];
text2.clearButtonMode = UITextFieldViewModeWhileEditing;
text2.font = [UIFont systemFontOfSize:15.0f];
text2.placeholder=#"ENTER EDUCATION EXPENSE";
[text2 setKeyboardType:UIKeyboardTypeNumberPad];
text2.userInteractionEnabled=YES;
text2.textAlignment=NSTextAlignmentCenter;
[scrollview addSubview:text2];
scrollview.contentSize=CGSizeMake(self.view.frame.size.width,self.view.frame.size.height+to);
to+=to;
y2+=30;
img6.frame=CGRectMake(0, y2+4, 320, 60);
y2=y2+60;
img7.frame=CGRectMake(0, y2+5, 320, 60);
y2=y2-60;
}
}
how can i prevent user from creating same textfield many time
It looks to me as if the two textfields are the same, with the exception of the placeholder, and presumably what you do with the value later. You could easily use a single textfield that is accessible as a property in your class. If you are using storyboards, it's possible that you can place the textfield in your storyboard and perhaps set it to 'hidden'.
Once you have a common text field, you can simply set the placeholder text and make the field visible inside the actionsheet delegate method. If there are other differences in the layout, then those too can be handled at this point.
This approach would need a way to hold 'state' so that you know the context of the field later on. You could manage this by examining the placeholder text, but this would be fragile because the text may change on a whim. A better approach would be to set a tag value e.g. text2.tag = 1 for home load interest and text2.tag = 2 for education expense. You can later access the tag value to interpret the context.
Because there is only one field, and because it is accessible by a property, you can later remove the textfield from the superview, or hide it again and use another flag to know that it has been displayed to the user. Most likely you can check that you have a value for either home loan interest or education expense, in which case, don't show it again.
You can hide/remove/disable the button that invokes the actionsheet from within the actionsheet delegate callback method too, again you will need a property in order to access it.
Related
I want to transfer a UITextField properties to another UITextField. How can I do so?
Here's my complete scenario:
I have created a custom cell in which I have a UITextField.
Now when I'm using this UITextField in my UITableView i want to assign already existing
UITextField's property to this cell's UITextField. I tried
cell.myCustomTextField = myAlreadyCreatedTextField;
Also tried to transfer individual properties like
cell.myCustomTextField.borderStyle = myAlreadyCreatedTextField.borderStyle;
but this also didn't work.
Please help
If you need multiple UITextFields with the same properties you can make an initializer like this.
myField = [self fieldWithLabel:#"Placeholder" withTextField:myField withFrame:CGRectMake(0,0,260,45) withKeyboard:UIKeyboardTypeNumberPad];
//
-(UITextField *)fieldWithLabel:(NSString *)name withTextField:(UITextField *)field
withFrame:(CGRect)frame withKeyboard:(UIKeyboardType)keyboard
{
CGRect fieldFrame = frame;
field = [[UITextField alloc] initWithFrame:fieldFrame];
field.placeholder = name;
field.autocorrectionType = UITextAutocorrectionTypeNo;
field.textAlignment=UITextAlignmentLeft;
field.userInteractionEnabled = YES;
field.enabled = YES;
field.enablesReturnKeyAutomatically= NO;
field.clearsOnBeginEditing = NO;
field.delegate = self;
field.returnKeyType = UIReturnKeyDone;
field.keyboardType = keyboard;
return field;
}
Controls (like NSTextField) each have an associated cell which
contains the display attributes of the control. NSCell and its
subclasses implement copying, so you can copy the cell instead of the
view (which does not support NSCopying).
Alternatively, NSViews support NSCoding, so generally speaking if you
have a view or subclass that you need to replicate, one way to do it
is to archive it and then unarchive it for however many "copies" you
might need.
From Source.
IN SHORT : It is not possible to assign a property of one control to another control.
Because Apple/iOS does not provide this type of feature.
But you can get text of one UITextField to another UITextField, by
textField1.text = textField2.text;
I am currently create a UITableViewCell with a UITextField in it.
On click of the text field, I want to bring up a number keyboard that I created. And as I type, the textfield should check the input for me; on click of other place, the keypad should be dismissed.
Code:
UITableViewCell *sizeCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"sizeCell"];
sizeCell.textLabel.text = #"Size";
UITextField* sizeField = [[UITextField alloc] initWithFrame:CGRectMake(185, 10, 100, 28)];
sizeField.text = #"0";
sizeField.textAlignment = UITextAlignmentRight;
sizeField.textColor = [UIColor colorWithRed:50.0/255.0 green:79.0/255.0 blue:133.0/255.0 alpha:1.0f];
sizeField.backgroundColor = [UIColor clearColor];
sizeField.keyboardType = UIKeyboardTypeDecimalPad;
[sizeCell.contentView addSubview:sizeField];
rows = [[NSArray arrayWithObjects:switchCell, typeCell, sizeCell, nil] retain];
I tried to implement UITextFieldDelegate like:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[sizeField resignFirstResponder];
return YES;
}
but the keyboard doesn't go away...
How do I validate the input and dismiss the keyboard?
You never set the delegate on your textfield so that textFieldShouldReturn: gets called. Make sure your class conforms to UITextFieldDelegate and then do the following:
...
UITextField* sizeField = [[UITextField alloc] initWithFrame:CGRectMake(185, 10, 100, 28)];
sizeField.delegate = self; //This is important!
sizeField.text = #"0";
...
A few observations:
As another poster suggested, make sure you set the keyboard's delegate correctly.
If you want to dismiss the keyboard on keyboard return, make sure you have one on your custom keyboard and it's correctly set up to call the ...ShouldReturn method.
If you want to dismiss on taps outside, you'll have to do that on your own.
You are declaring sizeField inside the method where you are setting it up, then calling it from another method outside that scope. I assume you have a class variable called sizeField or you'd be getting a compiler error. However, declaring it again when you're setting it up like you do shadows the class variable declaration so it never gets set up. Incidentally, that's a memory leak.
This shouldn't affect the actual running of the program if all else is correct (but it will if, e.g. 4 is the problem and not fixed), but I think it's better form to call [textField resign...] instead of [sizeField resign...]. At the least you should assert(textField == sizeField).
I got an app in which I allocate 5 UISegmentedControl dynamically into the view. And got a Done button at the end. My condition that to proceed into next step (when done button is pressed), all the UISegmentControls "should be selected by user".
The default selection in segmentcontrol is none.
How to check whether all the UISegmentedControls in my view is selected by the user before action on the done button is executed?
Right from the apple document, this should answer your question:
#property(nonatomic) NSInteger selectedSegmentIndex
Discussion
The default value is UISegmentedControlNoSegment (no segment selected) until the user touches a segment.
Hope you can use that to check whether the value is user selected or not, to prevent going to next page.
NSLog(#"%i", self.segment.selectedSegmentIndex);
this results -1 if no segment is selected.
add target and action to your segmentControls for UIControlEventValueChanged.
From the selector you gave in action, check which segmentControl was changed, and set it's corresponding flag (ex: array of string which are #"0" for not selected and #"1" once selected).
At any time check which flags are not set, the corresponding segmentControls were never selected.
This is how u create an UISegmentedControl
NSArray *itemArray = [NSArray arrayWithObjects: #"Title1", #"Title2", #"Title3", #"Title4",nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(0, 0, 310, 35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:#selector(pickOne:) forControlEvents:UIControlEventValueChanged];
segmentedControl.tintColor=[UIColor grayColor];
Then to find which segment was selected,
NSString *category =[segmentedControl titleForSegmentAtIndex: [segmentedControl selectedSegmentIndex]];
if(category==#"Title1"){
//Do something here..
}
Hope this helps.... Happy Coding
In my iphone app, I have created some UITextField in scrollView dynamically. And added one button in xib. On that button's TouchUpInside event, I opened UIImagePickercontroller to open photo library and taking selected image on UIImageView.
Now when modalview is dissmissed, values in my UITextField disappears.
How do I retain the UITextField values?
txt = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, 30.0f, 200.0f, 30.0f)];
[txt addTarget:self action:#selector(keyDown:)forControlEvents:UIControlEventEditingDidEndOnExit];
txt.textColor = [UIColor blackColor];
txt.borderStyle = UITextBorderStyleBezel;
txt.autocorrectionType = UITextAutocorrectionTypeNo;
[fieldArray addObject:txt];
Using for loop I am adding txt to NSMutableArray fieldArray.
Here is the code where I fetch values from TextFields
NSMutableArray *insertValues = [[NSMutableArray alloc]init];
//Taking values from textFields
for (int i=1; i<=nooffields; i++) {
UITextField *tf = (UITextField *)[self.view viewWithTag:i];
NSLog(#"TF : %#",tf.text);
if (tf.text.length<=0) {
tf.text=#"";
}
[insertValues addObject:[NSString stringWithFormat:#"'%#'",tf.text]];
}
EDIT :
And also when I display values from database in this textFields and try to edit it. It gives me null values.
What may be the reason? If there any other alternatives? please help me
UITextField *textField=[[UITextField alloc]init];
[textField setFrame:CGRectMake(x,y, width, hight)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:textField];
The information that your scrollview uses to get populated should be stored outside the scrollview, in a NSMutableDictionary for example. So everytime cellForRowAtIndexPath gets called it can retrieve the information from that source. So I advise you to store the UITextField's text within the dictionary. You can index it by its tag number. That way you won't loose whatever it contained.
I hope it helps you
Cheers
I finally got a work around for my problem.
I am temporarily storing my textField's values into database when presentModalViewController is called.
These values I retrieve back into the textFields on viewWillAppear.
This may not be a very efficient solution but it worked in my case.
Hope this helps someone.
Thanks for all your responses.
Good Evening all!
I have some UIButtons added dynamically into my view and of course I have an IBAction which handles button events.
The problem is: How can I detect which button is pressed if the only thing I know is the (id)sender and the array of buttons?
Buttons are never the same, every button has a different behavior. When I want to use static buttons and connect them through the IB I use something like this :
-(IBAction)doSomething:(id)sender
{
if(sender == button1)
dosomething;
if(sender == button2)
dosomething else;
if(sender == button3)
dosomething3;
}
In my case this does not work because there is no button1, button2, button3 but a MutableArray of buttons which have the same name as they were allocated with it. Button!
I tried using the way above but with no success and i tried also getting the tag of a button but I have nothing to compare it to!
I would really appreciate your help.
sincerely
L_Sonic
PS Dynamicaly means that i am creating the buttons in random time during run time like this
-(void)configActionSheetView
{
buttonView = [[UIView alloc]initWithFrame:CGRectMake(0.0,460, 60, 480)];
[buttonView setBackgroundColor:[UIColor blackColor]];
[buttonView setAlpha:0.6];
for (int i = 0 ;i<[buffButtons count];i++)
{
UIButton *customButton = [buffButtons objectAtIndex:i];
customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//UILabel *customLabel = [[UILabel alloc]init];
//[customButton setTag:(i)+11];
[customButton addTarget:self action:#selector(activateBuffEffect:) forControlEvents:UIControlEventTouchUpInside];
[customButton setAlpha:1.0];
customButton.frame = CGRectMake(8.0, 5+(50*i), 44.0, 44.0);
[customButton setTitle:nil forState:UIControlStateNormal];
buttonView.frame = CGRectMake(0, 460, 60, 50+(44*(i+1)));
[buttonView addSubview:customButton];
}
}
this is inside a functions and gets called during run time. the buffButtons is a mutableArray with buttons that gets populated during runtime.
i need a solution like this i cannot get a different eventhandling method for everybutton.
When you was "added dynamically" I assume you mean that they are created from some piece of code. Since all buttons to different things and you know what a certain button should do, why don't you add different actions to different buttons?
UIButton *myCreatedButton = [[UIButton alloc] init];
[myCreatedButton addTarget:self
action:#selector(doSomething:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *myOtherCreatedButton = [[UIButton alloc] init];
[myOtherCreatedButton addTarget:self
action:#selector(doSomethingElse:)
forControlEvents:UIControlEventTouchUpInside];
In the above code the target (set to self) is the class where the method you want to run is found, the action is the method that you want to run and the controlEvent is what should cause the method to run.
If you did it like this you would split the code in different methods like these (you do not need to specify them in the header):
-(void)doSomething:(id)sender {
// do somthing here ...
}
-(void)doSomethingElse:(id)sender {
// do somthing else here ...
}
This way you don't need to know what button was pressed since the correct code would get called anyway. Besides it makes it cleaner if you need to change the code for some of the buttons.
Found it!
-(IBAction)buttonTapped:(id)sender {
UIButton *btn = (UIButton *)sender;
NSLog(#"tapped: %#", btn.titleLabel.text);
[self anotherIBAction:sender];
}
now i can get the tag from the btn :D
thnk you!
Why not add a tag the button and then get the tag number from (id)sender in the selector function?