UITextField settext not working - iphone

This is frustrating the hell out of me. I am a beginner programmer and cannot figure out why the text is not being changed.
Here is my method which is supposed to set the text of a UITextField:
-(void)updateDays:(NSInteger)days
{
NSString* daysString = [[NSString alloc] initWithFormat:#"%d", days];
[daysTextField setText:daysString];
[daysString release];
}
For whatever reason nothing is happening.
Help appreaciated!

Anytime you have a frustration along the lines of "why isn't this line working", use the debugger or just add an NSLog before it to print out the relevant data:
NSLog(#"updateDays: %# %# <= %#", daysTextField, daysTextField.text, daysString);
Then you know the line (a) is getting executed, (b) the variables are the ones you think they are, and (c) the values are reasonable.

I have experienced this several times, but I think all of them was reasoned by that the UITextField was not initialized at that point.
Set a breakpoint and use debugger to make sure that your UITextField is not nil. You should also check the connection between the .xib file and your code.

Check if the method is called. If yes check if the textfield is set up properly. Cross check to see if the IBOutlet connections are made to the correct object.

I meet this problem too .
It's my code:(in viewdidload)
UITextView *tv = [[UITextView alloc]initWithFrame:CGRectMake(0,0,320,50)]];
tv.text = #"12345";
tv.textColor=[UIColor blackColor];
[self.view addSubView:tv];
And the text(12345) doesn't show;
Lastly,I found that when i set textColor to gray,or any color not black,it works;
I think that it's a bug of the simulator.
I use xcode 4.2 and the iphone5.0simulator.

Related

How do I save values from a UITextField in the FlipsideViewController and use them in the MainViewController?

SEE EDIT AT BOTTOM
I am making a UtilityView application for the iPhone and I want to be able to change values from the FlipsideViewController. I have a problem that whenever I switch back to the MainView from the FlipsideView, the values of the text fields that I had changed in the FlipsideView are seemingly reset to 0. In the MainViewController.m, under the flipsideViewControllerDidFinish:
playToInt = [controller.playTo.text intValue];
computerMoveSpeed = [controller.aiSpeed.text intValue];
The text field 'playTo' coordinates with the integer 'playToInt' and the text field 'aiSpeed' coordinates with the integer 'computerMoveSpeed'. I think this should save the values, even when the ViewDidLoad runs again, but they both seem to be reset to 0.
if (playToInt != 10 || computerMoveSpeed != 3)
{
playBool = true;
}
else
{
playBool = false;
}
I know that the boolean is working because I can change the integers' values with it, but they always default back to 0. Any help is appreciated. Thanks in advance.
NOTE: I have consulted the previous question 'Passing UISegmentedControl values from FlipSideViewController in an Utility application to the mainviewcontroller…' and that does not work for me... I am have too many possible values to use an index or a switch.
EDIT: I am trying something that I think is really close to working... I feel like it just has bad syntax or something... I have in the MainViewController.m:
computerMoveSpeed = [FlipsideViewController.aiSpeed.text intValue];
playToInt = [FlipsideViewController.playTo.text];
EDIT: I have something that is similar to the last one but I think it will have a better chance of working with some help...
in the FlipsideViewController.m I have
_playToNumber = [_playTo.text intValue];
and in the MainViewController.m
playToInt = [controller.playToNumber];
Any help is greatly appreciated, but as some additional info, on the string in the MainViewController it says it needs an identifier on the playToNumber variable, but it is declared as int... I don't know if there is an identifier for ints but if there is, please let me know!
EDIT: Is there any way to save the values in a usable format for the MainViewController using the prepareForSegue function in the FlipsideViewController?
EDIT: I figured out that it was just getting the initial values when it loaded the ViewDidLoad, so I started using a timer to retrieve the values. This seems to be working, I just need to know how to access these values from the MainViewController?
Use delegate to send values back to MainViewController. See this tutorial to learn how to use delegates.
You have many way to do this task,
1. NSUserDefault
Follow ma answer Link
2. Using Getters, setters, property
Follow ma answer Link
EDIT
If you want int or NSInteger then no need for retain
just do
#property(nonatomic) NSInteger YourIntValue;
and
_playToNumber = [_playTo.text intValue];
MainViewController *MVC = [[MainViewController alloc] init];
MVC.playToInt=_playToNumber;
[self.navigationController pushViewController:MVC animated:YES];

How to make tableview cell "detailLabel" editable?

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

UITextField isn't working

am new to objective-c programming, so sorry if it's a silly question but i looked thoroughly and couldn't find the error,
p.s: english isn't my first language so programming keywords might be unintentionally used
I've set up a txtfield and defined some of it's properties, such as the keyboard-number pad and stuff, the problem is, when i run it on the simulator, nothing happens, the defult keyboard is called.
i'm just trying to do simple stuff to get it in my head,
in my .h, i have
#interface practiceViewController : UIViewController <UITextFieldDelegate>
{
UITextField *userInput;
}
#property (nonatomic, retain, readwrite) UITextField *userInput;
#end
and in my .m
-(UITextField *)userInput{
if (userInput == nil) {
userInput=[[UITextField alloc]init];
userInput.keyboardType= UIKeyboardTypeNumberPad;
userInput.returnKeyType= UIReturnKeyDone;
userInput.textColor=[ UIColor redColor];
userInput.clearButtonMode= UITextFieldViewModeWhileEditing;
userInput.delegate = self;
}
return userInput;
}
i also tried to make it as an input, but for some reson it's not working
int nInput=[[userInput text] intValue];
whats pissing me off about this is that i did the same in a previous project and the "input worked" only, but now everything isn't.
and my property as i haven't defined anything.
i appreciate your help
I Think you have to set the frame for UITextField like this ...
userInput.frame=CGRectMake(100,100,60,31);
in your -(UITextField *)userInput method, that's y it didn't show ..
Well few things are hazy but ...
You are getting the value of the text field using,
int nInput = [[userInput text] intValue];
If you are not getting the correct value then nInput will be zero. That is because userInput must be nil. You can verify this by logging it by adding NSLog(#"%#", userInput); before the line above.
Whatever value is printed, it looks like your on screen text field is not referenced to by userInput.
Since you're doing the entire thing programmatically, you will have to add your userInput to the view hierarchy by doing this,
[self.view addSubview:self.userInput];
Using the property accessor is important here is important as otherwise userInput would be nil unless you've accessed it using self.userInput earlier.
Although this is tagged iPhone, do you happen to be simulating an iPad? If so, I have run into this. The iPad will not show a number pad keyboard.
userInput= [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 200, 30)]
userInput.keyboardType= UIKeyboardTypeNumberPad;
userInput.returnKeyType= UIReturnKeyDone;
userInput.textColor=[ UIColor redColor];
userInput.clearButtonMode= UITextFieldViewModeWhileEditing;
userInput.delegate = self;
You can just set the frame and add below property(to visible the textfield frame in View otherwise it is not visible in View).
userInput.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:userInput];
I hope it will be helpful to you.

Multiple red/destructive buttons UIActionSheet

Is there any way to have more than 1 red "destructive button" in an iPhone app's UIActionSheet?
I need to have different clear options, in the same action sheet, one where it deletes everything and one where it deletes less, so both need to be red.
I just created a simple customizable replacement for UIActionSheet for iPhone to use in a similar case. It does not use the standard appearance, but this can be changed. Probably it is of any use to you.
https://github.com/4marcus/WMActionSheet
The poster of this question seemed to manage what you want...if i understand you correctly.
Is using subviews in Alert undocumented
EDIT:
I swear I searched for UIActionSheet the first time. Really.
http://www.nearinfinity.com/blogs/andrew_homeyer/display_a_custom_uiview_like_a.html
There's no supported way of doing this on the standard UIActionSheet. You could build your own "action sheet" replacement using gradient buttons like the ones here:
http://iphonedevelopment.blogspot.com/2010/05/gradient-buttons-yet-again.html
I would expect someone to have created a more customizable lookalike action sheet replacement, but I don't know of one off the top of my head.
I tried this before I saw griotspeak updated answer:
SEL getTitle = NSSelectorFromString(#"title");
SEL getBackground = NSSelectorFromString(#"background");
SEL setBackground = NSSelectorFromString(#"setBackgroundImage:");
SEL setTitleColor = NSSelectorFromString(#"setTitleColor:");
UIImage *redImage;
UIColor *titleColor;
UIColor *shadowColor;
for (NSObject *object in [action subviews]) {
if ([[NSString stringWithFormat:#"%#", [object class]] isEqualToString:#"UIThreePartButton"]) {
if ([[object performSelector:getTitle] isEqualToString:#"Clear all"]) {
redImage = [object performSelector:getBackground];
titleColor = [object performSelector:#selector(titleColor)];
shadowColor = [object performSelector:#selector(shadowColorForState:) withObject:0];
shadowOffset = [object performSelector:#selector(shadowOffset)];
}
if ([[object performSelector:getTitle] isEqualToString:#"Clear all except this month"]) {
[object performSelector:setBackground withObject:redImage];
[object performSelector:setTitleColor withObject:titleColor];
[object performSelector:#selector(setShadowColor:) withObject:shadowColor];
//[object performSelector:#selector(setShadowOffset:) withObject:CGSizeMake(-2.5,0)];
}
}
}
(I use NSSelectorFromString rather than a #selector() because it means it's not really using undocumented things, kind of, if you get what I mean)
It's not entirely documented but I don't think I've used undocumented methods.
Basically what it does is take the red background from the destructive background and apply it to the other button named "Cancel".
So that should Apple change the color of the destructive button, so will the non-destructive-destructive change as well without needing an update. Although it doesn't use particularly Apple-safe methods.
I'm having a little bit of trouble with the commented-out line above, if you can help please answer here: performSelector:withObject:, but not with an object

iPhone UIActionSheet with multiple red, destructive buttons [duplicate]

Is there any way to have more than 1 red "destructive button" in an iPhone app's UIActionSheet?
I need to have different clear options, in the same action sheet, one where it deletes everything and one where it deletes less, so both need to be red.
I just created a simple customizable replacement for UIActionSheet for iPhone to use in a similar case. It does not use the standard appearance, but this can be changed. Probably it is of any use to you.
https://github.com/4marcus/WMActionSheet
The poster of this question seemed to manage what you want...if i understand you correctly.
Is using subviews in Alert undocumented
EDIT:
I swear I searched for UIActionSheet the first time. Really.
http://www.nearinfinity.com/blogs/andrew_homeyer/display_a_custom_uiview_like_a.html
There's no supported way of doing this on the standard UIActionSheet. You could build your own "action sheet" replacement using gradient buttons like the ones here:
http://iphonedevelopment.blogspot.com/2010/05/gradient-buttons-yet-again.html
I would expect someone to have created a more customizable lookalike action sheet replacement, but I don't know of one off the top of my head.
I tried this before I saw griotspeak updated answer:
SEL getTitle = NSSelectorFromString(#"title");
SEL getBackground = NSSelectorFromString(#"background");
SEL setBackground = NSSelectorFromString(#"setBackgroundImage:");
SEL setTitleColor = NSSelectorFromString(#"setTitleColor:");
UIImage *redImage;
UIColor *titleColor;
UIColor *shadowColor;
for (NSObject *object in [action subviews]) {
if ([[NSString stringWithFormat:#"%#", [object class]] isEqualToString:#"UIThreePartButton"]) {
if ([[object performSelector:getTitle] isEqualToString:#"Clear all"]) {
redImage = [object performSelector:getBackground];
titleColor = [object performSelector:#selector(titleColor)];
shadowColor = [object performSelector:#selector(shadowColorForState:) withObject:0];
shadowOffset = [object performSelector:#selector(shadowOffset)];
}
if ([[object performSelector:getTitle] isEqualToString:#"Clear all except this month"]) {
[object performSelector:setBackground withObject:redImage];
[object performSelector:setTitleColor withObject:titleColor];
[object performSelector:#selector(setShadowColor:) withObject:shadowColor];
//[object performSelector:#selector(setShadowOffset:) withObject:CGSizeMake(-2.5,0)];
}
}
}
(I use NSSelectorFromString rather than a #selector() because it means it's not really using undocumented things, kind of, if you get what I mean)
It's not entirely documented but I don't think I've used undocumented methods.
Basically what it does is take the red background from the destructive background and apply it to the other button named "Cancel".
So that should Apple change the color of the destructive button, so will the non-destructive-destructive change as well without needing an update. Although it doesn't use particularly Apple-safe methods.
I'm having a little bit of trouble with the commented-out line above, if you can help please answer here: performSelector:withObject:, but not with an object