Keeping text in an alert view, and saving text to an NSString? - objective-c

I have a UITableView that is meant have a list of names. I want it to bring up an UIAlertView whenever a name is clicked, so that the accompanying information to the name (for now just the age) will be allowed to be changed.
As of now, I can get an alertView with a UITextField to appear, however once you close the alertView, the text inside the UITextField resets to the placeholder. How can I get the text to stay with the UIAlertView?
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:#"Set Age"
message:[_tableData1 objectAtIndex:indexPath.row]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alert textFieldAtIndex:0];
textField.placeholder = #"18";
[alert show];
}
Also, how can I get the text from the UIAlertView, and save it as a NSString?
Thank you!

You need to set your view controller as the delegate and then retrieve the value in the callback. You also need to add UIAlertViewDelegate to your #interface
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Set Age" message:[_tableData1 objectAtIndex:indexPath.row] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alert textFieldAtIndex:0];
textField.placeholder = #"18";
[alert show];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if(buttonIndex == alertView.cancelButtonIndex) {
return;
}
NSString * yourString = [alertView textFieldAtIndex:0].text
}

Related

How to pop up UIAlertView when the numberofrow (TableView) is 1

I have an 'add' button to create new row of tableview. And the number of row is limited to one.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
I'd like the button to pop up an alert view when there is one row, not allowing to create a new row.
However i don't know how to implement an 'if condition' in the Button action as following:
- (IBAction)add
{
if (condition)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Hi" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
}
[alert show];
}
Please Help! Sorry for not being professional, I'm trying to learn objective c
All you have to do is -
- (IBAction)add
{
if ([self.tableView numberOfRowsInSection:0] == 1)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Hi" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
}
[alert show];
}
I think you need have an mutableArray to hold the data of UITableView and return the count of array provide to UITableView through "numberOfRowsInSection" method, when you want to add a row , you just add a data into the Array and reload the tableView.
ps:you can't add row but return 1 to UITableView
Create boolean variable shouldCreateRow.
Write the tableview datasource method as
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if(shouldCreateRow){
return 1;
}
}
and the action method as
- (IBAction)add:(UIButton *)sender {
sender.selected=!sender.selected;
if (sender.selected && !shouldCreateRow) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert" message:#"One row created" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
shouldCreateRow=TRUE;
[myTable reloadData];
}else{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert" message:#"Already there is One row." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}

How to show detail in table view on xcode

How can I create 10 rows in a table view on xcode and show a detail as alert when I click on them?
You have to write Logic in didselectRowAtIndexPath Method
arr is NSMutablearray..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Detail"
message:[arr objectAtIndex:indexPath.row]
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}

Two UIAlertView consecutively in didFinishLaunchingWithOptions

I want two alert views to show up only when the user opens my application for the first time -- the second to appear after the first is dismissed. I have it set up to only show the UIAlertViews when it has not been shown before and I do not need help with this. I need help figuring out how to display two alert views in a row when this is the case.
-(void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex does not work for me.
Here is the code I have -- remember this is in didFinishLaunchingWithOptions:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL didFirstLaunch = [defaults boolForKey:#"DidFirstLaunch"];
if (!didFirstLaunch) {
[defaults setBool:YES forKey:#"DidFirstLaunch"];
UIAlertView *successAlert = //not important
[successAlert show];
[successAlert release];
//Somehow show second alert after the first is dismissed
}
I'm gonna post a very simple solution using GCD & blocks (GCD part is just in case the alert view is created on another thread then the main thread, callback should be safe to perform on the main thread). Remember, I just coded this in like 5 mins, so you definitely should work on improving the code. One thing that's a bit ugly is the delegate parameter that is overridden in my subclass. The interface of the subclass could be changed a bit to make it more obvious of what happens ...
Anyway, here goes ...
First create a subclass of UIAlertView, make it look somewhat like the following ...
#interface FSAlertView () <UIAlertViewDelegate>
#property (nonatomic, copy) void (^dismissHandler)(NSInteger buttonIndex);
#end
#implementation FSAlertView
#synthesize dismissHandler = _dismissHandler;
- (void)showWithDismissHandler:(void (^)(NSInteger buttonIndex))dismissHandler
{
self.dismissHandler = dismissHandler;
self.delegate = self;
[self show];
}
// Alert view delegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
dispatch_async(dispatch_get_main_queue(), ^ {
if (_dismissHandler)
{
_dismissHandler(buttonIndex);
}
});
}
Now in the app we can create alert views like the following ...
FSAlertView *alert1 = [[FSAlertView alloc] initWithTitle:#"Alert 1"
message:#"Some message"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Show 2nd Alert", nil];
[alert1 showWithDismissHandler:^ (NSInteger buttonIndex) {
NSLog(#"button pressed: %d", buttonIndex);
if (buttonIndex == 1)
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Alert 2"
message:#"Hi!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert2 show];
}
}];
If i understand your question correctly , then this may help:
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"Alert 1" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[firstAlert show];
[self performSelector:#selector(test:) withObject:firstAlert afterDelay:2];
[firstAlert release];
UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:#"Alert 2" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[secondAlert show];
[self performSelector:#selector(test:) withObject:secondAlert afterDelay:2];
[secondAlert release];
-(void)test:(UIAlertView*)alert{
[alert dismissWithClickedButtonIndex:-1 animated:YES];
}
This will show two alert views one after the other.
NOTE: I am not sure if you are dismissing the alerts with cancel button so i am dismissing them automatically after few seconds.
Try this:
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[firstAlert setTag:444];
[firstAlert show];
firstAlert = nil;
AlertView Delegate Method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
switch (alertView.tag) {
case 444:
{
//Cancel ButtonIndex = 0
if (buttonIndex == 1) {
UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:#"Title 2" message:#"Message2" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Dismiss", nil];
[secondAlert setTag:555];
[secondAlert show];
secondAlert = nil;
}
}
break;
case 555:
{
if (buttonIndex == 1) {
NSLog(#"Code Here");
}
}
break;
}
}

clickedButtonAtIndex: not working

Hi guys i have this i IBAction linked to a button:
- (IBAction)showCurl:(id)sender {
alert1 = [[UIAlertView alloc]initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert1 show];
}
and a clickedButtonIndex to auto run but somehow it doesn't load SecondViewController:
#pragma mark UIAlertView
- (void)alertView:(UIAlertView *)alert1 clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0){
SecondViewController *sampleView = [[SecondController alloc] init];
[sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:sampleView animated:YES];
}
else{
// Cancel prompt
}
}
Am i missing something here?
If you don’t give the alert view some button titles, there won’t be any buttons to tap on and that delegate method won’t get called.
- (IBAction)showCurl:(id)sender {
alert1 = [[UIAlertView alloc] initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert1 show];
}
Your code doesn't show a button
alert1 = [[UIAlertView alloc]initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:**nil** otherButtonTitles:**nil**];
You pass nil as cancelButtonTitle AND nil as otherbuttonTitles, you should at least have one button title set.

Request Fail in ASIHttprequest

i am using ASIHttpRequest in my iOS project. In the requestFail i am doing like this :
#pragma mark Erreur requête de connexion
- (void)requestFailed:(ASIHTTPRequest *)request
if([[request error] code] == ASIConnectionFailureErrorType )
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Erreur de connexion"
message:#"Connexion avec le serveur impossible : vérifiez vos paramètres réseaux."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
else if ([[request error] code] == ASIRequestTimedOutErrorType)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Erreur de connexion"
message:#"La requête a expiré !"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Erreur de connexion"
message:#"Une erreur inconnue est survenue."
delegate:nil
cancelButtonTitle:#"Annuler"
otherButtonTitles:nil];
[alert show];
}
}
Now, i would like to replace all my UIAlertView with an imageView ( show to the user a special UIImageView to tell him that there is an error ( connexion failure....), and remove this imageView when there is not server error.
Resume : I would like to show an imageView when the request fail and remove this image view and relance the request ( How I can do this with ASIHtppRequest) ?
I think you can use a UIActionSheet to replace the UIAlertView because the ActionSheet can be handled like a UIView, so you can present the ActionSheet that contains your UIImageView. You can also use one of the buttons to dismiss the ActionSheet to reload your ASIHTTPRequest.
The following code shows you the UIActionSheet's delegate methods that allow you to add your UIImageView and trigger your request when the cancel button is clicked.
#pragma -
#pragma ActionSheet Delegate
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
UIImageView * yourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 55, 320, 100)];
//Configure yourImageView...
//Add picker to action sheet
[actionSheet addSubview:yourImageView];
// Release your yourImageView
[yourImageView release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != [actionSheet cancelButtonIndex])
{
// Call back your ASIHTTPRequest
}
}
Don't forget to implement the UIActionSheetDelegate in your .h file.
Good luck.