UiAlertView should change ViewController - ios

What is wrong with following code? I will change the viewController, when the text in the alertView is not empty. The problem is, it will change the UiViewController every time, even it is empty.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
if ([[alertView textFieldAtIndex:0].text isEqual:#""]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Error warning" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"ChangeView"];
[self presentViewController:vc animated:YES completion:nil];
}
}
}

You should do this check instead:
if (![[alertView textFieldAtIndex:0].text.length) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Error warning" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
} else ...

Related

Opening a new view controller after alert

enter code hereI am having trouble with going back to the previous view controller when alert is presented.
What I am trying to do is have the user enter in data, then an alert appear saying it was successful, then return to the previous view controller.
I currently have no code doing so and am seeking assistance with what I should put in.
- (IBAction)saveLabel:(id)sender
{
NSArray *data = [[NSUserDefaults standardUserDefaults] objectForKey:#"DATA"];
NSMutableArray *currentDataArray;
if (data == nil)
{
currentDataArray = [[NSMutableArray alloc]init];
}
else
{
currentDataArray = [[NSMutableArray alloc]initWithArray:data];
}
[currentDataArray addObject:self.textField.text];
[[NSUserDefaults standardUserDefaults] setObject:currentDataArray forKey:#"DATA"];
}
- (IBAction)enterButtonPressed:(id)sender
{
NSLog(#"enterButtonPressed");
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[enterAlert show];
}
//If u r using dismissing
[self dismissViewControllerAnimated:YES completion:^{
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[enterAlert show];
}];
//if u r using navigation ,popViewController
[CATransaction begin];
[CATransaction setCompletionBlock:^{
// handle completion here
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[enterAlert show];
}];
[self.navigationController popViewControllerAnimated:YES];
[CATransaction commit];
Set Delegate self of your UIAlertView
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
[enterAlert show];
Use Delegate Method of UIAlertviewController.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0){
// Do your Stuff Here....
[self.navigationController popViewControllerAnimated:TRUE];
}
}
Add the following UIAlertViewDelegate method to your implementation file:
- (void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger)buttonIndex {
// If you are presenting this view controller
[self dismissViewControllerAnimated:YES completion:nil];
// If you are pushing this view controller
[self.navigationController popViewControllerAnimated:YES];
}
Also remember to set your UIAlertView delegate to your view controller, change to the following code:
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:#"Entry was recorded" delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil, nil];
enterAlert.tag=100;
[enterAlert show];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (alertView.tag == 100) {
if (buttonIndex == 0) {
// Do something when ok pressed
// If you are presenting this view controller
[self dismissViewControllerAnimated:YES completion:nil];
// If you are pushing this view controller
[self.navigationController popViewControllerAnimated:YES];
} else {
// Do something for other alertviewButton}
else{// Do something with responses from other alertViews by giving tags
}

Pushing a view controller from an CFCoverFlowView

Hi Im developing an app in which I'm using a third party library called CFCoverFlowView (Can't link It but if you just Google CFCoverFlowView you will find the GitHub site).
What I want to accomplish Is to be able to push septet ViewControllers when the different images is clicked. Im using Storyboard so I would like to use the
instantiateViewControllerWithIdentifier:#""
In the library you get the following line to use when an image is clicked:
- (void)coverFlowView:(CFCoverFlowView *)coverFlowView didSelectPageItemAtIndex:(NSInteger)index;
And i have done this to make sure that it works with UIAlertView:
- (void)coverFlowView:(CFCoverFlowView *)coverFlowView didSelectPageItemAtIndex:(NSInteger)index;
{
if (index == 0)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"0" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
if (index == 1)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"1" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
if (index == 2)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"2" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
if (index == 3)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"3" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
if (index == 4)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"4" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
And that workes, when i click on the separate images i get this (Since this Is my first question here I can't post images or more than 2 links because of my low reputation.)
Image 1 http://i60.tinypic.com/k1e4wz.png
Image 2 http://i59.tinypic.com/1g6lif.png
What I want to do Is when an image is clicked a ViewController with a StoryboardID gets pushed.
When i tried this the app crashes:
- (void)coverFlowView:(CFCoverFlowView *)coverFlowView didSelectPageItemAtIndex:(NSInteger)index;
{
if (index == 0)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main.storyboard" bundle:nil];
UIViewController *ViewController1 = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"ViewController1"];
[self.navigationController pushViewController:ViewController1 animated:YES];
}
}
So if anyone could help me It would very appreciated and If anything Is missing or you don't understand my code comment and i will edit the text.
Assumption : You're getting this error;
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Main.storyboard'
Solution :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
Is that a typo or are you not pushing the instance of the ViewController you instantiated? You need to push ViewController1:
[self.navigationController pushViewController:ViewController1 animated:YES];

Using UIAlertView for a GetReady title

I am trying to use UIAlertView to pull up a get ready title then pull it off the screen to let the player know the game is starting. How do I do this using UIAlertView?
Try this Methods...
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"Get Ready!" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];;
[alertView show];
[self performSelector:#selector(dismissReadyAlert:) withObject:alertView afterDelay:3.0];
-(void)dismissReadyAlert:(UIAlertView *)alertView
{
[alertView dismissWithClickedButtonIndex:0 animated:NO];
alertView.title = #"Your game is started!";
[alertView show];
[self performSelector:#selector(dismissStartAlert:) withObject:alertView afterDelay:2.0];
}
-(void)dismissStartAlert:(UIAlertView *)alertView
{
[alertView dismissWithClickedButtonIndex:0 animated:YES];
}
Thanks!
You need to keep a reference to the alert. Then just call this method:
[alert dismissWithClickedButtonIndex:0 animated:YES];
Declare UIAlertView Object in header file.
UIAlertView *alertReady;
Then Display alertview whenever you like to display and call one method to dismiss alertview
alertReady = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Get Ready" delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
[alertReady show];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(hideAlertView) userInfo:nil repeats:NO];
-(void)hideAlertView
{
[alertReady dismissWithClickedButtonIndex:0 animated:YES];
}

Using buttonIndex with multiple alert views

I have about 4 alert views with different criteria when they appear. In all 4 views, the right button should always do the same thing.
I use the code below to try and say IF the buttonIndex == 1, do something.
Currently, It only works in one of my alert views. The others just end up closing the alert view and never running the code for IF buttonIndex == 1.
Any ideas would be appreciated.
if (a==1) {
NSString *message = [[NSString alloc] initWithFormat:
#"Only $%#!",dollas.text];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
#"Really?!"
message:message
delegate:self
cancelButtonTitle:#"Close"
otherButtonTitles:#"Facebook",nil];
[alert show];
[alert release];
[message release];
}
else if (a==2) {
NSString *message = [[NSString alloc] initWithFormat:
#"Somone just paid you $%#", dollas.text];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
#"Swish!"
message:message
delegate:nil
cancelButtonTitle:#"Close"
otherButtonTitles:#"Facebook",nil];
[alert show];
[alert release];
[message release];
}
And the delegate:
- (void)alertView:(UIAlertView *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1)
{
do.stuff;
}
You should be setting the delegate to self so that method gets called.
IE -
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
#"Really?!"
message:message
delegate:self //SELF
cancelButtonTitle:#"Close"
otherButtonTitles:#"Facebook",nil];
Set the tags on each alertview and inside -didDismissWithButtonIndex check first for the alerts tag
eg:
if (a==1) {
NSString *message = [[NSString alloc] initWithFormat:
#"Only $%#!",dollas.text];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
#"Really?!"
message:message
delegate:self
cancelButtonTitle:#"Close"
otherButtonTitles:#"Facebook",nil];
alert.tag = 1;
[alert show];
[alert release];
[message release];
}
else if (a==2) {
NSString *message = [[NSString alloc] initWithFormat:
#"Somone just paid you $%#", dollas.text];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
#"Swish!"
message:message
delegate:nil
cancelButtonTitle:#"Close"
otherButtonTitles:#"Facebook",nil];
alert.tag = 2;
[alert show];
[alert release];
[message release];
}
then in -didDismissWithButtonIndex
- (void)alertView:(UIAlertView *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1 && actionSheet.tag == 1)
{
do.stuff;
}
else if (buttonIndex == 1 && actionSheet.tag == 2)
{
do.otherStuff;
}
For the case (a == 2) you set the UIAlertView delegate to nil, so - (void)alertView:(UIAlertView *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex shouldn't even be getting called for this case. Change it to set the delegate to self.

UIAlertVIew in UIAlertView

-(IBAction) secondTwoSlotSettings: (id)sender{
UIAlertView *secondTwoSlotSettings = [[UIAlertView alloc] initWithTitle:#""
message:#""
delegate:self
cancelButtonTitle:#"キャンセル"
otherButtonTitles:#"2スロット カメラ",#"2スロット カメラロール", nil];
[secondTwoSlotSettings show];
}
-(IBAction) firstTwoSlotSettings: (id)sender{
UIAlertView *firstTwoSlotSettingsMessage = [[UIAlertView alloc] initWithTitle:#""
message:#""
delegate:self
cancelButtonTitle:#"キャンセル"
otherButtonTitles:#"1スロット",#"2スロット", nil];
[firstTwoSlotSettingsMessage show];
}
-(IBAction) oneSlotSettings: (id)sender{
UIAlertView *oneSlotSettingsMessage = [[UIAlertView alloc] initWithTitle:#""
message:#""
delegate:self
cancelButtonTitle:#"キャンセル"
otherButtonTitles:#"カメラ",#"カメラロール", nil];
[oneSlotSettingsMessage show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"キャンセル"])
{
NSLog(#"キャンセル was selected.");
}
else if([title isEqualToString:#"はい"])
{
NSLog(#"はい was selected.");
}
else if([title isEqualToString:#"2スロット カメラ"])
{
NSLog(#"2スロット カメラ was selected.");
}
else if([title isEqualToString:#"1スロット"])
{
NSLog(#"1スロット was selected.");
}
else if([title isEqualToString:#"2スロット"])
{
NSLog(#"2スロット was selected.");
UIAlertView *oneSlotSettingsMessage = [[UIAlertView alloc] initWithTitle:#""
message:#""
delegate:self
cancelButtonTitle:#"キャンセル"
otherButtonTitles:#"2スロット カメラ",#"2スロット カメラロール", nil];
[oneSlotSettingsMessage show];
}
else if([title isEqualToString:#"カメラ"])
{
NSLog(#"カメラ was selected.");
}
else if ([title isEqualToString:#"カメラロール"])
{
NSLog(#"カメラロール was selected.");
}
Here is my code:
I was only able to show the firstTwoSlotSettings and oneSlotSettings. but when I try to show the secondTwoSlotSettings it vanish.
No It is not possible with Apple's UIAlertView. When you present an alertView ,it will come modal to all other views and dismiss any other Alerts already present in the screen.
One thing you can do is to create a custom View as your alert and animates its present/dismiss action? Then you have the control to do whatever you want..

Resources