Edit Text in Pop Up Box - ios

I need some kind of easy way to be able to edit a title when a button is pressed. I would basically just redirect to a view controller with the edit possibility included but that just seems to "heavy". It would be far better to just be able to edit text in a pop up box or something. Is this possible and if so how would I do this?

You can use an UIAlertview control to update your title.
In your button click put the following code.For example
-(IBAction)ButtonClick:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
[alert release];
}
In the UIAlertViewDelegate method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == alertView.cancelButtonIndex)
NSLog(#"%#", [alertView textFieldAtIndex:0].text);
}
I hope this will help you.
Please note the alert.alertViewStyle = UIAlertViewStylePlainTextInput only works in iOS 5.0 and later.

Related

Integrate with Chartboost objective-c

I've successfully integrated Chartboost rewarded video, it works but i want to implement the code that will increase credits when a user has completely watched a rewarded video. I've tried to implement in app delegate and it works calling it but due to some internal functions i will need to implement in specific view controller. so when i put the same code
-(void)didCompleteRewardedVideo:(CBLocation)location
withReward:(int)reward {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Really reset?" message:#"hi ?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
// optional - add more buttons:
[alert addButtonWithTitle:#"Yes"];
[alert show];
}
the alert view will not appear if i implement this code in specific view controller instead of appdelegate. i assume it is not called in view controller since it has no response. i need help to implement in view controller
In order to make UI changes, you should call it on main queue.
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Really reset?" message:#"hi ?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
// optional - add more buttons:
[alert addButtonWithTitle:#"Yes"];
[alert show];
});
Or you can just pass the delegate of the view and show the alert on it.
-(void) DisplayMessage:(NSString*)message withDelegate:(id)delegate
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Title"
message:message delegate:delegate
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[alert show];
}

Dismiss alert view when performing delegate method

hi there when i run my delegate method which is parsing json data the alert view appears to freeze whilst it is performing the method is there anyway to hide the alert view whilst the app is running the code I've tried
- (IBAction)btnAdd:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Add Source" message:#"Enter the web address of the json data" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Add", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert setTag:0];
[alert dismissWithClickedButtonIndex:-1 animated:YES];
[alert show];
}
this doesn't actually do anything.
any advice?
*UPDATE
in the delegate method i get the same result
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == 0) {
if (buttonIndex == 1) {
NSString *textEnteredraw = [[alertView textFieldAtIndex:0] text];
[alertView dismissWithClickedButtonIndex:-1 animated:YES];
From method
- (IBAction)btnAdd:(id)sender
Remove
[alert dismissWithClickedButtonIndex:-1 animated:YES];
Because as #Dima said, you are dismissing the alertView before you are even showing it.
You call the code to hide the alert before you even show it. This method is meant to be called after the alert is shown.

Show Next Screen Only After User Closes Alert

I'm working on a losing screen and I'd like to let users view their game before showing the stats screen. I've decided to use an alert to show users the current screen. After they hit OK, they should be taken to the stats screen.
The problem is, the stat screen pops up at the same time as the alert. How can I make sure the stats screen opens after the user closes the alert?
if ([self.model userHasLost]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:#"You lost..." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[self _showGameEndScreenWitnWin:NO]; //Stats screen should open after alert is closed
}
You need to use delegate method of UIAlertView and also need to set delegate of UIAlertView = self,
Following is delegate method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
check buttonIndex in this method it's give you NSInteger and manage click of button.
For more information read this UIAlertView Delegates.
You need to set the alert view delegate. In this example self will conform to the UIAlertViewDelegate protocol.
if ([self.model userHasLost]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:#"You lost..." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.delegate = self;
[alert show];
}
You can then listen to a delegate method for when the alert view is dismissed :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[self _showGameEndScreenWitnWin:NO]; //Stats screen should open after alert is closed
}
For this situation need to use delegate method,
For simple Example Refer Here: http://www.idev101.com/code/User_Interface/UIAlertView.html

How to create a pop-up when exiting a view?

So I'm making a photo editing app for school from last semesters project. When a user clicks the back button (in editor mode) which in my case is called "Product Selection" I'd like to have a pop-up come up and say "Do you want to delete everything and go back to product selection?" to see if they want to discard their work, and if the user selects yes, the entire project is discarded and they are put back in the lobby.
Where do I put this code to do this? I found the "Product Selection" button in the min story board but not sure what to do from there.
The code for the pop-up I was going to use is:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification"
message:#"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes"];
[alert show];
[alert release]
Any help/wisdom would be extremely appreciated!
I would create a "Cancel" UIBarButton which executes a custom function:
- (void)cancelTapped {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes"];
[alert setTag:1];
[alert show];
}
And listens for the alert to return on:
- alertView:didDismissWithButtonIndex:
From there, I would write the logic to hide the page and pop the view.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (alertView.tag == 1 && buttonIndex == 1) {
// Delete data and return to lobby
[self.navigationController popViewControllerAnimated:YES];
}
}
Use this delegate method (don't forget adding UIAlertViewDelegate on your .h):
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes"];
alert.cancelButtonIndex = 0;
[alert show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
// If user confirmed:
if (buttonIndex != 0) {
// Do what you need.
}
}

UIAlertView Login and Password Input with three buttons

I just found a strange problem, when I try to use three buttons in an UIAlertView, the view is displayed weirdly, as you can see in this screenshot :
Here is the code :
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Login" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Login",#"Forget Password", nil];
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
UITextField *place=[alert textFieldAtIndex:0];
place.placeholder=#"Email";
[alert show];
[alert release];
Please suggest me how I can correct this. Thanks.

Resources