UITextView hasText not working? - iphone

I am assigning the contents of the clipboard to the UITextView text property. However, when I check the hasText property, the condition is always false.
NSString paste_text = [[NSString alloc] init];
self.paste_text = [UIPasteboard generalPasteboard].string;
....
my_UITextView.text = self.paste_text;
//THIS CONDITION IS ALWAYS FALSE
if (my_UITextView hasText)
{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
#"Text ready to copy"
message:err_msg
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}

You need to send your UITextView the hasText message by using brackets:
if ([my_UITextView hasText])
UPDATE:
Do you know that your UTTextView has text? You might want to check it on the console:
my_UITextView.text = self.paste_text;
NSLog(#"my_UITextView.text = %#",my_UITextView.text); // check for text
//THIS CONDITION IS ALWAYS FALSE
if ([my_UITextView hasText])

First off, your paste_text isn't used as intended since right after you alloc, it will immediately be discarded, possibly released, maybe not. You could actually just do away with the [[NSString alloc] init];.
Then add the following to test your code:
// delete:
// NSString paste_text = [[NSString alloc] init];
NSLog([UIPasteboard generalPasteboard].string);
self.paste_text = [UIPasteboard generalPasteboard].string;
NSLog(self.paste_text);
....
my_UITextView.text = self.paste_text;
NSLog(#"my_UITextView is %#, text contained: %#, my_UITextView , my_UITextView.text);
The first NSLog prints out the pasteboard string, the second the string once it is passed on to your paste_text, and the last will let you know if my_UITextView is non-nil, and what text it contains.
Also, if paste_text is a #property, what are its attributes? The text from [UIPasteboard generalPasteboard].string needs to be copied into it, otherwise when the pasteboard's string is changed, so is your paste_text.

How are you creating the my_UITextView property? If you did it in InterfaceBuilder, it's possible that you forgot to create an IBOutlet.

Related

How to get value from UITextfield and sent it to email?

I've created an app which contains form and that have to filled up in appropiate text fields..now i need to get all data in textfield and it to be sent in email.here is my code for composing email
- (IBAction)compose:(id)sender
{
if (text1.text=#"" && text2.text=#"" && text3.text=#"" && text4.text=#"" && text5.text=#"") {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Please enter all the field details"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
}
else{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:[NSString stringWithFormat:#"Appointment From Mr/Mrs. %#",text1.text]];
NSArray *toRecipients = [NSArray arrayWithObjects:#"xxx#xxxx.com", nil];
[mailer setToRecipients:toRecipients];
NSString *emailBody =[NSString stringWithFormat:#"Name =%#\nDate= %#\nPreferred Time Slot= %#\nE-Mail=%#\nSpecific Requests=",text1.text,text2.text,text3.text,text4.text,text5.text];
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
}
if all the text field is null it have to popup an alert.But i'm getting error?...guide me pls..
If you are wondering how to get the text from textfield, and send it, you will need to use textfield.text
NSString *emailBody = textField.text;
[mailer setMessageBody:emailBody isHTML:NO];
There can be two way -
You can create proprty for your text field, and then access like - myTextField.text where you want.
You can create a NSString property and set value in to this when text field has some text.
//this is one single line which return your textFiledData
your_text_field.text
Create a variable for ur textfield like IBOutlet UITextField * textFieldVar; and connect it to ur Text field in the nib , then using the following code u can get the text from the text field
NSString *emailBody = textFieldVar.text;
You can create the body of email as you want by appending the string with particular format
Like
User ID: 223984775(value from the textfieldUserID)
Name: XYZ(value from the textfieldName)
......
You can do this by appending the string as the user moves to the next text field to enter.
Use Nsstring "stringWithFormat" method to append the data in the body. Use \n and \t to give a Tabular structure to the body of email. Use formatting too

Error with NSDictionary

I am trying to generate an UIActionSheet from a NSDictionary.
codeCountryMap is NSDictionary variable defined in .h file. The code compiled correctly but crashes on run time. But the whole code works when the initialization is done in the handleEvents method
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *codes = [NSArray arrayWithObjects:#"91", #"01", #"002", nil];
NSArray *cName = [NSArray arrayWithObjects:#"ABC", #"QWE", #"XYZ", nil];
codeCountryMap = [NSDictionary dictionaryWithObjects:codes forKeys:cName];
}
-(IBAction) handleEvents:(id)sender
{
UIActionSheet *displayCodeCountryMap = [[UIActionSheet alloc] initWithTitle:#"Select Country" delegate:self cancelButtonTitle:nil
destructiveButtonTitle:nil otherButtonTitles:nil,nil];
for(id key in codeCountryMap) {
[displayCodeCountryMap addButtonWithTitle:(NSString *)key];
}
[displayCodeCountryMap addButtonWithTitle:#"Cancel"];
displayCodeCountryMap.cancelButtonIndex = [codeCountryMap count];
displayCodeCountryMap.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[displayCodeCountryMap showInView:self.view];
[displayCodeCountryMap release];
[country resignFirstResponder];
}
The app crashes when the handleEvents: method is called.
Any kind of help would be greatly appriciated.
Thanks in advance.
This more than likely is a memory management issue. It looks like you are setting the iVar codeCountryMap to an autoreleased object of NSDictionary and not retaining it(at least not in this code snippet). This means that when the 'handleEvents' method is called the NSDictionary item has been released from memory.
Try either retaining the NSDictionary item or, if codeCountryMap is defined as a retained property, then use the following code to set it.
self.codeContryMap = [NSDictionary dictionaryWithObjects:codes forKeys:cName];
This will use the synthesized methods to set the property and retain it.
No need for 2 nils terminators in the otherButtonTitles argument. If you had passed in strings then it would have to be nil terminated. Since you did not, one nil is enough.
UIActionSheet *displayCodeCountryMap = [[UIActionSheet alloc] initWithTitle:#"Select Country" delegate:self cancelButtonTitle:nil
destructiveButtonTitle:nil otherButtonTitles:nil];

how can I save data of UITextField into NSMutableArray?

I am writing a simple program having two UITextFields and one UIButton, when I press UIButton, a method is called, the coding of that method is as below
-(void) saveData{
restaurant_name_save = restaurant_name_textfield.text;
amount_name_save = amount_textfield.text;
order = [[NSMutableArray alloc] initWithObject: restaurant_name_save, amount_name_save, nil];
NSLog(#"%#", order);
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"warning" message:[NSString stringWithFormat:#"%d",[order count]] delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
In NSLog, both UITextField data is showing properly, but in UIAlertView, it is ever showing 2, even when I change the data and again press button...
What should I do, I simply want to save the data for each time in NSMutableArray, as I press button,,
please help ....
I might be missing something here but
it says [order count]:
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"warning"
message:[NSString stringWithFormat:#"%d",[order count]]
delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
The output of [order count] will be 2 because there are two entries in the array.
I would assume to see the contents of the array you write
stringWithFormat:#"%#", order
You might have a typo. Try using -initWithObjects: instead:
order = [[NSMutableArray alloc] initWithObjects: restaurant_name_save, amount_name_save, nil];
On repeat runs, also make sure you release the member variable order and set it to nil, before doing another +alloc and -initWithObjects: call:
if (order) {
[order release], order = nil;
}
order = [[NSMutableArray alloc] initWithObjects: restaurant_name_save, amount_name_save, nil];
...
Better yet, don't repeatedly use +alloc and -initWithObjects: in this method, but outside this method (perhaps in the larger object's init method) create an NSMutableArray of capacity 2:
self.order = [[[NSMutableArray alloc] initWithCapacity:2] autorelease];
In the method that handles the button action, set the items at their respective indices:
[order replaceObjectAtIndex:0 withObject:restaurant_name_textfield.text];
[order replaceObjectAtIndex:1 withObject:amount_textfield.text];
use initWithObjects insted of initWithObject
and always two objects set in the array and you are printing the count of array thats why it always show 2.
all other code written by you is correct.
If I follow you correctly, you want to have one array in which you want to store restaurant_name_save and amount_name_save each time a button on your screen is pressed. (I'm assuming this button calls the saveData method?
In that case, reallocating your array will clear any objects in it first and then adds the two strings to it. You should declare your array as a class variable and just do
[order addObject:string1];
[order addObject:string2];
EDIT:
Are you allocating it though? Here's a simple way of doing it, you might need to modify it to suit your needs -
Header file:
#interface WelcomeScreen : UIViewController {
NSMutableArray *array;
}
#property (nonatomic, retain) NSMutableArray *array;
-(IBAction) saveData:(id) sender;
Source file
#synthesize array;
-(void) viewDidLoad {
array = [[NSMutableArray alloc] init];
}
// Make the UIButton's tap option point to this -
-(IBAction) saveData:(id) sender
{
[array addObject:string1];
[array addObject:string2];
// alert.
}

iPhone: Using Alerts to Help Debugging

I've been building a rather complex system and there's come the time now where I want more concise debugging. I would like to display the contents of a variable (for this example an NSString called v_string) in a notification window (the kind of window that appear when you receive an SMS text).
Is there an easy way to just call an alert with a variable?
Thanks in Advance,
Dan
NSLog does not do? If not (like if you need to debug an application running on a disconnected device), you can extend the UIAlertView with a category:
#implementation UIAlertView (Logging)
+ (void) log: (id <NSObject>) anObject
{
NSString *message = [anObject description];
UIAlertView *alert = [[self alloc] initWith…];
[alert show];
[alert release];
}
And then in code:
NSString *anInterestingString = …;
[UIAlertView log:anInterestingString];
When you build the string to display in the alert window, simply append your variable's string represenation using stringByAppendingString.
Alert window is cumbersome. Use NSLog instead:
NSLog(#"Variable is: %#", v_string);
And in Xcode's console you will see that text.
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"My Debug String" message:v_string delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[message show];
[message release];
I think this way you can see what you want.
But, as zoul said, why not to use NSLog(#"my var: %#", v_string); ?
Hope that it helps.

Help adding string inside alert view

I have a sting that is equal to a text field that i want to insert inside of a UIAlertView.
NSString *finalScore = [[NSString alloc] initWithFormat:[counter2 text]];
UIAlertView *myAlertView = [[UIAlertView alloc]
initWithTitle:finalScore
message:#"You scored X points"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"OK", nil];
[myAlertView show];
[myAlertView release];
I want to replace"X" with the score from the string "finalScore"
I know how to have it just with the score, you would simple just enter the strings name without quotes under the message section, but i want the the text there too along with the string.
Thanks a bunch,
Chase
Replace your message: parameter with:
message:[NSString stringWithFormat:#"You scored %# points", finalScore]
Also, note that your original code for finalScore uses string formatting without any arguments, which is either a no-op or unsafe (if it contains a % symbol). You are also leaking your finalScore object.
Try something like the following, and pass it as the message variable to UIAlertView's initialization:
NSString* message = [NSString stringWithFormat:#"You scored %# points",
[counter2 text]];
You might have to change some details about the formatting string depending on the type that comes out of [counter2 text], however, though the above should work in many of the cases you'll encounter.
Instead of:
#"You scored X points"
use:
[NSString stringWithFormat:#"You scored %# points", finalScore]