I'm having trouble programming an AlertView to push to a new ViewController. I've followed several tutorials and still have not been able to get it function properly. Any help would be appreciated.
- (IBAction)web:(id)sender {
UIAlertView *testAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"You are now entering a website outside of the Company App: Any links or references to other Internet sites (hyperlinks) are provided by Company merely as a convenience to users of this App." delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Proceed", nil];
[testAlert show];
[testAlert release];
}
-(void)web:(UIAlertView *)web clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
}
else if (buttonIndex == 1) {
JBWebViewController *jbweb = [[JBWebViewController alloc] initWithNibName:#"JBWebViewController" bundle:nil];
[self.navigationController pushViewController:jbweb animated:YES];
}
}
You should use UIAlertView delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
not
-(void)web:(UIAlertView *)web clickedButtonAtIndex:(NSInteger)buttonIndex
and check self.navigationController is not nil.
Related
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
I have two different view controllers, one for a dashboard and one for registration. I do not want the user to be able to interact with anything on the dashboard until the user logs in through an alertview. So every time the user navigates back to the dashboard or presses cancel and they are not logged in, I want the login alert to popup.
This works perfectly in all cases, including when the user hits the back button on the navigation bar in the registration view, but does not work when the user clicks OK on the alert in the registration page.
the dashboard view contains this code:
#property(strong) UIAlertView * alert;
//...
-(void)viewWillAppear:(BOOL)animated
{
user_email = [[NSUserDefaults standardUserDefaults] stringForKey:#"email"];
if ( user_email==nil ){
[self auto_login];
} else //...
}
-(void)auto_login
{
alert = [[UIAlertView alloc] initWithTitle:#"Login" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Login",#"Forgot Password",#"Register",nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
{
self.debug.text = #"Cancel";
[self auto_login];
break;
}
//...
default:
{
self.debug.text = #"Register";
[self nav_register];
break;
}
}
}
-(void)nav_register
{
RegisterProfileController *rvc = [[RegisterProfileController alloc] init];
[self.navigationController pushViewController:rvc animated:YES];
}
The registration view controller contains this code:
-(void)catch_registration
{
NSString *response = [[NSString alloc] initWithData:self.httpdata encoding:NSASCIIStringEncoding];
if( [response isEqualToString:#"OK"] ){
UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:#"Success" message:#"..." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
successAlert.alertViewStyle = UIAlertViewStyleDefault;
[successAlert show];
}
else //...
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ([alertView.title isEqualToString:#"Success"])
[self.navigationController popViewControllerAnimated:TRUE];
}
After debugging, I know that after clickedButtonAtIndex runs in the registration view controller, [alert show] runs in the dashboard view controller, and clickedButtonAtIndex does NOT run in the dashboard view controller, but no alert shows up.
Why isn't the alert showing or how can I debug this further?
If clickedButtonAtIndex "does NOT run" as you said, then the delegate might not be set correctly. In addition, you should likely move the code from viewWillAppear: to viewDidAppear: because the view is not in the view hierarchy at that point. Your solution might be a combination of these two issues.
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.
I have a button in a menu which when touched, pops up a alert message with two buttons: "Cancel" and "Yes". This is the code I have for the alert:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Exit game"
message:#"Are you sure?"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Yes", nil];
[alert show];
Is it possible to add an action to the button "Yes"?
In your code set the UIAlertView delegate:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Exit game" message:#"Are you sure?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Yes", nil]; [alert show];
As you have set delegate to self, write the delegate function in the same class as shown below:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) { // Set buttonIndex == 0 to handel "Ok"/"Yes" button response
// Cancel button response
}}
You need to implement the UIAlertViewDelegate
and add the following...
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
// do stuff
}
}
Yes it is easy. See that argument called "delegate" that you have set to nil right now? Set that to an object... usually "self" if you are calling it from your view controller and then implement the selector for UIAlertViewDelegate.
You also need to declare that your view controller conforms to the UIAlertViewDelegate protocol. A good place to do this is in the "private" continuation class of the view controller.
#interface MyViewController() <UIAlertViewDelegate>
#end
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Button pushed: %d", buttonIndex);
}
I am trying to use a UIAction sheet to confirm a user’s action. The log prints fine... but the app hangs and shows the lightened circle in the middle like when you do a UIAlert view. I’m sure its something simple... but can’t seem to find it.
-(IBAction)showActionSheet:(id)sender
{
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:#"Are you sure?" delegate:self cancelButtonTitle:#"Cancel Button" destructiveButtonTitle:#"Reset Player" otherButtonTitles:nil];
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(#"Destructive Button Clicked");
}
else if (buttonIndex == 1) {
NSLog(#"Cancel Clicked");
}
Implement this handler instead of clickedButtonAtIndex:
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
Please release the UIActionSheet i.e where you create
[popupQuery release];