iPhone Development xcode : Putting user input (names) into text array - iphone

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

Related

How to show a UIAlertView upon selection of an item in UIPickerView

In my picker view i have a "Custom" option which should popup a UIAlertView for the user to enter a new value, the value is saved in the plist source of the picker for future reference. xxxEditingDidBegin is being called repeatedly (never ending).
I presume its because my UIAlertView is triggering the picker to close.
How should I have done this?
- (IBAction)serviceTypeFieldEditingDidEnd:(UITextField *)sender
{
UIPickerView *picker = [sender.inputView.subviews objectAtIndex:0];
NSString *selText = [serviceTypeArray objectAtIndex: [picker selectedRowInComponent:0]];
sender.text = selText;
if (NSOrderedSame==[selText compare:#"Custom"])
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Custom Role"
message:#"Enter Role Title"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];
}
}
Implement method like
- (IBAction)doSelectDate:(UIDatePicker *)sender
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Hi" message:#"AlertView is shwoing" delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
and connect above method with ValueChanged Event of UIPickerView;
I fixed it like this
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
NSString *rowString = [serviceTypeArray objectAtIndex:row];
if ([rowString compare:#"Custom"] == NSOrderedSame)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Custom Role"
message:#"Enter Role Title"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];
}
else
{
_serviceType.text = rowString;
}
}

memory problem with array releasing in objective-c

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.

objective-c : iphone programming : Showing variables on UIAlert view

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

How to upload photos to Plixi (Tweetphoto) using Oauth?

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.

iPhone SDK simple alert message question

char asd='a';
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Are you sure?" message:asd
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
above code not compiling in iPhone simulator.
Why is that?
:)
You're trying to pass a char where an NSString is expected. Read up on Objective-C and th Cocoa/Cocoa Touch documentation.
To fix your issue, try this:
NSString *asd = #"a";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Are you sure?" message:asd
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert show];
[alert release];
You don't compile in the iPhone
simulator, you compile on your mac.
Do you get an error message?
The message parameter should be an NSString, not a char?
replace
char asd='a';
With
NSString *asd=#"a";
It should compile after it...