EXTRA INFO: I must have asked wrong, I'd like the user to click the button and be redirected to Facebook!
I need to add a visit facebook button called "facebook" as swell in the code below! Right now I just have an ok button.
Also if possible could you help me understand, how I will be able to retrieve user information - e.g I'll ask them to add there email in the textfield, when they press ok then where will I store the email and obtain it, how does that work, and what do I need to read to find out?
Try to add your buttons in otherButtonTitles
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Refresh"
message:#"Are you want to Refresh Data"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Button1",#"Button2",#"Button3",
nil];
As per Duncan's answer you can use delegate and get which button is clicked.so on specific button's tap you can redirect to facebook page using below code.
UIAlertView *info = [[UIAlertView alloc]initWithTitle:#"Yup" message:#"You've won! Like Us on Facebook too" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Facebook", nil];
info.tag = 10;
[info show];
So when the user presses the Facebook button the delegate method alertView:clickedButtonAtIndex will be called so at that time check alert.tag and then check if facebook button is tapped then show another alert.
Your Delegate Method should be
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 10)
{
switch (buttonIndex) {
case 0:
NSLog(#"Cancel");
break;
case 1:
NSLog(#"Facebook");
[self showAlertForRedirect];
break;
default:
break;
}
}
else if (alertView.tag == 20)
{
switch (buttonIndex) {
case 0:
NSLog(#"Cancel");
break;
case 1:
NSLog(#"OK");
[self RedirectNow];
break;
default:
break;
}
}
}
-(void)showAlertForRedirect
{
UIAlertView *info2 = [[UIAlertView alloc]initWithTitle:#"Note" message:#"Would you like to redirect on facebook?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
info2.tag = 20;
[info2 show];
}
-(void)RedirectNow
{
NSURL *fanPageURL = [NSURL URLWithString:#"fb://profile/yourid"];
if (![[UIApplication sharedApplication] openURL: fanPageURL])
{
NSURL *webURL = [NSURL URLWithString:#"https://www.facebook.com/yourpagename"];
[[UIApplication sharedApplication] openURL: webURL];
}
}
As the other poster said, you can add additional buttons by providing button titles in the otherButtonTitles parameter to the initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles: method.
You can set up your alert to have an input field by setting the alertViewStyle property of the button returned by the above method to UIAlertViewStylePlainTextInput
You need to pass in self (your view controller) as the delegate parameter.
Then you need to implement the method alertView:didDismissWithButtonIndex:
In that method, you will get the button index the user selected. You can use the textFieldAtIndex: method to get a pointer to the text field, and get the user-entered text.
Related
I'm trying to deny a certain tabBarItem from pulling its action if there is no internet connection.
This is my code:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
ONLog(#"tab selected: %#", item.title);
if (item.tag == 2) {
if (![[InternetManager sharedManager] isInternetWorking]) {
[self setSelectedIndex:1];
UIAlertView *noInternet = [[UIAlertView alloc] initWithTitle:#"No Internet Connection" message:nil delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[noInternet show];
}
}
}
Issue is: The alert view is being called but the "setSelectedIndex" is not doing its work...
Any ideas?
Check what the value of self.selectedItem is when the method is first called. Maybe the selected item isn't actually set as selected until after this method is called, so [self setSelectedIndex:1] is being called but the item that the user tapped gets selected immediately after.
I have an IF statement that checks labels to see if a label is empty, if it is show an alert.
if ([_DOBDate.text length] == 0)
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Cannot Proceed"
message:#"Please Submit your DOB and Gender"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
later on in the function I have perform segue, like this :
[self performSegueWithIdentifier:#"SetUptoMain" sender:self];
True, the alert does fire when the label is blank, but it also performs the segue. This perform segue line is not in the IF statement. So I would have thought it would have ran the IF statement and stayed there till I pressed OK. OK would be staying on the same view controller.
The segue is performed, is this due to Blocks ? any advice ?
So if the USER pressed Ok from the UIAlert the VC does not move, it stays where it was so the user can enter the details required.
This is my code :
- (IBAction)SettingsSave:(id)sender {
if ([_DOBDate.text length] == 0)
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Cannot Proceed"
message:#"Please Submit your DOB and Gender"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
more code...
then at the end
[self performSegueWithIdentifier:#"SetUptoMain" sender:self];
}
thanks
Seems to be an if/else logic issue. Read about it.
-(IBAction)yourAction:(id)sender
{
if ([_DOBDate.text length] == 0)
{
//Show your AlertView as you did
}
else
{
//put the rest of your code, including the performSegue
}
}
UIAlertView doesn't block execution. It runs asynchronously. Therefore the code path will go into your if statement, then continue past it.
If you only want the segue to be performed after the user presses the alert view button then you need to implement UIAlertViewDelegate.
In your header file add something like this:
#interface MyController : UIViewController <UIAlertViewDelegate>
When you create the alert view do it like this:
UIAlertView *message = [[UIAlertView alloc] initWithTitle: #"Cannot Proceed"
message: #"Please Submit your DOB and Gender"
delegate: self
cancelButtonTitle: #"OK"
otherButtonTitles: nil];
[message show];
And add this method to implement the UIAlertViewDelegate.
- (void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) buttonIndex {
// perform segue here. You can also check what button was pressed based on the button index.
}
I have a UIAlertView with UIAlertViewStyleSecureTextInput. When the user taps "Login" and the password is correct, it will push the next ViewController. If the password is incorrect, it displays another UIAlertView prompting a single "Dismiss" button. What I am trying to do is when the user taps the return key, the "Login" button will be triggered. As it sits now, when the return key is pressed the alert just dismisses, regardless if the password is correct or not. Maybe there is a more logical solution than what I have attempted? Sorry if the title is a little confusing, I don't know how else to explain it. Any help is greatly appreciated. Thanks.
I have declared the first alert in my .h file, and conformed to the UIAlertViewDelegate and UITextFieldDelegate:
#interface EndViewController : UIViewController <UIAlertViewDelegate, UITextFieldDelegate>
#property (retain, strong) UIAlertView *loginRequiredMsg;
Method for the "Login" alert:
- (IBAction)resultsBtnPressed:(UIButton *)sender {
self.loginRequiredMsg = [[UIAlertView alloc]initWithTitle:#"Login Required"
message:#"Please enter the admin password"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Login", nil];
self.loginRequiredMsg.alertViewStyle = UIAlertViewStyleSecureTextInput;
[self.loginRequiredMsg textFieldAtIndex:0].delegate = self;
[self.loginRequiredMsg show];
}
Method for dismissing the keyboard when return key is tapped (I think I need to somehow call the next alert here if the password is incorrect, as currently it just dismisses the alert):
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Dismiss keyboard when return key is pressed
[self.loginRequiredMsg dismissWithClickedButtonIndex:self.loginRequiredMsg.firstOtherButtonIndex animated:YES];
return YES;
}
And finally the method containing the outcome of the password entered:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Login"]) {
UITextField *password = [alertView textFieldAtIndex:0];
// Basic login authentication
if ([password.text isEqualToString:#"admin"]) {
// Allocate & initialise ViewController
ResultsViewController *Results = [[ResultsViewController alloc]initWithNibName:#"ResultsViewController" bundle:nil];
// Push next ViewController
[self.navigationController pushViewController:Results animated:YES];
NSLog(#"Show results");
} else {
UIAlertView *errorMsg = [[UIAlertView alloc]initWithTitle:#"Error"
message:#"Admin password is incorrect. Please try again."
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[errorMsg show];
}
}
}
As discussed in the comments, you need to implement alertView:didDismissWithButtonIndex: which tells you when the alert view has been dismissed. When you tap the return key, it calls that method with the index of the OK button.
I just started iOS development, and now I'm stuck. I have a table cell, where the user cann see data. Next to it, there are two buttons. One to delete, one to edit. The edit part is where I'm stuck. I have a label field. When the user clicks on the edit button a prompt should come where the user can type in data. I already found something to do it. But there's no save button and I don't know how I could then save the variable to an NSString
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Name der neuen Liste" message:#"" delegate:self cancelButtonTitle:#"Abbrechen" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
I currently have this. lNow a prompt pops up, where I can write something, but not save. I just can press cancel afterwards. How do I work with user input in iOS? I don't want to use the label as an UITextField since it has to be clickable to perform another action.
Try this:
- (void)noteInput
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"input"
message:#"input"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
UITextField *noteText = [alertView textFieldAtIndex:0];
NSString *note = noteText.text;
}
}
Try adding a Save button in otherButtonTitles. Then, in your UIAlertViewDelegate:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
// Save button pressed
}
}
I am displaying a UIAlertView to the user with an "OK" button on it, and when the user presses the button I would like my delegate method to perform an action. However currently when the user presses the "OK" button the application crashes with this error:
Thread 1: EXC_BAD_ACCESS (code=1, address=0xb)
Here is my code, the alert view shows fine with the button etc, however as soon as the button is pressed this is the error I am getting. It's almost like a breakpoint but if I click the forward button nothing happens.
//.h
#interface ErrorHandling : NSObject <UIAlertViewDelegate> {
//.m
#define myAlertViewsTag 0
- (void)recivedErrorCode:(int)errorCode MethodName:(NSString *)methodName {
switch (errorCode) {
case 1: {
NSLog(#"ERROR = 1");
message = [[UIAlertView alloc] initWithTitle:#"Error 1:"
message:#"The supplied registration code does exist."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[SVProgressHUD dismiss];
[message show];
}
break;
case 2: {
NSLog(#"ERROR = 2");
message = [[UIAlertView alloc] initWithTitle:#"Error 2:"
message:#"Your registration is no longer valid."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
message.tag = myAlertViewsTag;
[SVProgressHUD dismiss];
[message show];
}
break;
default:
break;
}
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (message.tag == myAlertViewsTag) {
if (buttonIndex == 0) {
// Do something when ok pressed
NSLog(#"DONE 1");
} else {
// Do something for ok
NSLog(#"DONE 2");
}
} else {
// Do something with responses from other alertViews
NSLog(#"DONE 3");
}
}
update:
This is how I call the class the code above is in from my conneciton class.
// Do whatever you need to with the error number
ErrorHandling *errorHandling = [[ErrorHandling alloc] init];
[errorHandling recivedErrorCode:errorRecivedFromServerInt MethodName:methodName];
The problem is with the lifetime of your ErrorHandling instance. You create an instance and then call the recivedErrorCode: method. After that there are no other strong references to your errorHandling instance so it gets deallocated.
Meanwhile the alert view has made the instance its delegate. When you tap the button on the alert view, the alert view tries to contact its delegate. But the delegate has been deallocated and now points to garbage memory resulting in the crash.
The solution is to keep a longer lasting strong reference to the ErrorHandling instance. At least until after the alert view is dismissed.
BTW - Your method name has a typo - it should be receivedErrorCode:.
It is not at all clear from this code if this line:
if (message.tag == myAlertViewsTag)...
Actually points to anything valid. Is the message object still around even... I think you problems lies there, somewhere. Post more code that helps us understand this if you want more help.
In alertView:didDismissWithButtonIndex: you are checking the alert view's tag by using "message.tag".
It looks like you were intending for message to be an ivar in your class. A better way that should also solve your bad_access would be to use the alert view that is passed into the didDismissWithButtonIndex, rather than retaining the UIAlertView in your class. This code should do the trick:
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (alertView.tag == myAlertViewsTag) {
if (buttonIndex == 0) {
// Do something when ok pressed
NSLog(#"DONE 1");
} else {
// Do something for ok
NSLog(#"DONE 2");
}
} else {
// Do something with responses from other alertViews
NSLog(#"DONE 3");
}
}