i am getting problem with this can any one help me
here is my code. This code work 1st click but when click 2 time it get error
malloc: error for object 0x4e226a4: incorrect checksum for freed object - object was probably modified after being freed.
** set a breakpoint in malloc_error_break to de**bug
- (void)updateTextViewContents {
content = [[NSMutableString alloc] init];
for (int i = 0; i <[ _scoresArray count]; i++)
{
NSMutableString *data = [_scoresArray objectAtIndex:i];
[content appendString:data];
if([content isEqualToString:UserText.text]&&[content isEqualToString:PassText.text])
{
UIAlertView *alt = [[UIAlertView alloc] initWithTitle:nil message:#"Valid User" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alt show];
[alt release];
[content release];
}
else
{
UIAlertView *alt1 = [[UIAlertView alloc] initWithTitle:nil message:#"NOT A Valid User" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alt1 show];
[alt1 release];
}
}
when i release over here it work but when i click 2nd time by entering right user name and password both alter view display at time .I think it will happen because of array get add at every cilck that's why both alter view dispaly at time how i sovle this .
//[content release];
}
Here generate memory problem because of,
you release content NSMutableString in if condition and then append it again.
So, Don't release in to for loop, release it out of for loop.
- (void)updateTextViewContents
{
content = [[NSMutableString alloc] init];
for (int i = 0; i <[ _scoresArray count]; i++)
{
NSMutableString *data = [_scoresArray objectAtIndex:i];
[content appendString:data];
if([content isEqualToString:UserText.text]&&[content isEqualToString:PassText.text])
{
UIAlertView *alt = [[UIAlertView alloc] initWithTitle:nil message:#"Valid User" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alt show];
[alt release];
//[content release]; - Not release here.
}
else
{
UIAlertView *alt1 = [[UIAlertView alloc] initWithTitle:nil message:#"NOT A Valid User" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alt1 show];
[alt1 release];
}
}
[content release];
Can't you replace
NSMutableString *data = [_scoresArray objectAtIndex:i];
[content appendString:data];
with this
[content appendString:[_scoresArray objectAtIndex:i]];
your both alert is showing because of your loop put break after it is valid user because you must not go through loop after it is valid user
hope this will help
good luck
You have release content in the if condition. For the next iteration you are again trying to append data to content, which is already released if the condition was true in previous iteration. And thus you are getting that error object was probably modified after being freed.
Related
I'm trying to get text from an alert view and add it to my mutable array to list in a table view. I realize there is a similar question that was posted a few months ago, but I dont understand how to utilize the given answer.
-(IBAction)insert {
UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:#"Enter Name"];
[dialog setMessage:#" "];
[dialog addButtonWithTitle:#"Cancel"];
[dialog addButtonWithTitle:#"OK"];
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
[dialog show];
[data addObject:[nameField text]];
[mainTableView reloadData];
However my app crashes because it says I'm attempting to insert a nil object at index 0. What am I doing wrong?
EDIT: Ok I think I'm missing a method to handle the alertview. So I found this:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if([buttonTitle isEqualToString:#"Cancel"]) {
return;
}
else if([buttonTitle isEqualToString:#"Ok"]) {
[data addObject:nameField.text];
}
Now I just need to connect the pieces, but not sure how.
A common mistake that people make when using a UIAlertView is that they think that sending it a show message will block the main thread. It does not. Your code continues to execute, even though there is an alert on the screen. Thus, no value exists for the UITextField that you have passed in.
What you need to do is implement the UIAlertViewDelegate in your UIViewController to grab whatever a user has entered into the text field. That said, you still have to check for a nil value, because if you don't type anything in, the text value will be nil.
UPDATE: This answer was posted before Apple created an API for adding a UITextField to an alert through the UIAlertViewStyle. Here is the updated code, borrowed from #matt
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter Name"
message:#" "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
Then, in the UIAlertViewDelegate call back:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSString *name = [alertView textFieldAtIndex:0].text;
// Insert whatever needs to be done with "name"
}
}
You don't need to add your own text field to the AlertView but instead set the appropriate style
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter Name"
message:#" "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
Then access the entered text after OK (button 1) was pressed
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
NSString *name = [alertView textFieldAtIndex:0].text;
// name contains the entered value
}
}
Don't try to add a UITextView on a UIAlertView or else Apple might reject your app.
I already used the same kind of functionality in my app. But Apple REJECTED MY APP due to the use of UITextView in UIAlertView.
– alertView:clickedButtonAtIndex: is a delegate method of UIAlertView. Just add the method you defined in your edit to your controller implementation and you should be good to go, since you already set the controller to be the AlertView's delegate.
Also add to your class header, to avoid any delegate related warnings, ie:
#interface MyViewController : UIViewController <UIAlertViewDelegate> {
...
};
Don't forget to remove [data addObject:[nameField text]]; and [mainTableView reloadData]; from the insert method.
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"Alert title"
message:#"alert message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
[myAlert addTextFieldWithValue:nil label:#"<place holder>"];
[[myAlert textField] setTextAlignment:UITextAlignmentCenter];
[[myAlert textField] becomeFirstResponder];
[myAlert show];
[myAlert release];
myAlert = nil;
No need to add a custom textfield. You can directly add using addTextFieldWithValue method.
my app needs alert msg and if yes button pressed then one more alert msg and then i have to called a method.This is my code:
-(IBAction)resetPressed:(id)sender
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag ==1)
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
alert.tag =2;
[alert show];
[alert release];
}
else if(alertView.tag ==2)
{
[self resetArray];
}
}
Thanks.
I'm not sure what your goal is but a few things look wrong to me anyways:
First of all you should create your strings this way:
NSString *title= #"Warning";
There's no need to use stringWithFormat in your case.
Then, it doesn't seem you properly set the first UIAlert's tag to 1, and the default value for tags is 0 so I guess the if statements in didDismissWithButtonIndex are never true.
Also, you should check which button was pressed using buttonIndex, otherwise you are going to show both alert and call [self resetArray] whichever button is pressed by the user.
Hope that helps.
In your code, you create the first alert, but never actually set the tag on it. You should do:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
alert.tag = 1; //Or 2, or something.
[alert show];
[alert release];
Then the code in your delegate method will run.
Please define two separate UIAlertView in .h file
#interface XYZViewController:UIViewController
{
UIAlertView *firstAlertView;
UIAlertView *secondAlertView;
}
Now in your .m file modify as below:
-(IBAction)resetPressed:(id)sender
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
if(firstAlertView == nil)
{
firstAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
}
[firstAlertView show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView == firstAlertView)
{
NSString *title= [NSString stringWithFormat:#"Warning"];
NSString *message = [NSString stringWithFormat:#"Are you sure you want to Reset"];
NSString *ok = [NSString stringWithFormat:#"No"];
if(secondAlertView == nil)
{
secondAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:#"Yes",nil];
}
[secondAlertView show];
}
else if(alertView == secondAlertView)
{
[self resetArray];
}
}
and in dealloc method please release the allocated UIAlertviews.
Hope i am clear to you.
Thanks,
Jim.
i want simply to show a double value on a uialert view it is possible?
Put the value into a formatted NSString and send that to the alertView:
NSString *messageString = [NSString stringWithFormat:"#number = %.2f", 42.0];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Title text" message:messageString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
How are you showing the UIAlertView now? Something like the following should work:
double myDouble = 12.6;
NSString *alertString = [NSString stringWithFormat:#"%g", myDouble];
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:alertString
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
[myAlert show];
I am creating an iphone application in which I have to upload photos using different services.
I am successful in uploading photos with Twitpic & YFrog but not able to do with Plixi.
I am using Oauth ,as Twitter is not allowing Basic authentication.
If anyone has tried with Plixi ,please help me out!!
I have googled a lot but not getting any relevant documentation for the new Oauth for Plixi.
Finally, I am with a solution for my problem :)
If anyone is also stuck with this problem, just add the TweetPhoto folder from the following application link:
http://code.google.com/p/tweetphoto-api-objective-c/downloads/detail?name=TPAPI-Objective-C-Library.zip
Change the tweetphoto urls for plixi now.
Also, can refer to my following code for making function calls:
-(void)uploadtoTweetPhoto{
NSString *message = [self.tweetTextView.text stringByReplacingOccurrencesOfString:#"Max. 140 characters" withString:#""];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
if((message == nil) || ([message isEqual:#""])){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Please enter your tweet message.." message: #"" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
else{
[dictionary setObject:message forKey:#"message"];
}
if([dictionary count] == 1){
[indicator startAnimating];
[NSThread detachNewThreadSelector:#selector(uploadPhotoToTweetPhoto:) toTarget:self withObject:dictionary];
}
[dictionary release];
}
- (void)uploadPhotoToTweetPhoto:(NSDictionary *)dictionary{
NSString *message = [dictionary objectForKey:#"message"];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *accessTokenKey = [[NSUserDefaults standardUserDefaults] valueForKey:#"oauth_token"];
NSString *accessTokenSecret = [[NSUserDefaults standardUserDefaults] valueForKey:#"oauth_token_secret"];
TweetPhoto *tweetPhoto = [[TweetPhoto alloc] initWithSetup:accessTokenKey identitySecret:accessTokenSecret apiKey:Plixi_API_Key serviceName:#"Twitter" isoAuth:YES];
NSData *dat =[tweetPhoto upload:UIImageJPEGRepresentation(self.imgView.image,0.8) comment:message tags:#"" latitude:23.4646 longitude:-87.7809 returnType:TweetPhotoCommentReturnTypeXML];
NSString *status =[NSString stringWithFormat:#"%i",[tweetPhoto statusCode]];
[tweetPhoto release];
[self performSelectorOnMainThread:#selector(photoUploadedtoTweetPhoto:) withObject:status waitUntilDone:[NSThread isMainThread]];
[pool release];
}
- (void)photoUploadedtoTweetPhoto:(NSString*)status{
[indicator stopAnimating];
if([status isEqualToString:#"201"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Tweet Posted" message: #"" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Tweet Failed" message: #"" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
I'm curious as to the phrase you used in Google. In any case, googling "plixi api" tool me to this page, which links to a Cocoa wrapper on Google Code.
I am trying to load names in an array using UIAlert and then print them one by one. I am not able to figure this out, can anyone help me out with this? I would really appreciate your help.
NSArray *array = [NSArray arrayWithObjects:#"One", #"Two", #"Three", #"Four", nil];
for (NSString *element in array)
{
// Log to console..
NSLog(#"element: %#", element);
//Show in alert.. Really??
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ALERT TITLE" message:element delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
NSArray *array = [NSArray arrayWithObjects:#"One",#"Two",#"Three",nil];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"ALERT TITLE" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
for(NSInteger index=0; index<[array count]; index++)
{
[alert addButtonWithTitle:[array objectAtIndex:index]];
}