iOS Dev: Segue by pressing a button in an Alert? - ios

I've been searching for a few hours and haven't found any answers, so I would really appreciate your help!
I am designing an app and need to segue to a different view controller when a button in an alert is pressed. I already have the alert set up, but just need the code to segue to a new view.

Try this. create alertview first.
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Message" message:#"Open New controller?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes",nil];
[alertView show];
[alertView release];
Implement the AlertView Delegate Method
#pragma mark AlertView Delegate
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex)
{
Viewcontroller *vc = [[UIViewcontroller alloc] initWithNib:#"Viewcontroller"];
[self presentModalViewController:vc];
[vc release];
}
}

Implement the UIAlertViewDelegate and add the method alertView:clickedButtonAtIndex:. Once the right button is clicked, call the method to segue into the new view.

Implement UIAlertViewDelegate and then you get a callback with which button was pressed when the alert was dismissed. Simply call performSegueWithIdentifier in there.
More details at the UIAlertViewDelegate protocol reference

Related

Make UIAlertView stay

I have a UIAlertView that I implemented in viewDidLoad. I'm trying to make the alertView stay when the otherButton (buttonAtIndex:1) was selected. Here is my code:
UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message:"
delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Done", nil];
[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
[dialog show];
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) return;
[alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
When the second button was selected ("Done"), the alertView goes away. How can I make it stay?
You should create your own alert view class that is NOT a subclass of UIAlertView. UIAlertView's documentation, it says under 'Subclassing notes:
The UIAlertView class is intended to be used as-is and does not support subclassing. (...)
Above referenced in UIAlertView Apple Documentation section marked Subclassing Notes
You might have what you want here :
Subclass UIAlertView and then overload
-dismissWithClickedButtonIndex:animated:, e.g.
#implementation MyAlertView
-(void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
if (buttonIndex should not dismiss the alert)
return;
[super dismissWithClickedButtonIndex:buttonIndex animated:animated];
}
#end

Push view programmatically from UIAlertView button?

I would open a UIViewController from a UIAlertView button but it doesn't work.
When user terminate a game I show a UIAlertView with some text and two buttons: "Ok" to dismiss alert and "Score" to open the score page.
This is the actual UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"YOU WIN!"
message:[NSString stringWithFormat:#"You win a game in %d seconds!",seconds]
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:#"Score", nil];
[alert show];
and this is the code for push the view:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Score"]){
ScoreViewController *score = [[ScoreViewController alloc]init];
[[self navigationController] pushViewController:score animated:YES];
}
}
What I obtain is a completely black screen. Can someone help me? I can't figure it out what I'm doing wrong.
PS: ScoreViewController has a segue from the root view but of course I can't create a new one from storyboard because I want to perform segue programmatically, from the alert view button.
Hope I've been clear, any help will be very appreciate!
It looks like you're instantiating an instance of ScoreViewController and pushing onto your navigation controller. While this is a legitimate way of presenting another controller, it's different from performing a Storyboard segue.
First, obviously, you'll have to make sure you have a segue connecting your view controllers in the Storyboard. It sounds like you've made it that far, so next you'll want to make sure the segue has an identifier. Select the segue and set its identifier in the Attributes inspector. Then perform the segue in code:
[self performSegueWithIdentifier:#"YourIdentifier" sender:self];

UIAlertView crash issue on another view controller

I have 2 view controllers, ViewController 1(VC1) and 2(VC2). In VC2 I have a back and done button. On clicking back button it goes directly to VC1 and on done it makes an api call and when it gets a response it shows an alert view and clicking ok goes back to VC1. Now when I make a api call a loading bar shows up and disappears when I get response and shows the AlertView. But if during that fraction of second when the loading disappears and AlertView is going to be popped up if I click on back and the view changes to VC1, the alert appears on VC1 and results in a crash.
This is a rare case as no user will purposely try for it but I was wondering if that crash can be managed without disabling the back button. I think there can be other instance such cases like if we are making an asynchronous calls and if the user is allowed to use UI while waiting for response and if any error alert that was suppose to show on one ViewController shows up in another may result in crash since the delegate that alert is referring to is that of the previous view controller. So is there any way to handle this kind of crash efficiently?
//Alert View sample
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[[message objectAtIndex:1] capitalizedString] message:[message objectAtIndex:0] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] ;
[alert setTag:701];
[alert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ([alertView tag] == 701)
if (buttonIndex == 0)
{
[self.navigationController popViewControllerAnimated:YES];
}
}
The proper way to fix this problem is to use an instance variable to keep a reference to the alert view.
This instance variable should be set to nil in the alertView:didDismissWithButtonIndex: delegate method.
In the view controller's dealloc method, you call dismissWithClickedButtonIndex:animated: if the instance variable is still set.
Assume _alertView is the instance variable.
Create the alert:
_alertView = [[UIAlertView alloc] initWithTitle:[[message objectAtIndex:1] capitalizedString] message:[message objectAtIndex:0] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] ;
[_alertView setTag:701];
[_alertView show];
Update your existing alertView:clickedButtonAtIndex: method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 701) {
_alertView.delegate = nil;
_alertView = nil;
if (buttonIndex == 0) {
[self.navigationController popViewControllerAnimated:YES];
}
}
}
Add:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
_alertView = nil;
}
Add:
- (void)dealloc {
if (_alertView) {
_alertView.delegate = nil;
[_alertView dismissWithClickedButtonIndex:_alertView.cancelButtonIndex animated:NO];
_alertView = nil;
}
}

How to have a UIBarButton execute a custom function?

I'm hoping someone can help me figure this out...I'm a beginner Xcode / Objective-C programmer. I'm working on a app that is a continuation of last semester.
1: So I created a button and I need it to execute this 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];
}
How/where do I put this function? Is it in the button properties? Or would I write this in a custom class/controller and link it to it?
2: How do I get it to listen for the alert to return on: - alertView:didDismissWithButtonIndex:
3: From there, how would 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];
}
}
You will need a custom UIViewController to house the logic for your button and alert view interactions. I am assuming you know how to do that.
Once done, assuming you have a reference to your button property in your view controller, you can programatically add a target to your button and pass the selector cancelTapped as a parameter:
[myButton addTarget:self action:#selector(cancelTapped) forControlEvents:UIControlEventTouchUpInside];
Alternatively you could control-drag from the button in your Storyboard to the header file of your custom UIViewController, and define an IBAction. That will create an empty cancelTapped method in your implementation which you could then add your logic in.
As for listening on UIAlertView messages, you will need to make your custom UIViewController a delegate of the UIAlertView by passing "self" as the delegate in the following statement:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notification" message:#"My Message" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes"];
Your CustomViewController should also be declared as a UIAlertViewDelegate.
CustomViewController.h
#interface CustomViewController : UIViewController<UIAlertViewDelegate>
#end
Hope this helps!
By the using VIewWillDisappear method to detect the press of The back button of NavigationItem:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// Navigation button was pressed. Do some stuff
[self cancelTapped];
}
[super viewWillDisappear:animated];
}
- (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];
}
For More info && also Custom UIBArButtonItem Check Here

How to view another AlertView after dismiss?

can i display another UIAlertview after dismissed by timer? i use this method UIActivityindicatorview into UIAlertView with NSTimer to dismiss it self "please wait, Saving..." dismissed, then i want to view another UIAlertView say "Saved Successfully!" (Done). how to achieve this? thank you.
here i was use same this method but this not what i'm need. i just need view another UIAlertview after the first uialertview dismissed by timer.
please tell me if need more clear i can post my code here thanks.
- (IBAction)showAlert:(id)sender {
UIAlertView* alert_view = [[UIAlertView alloc]
initWithTitle: #"Save" message:#"please wait, saving..." delegate: self
cancelButtonTitle:#"done" otherButtonTitles:nil];
[alert_view show];
[alert_view release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0) {
UIAlertView* alert_view = [[UIAlertView alloc]
initWithTitle: #"Success" message:#"Saved Successfully!" delegate: self
cancelButtonTitle: #"Done" otherButtonTitles:nil];
[alertView dismissWithClickedButtonIndex:0 animated:TRUE];
}
else{
[alertView dismissWithClickedButtonIndex:1 animated:TRUE];
}
}
A timer is not the way to go about this. You are faking it instead of implementing it properly. You need to use protocols and delegates if you have multiple classes involved, but ultimately, you need to follow this flow:
1) Show a "Please wait...Saving" alert
2) Do the saving
3) When saving has completed, dismiss first alert, and show second.
Depending on HOW your are saving WHAT, this may need some extra logic to implement, but without more specifics, it's hard to help much more.

Resources