guys:
There is two buttons in my viewController of test app, the right one I call it "NO",
and the other one is "YES". The two buttons will call two different functions, and when
user press one of the buttons , I want show the user an alert to confirm that.
I know use the UIAlertViewDelegate
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
but there is two buttons, I am puzzled. How can I know which button is pressed.
So,pls help me with this, thank you in advance!
When you create an UIAlertView you can set up a tag for it
-(IBAction)yesButtonClick:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle: #"Cancel" otherButtonTitles:#"OK", nil];
alert.tag = 101;
[alert show];
}
-(IBAction)noButtonClick:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle: #"Cancel" otherButtonTitles:#"OK", nil];
alert.tag = 102;
[alert show];
}
In the delegate method check which alert is being shown
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == 101) {
// from YES button
}
else if (alertView.tag == 102) {
// from NO button
}
}
you can use the tag attribute to make the difference between your tow UIAlertView
in the function of button 1
alertView1.tag=1;
and in
-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(actionSheet.tag==1){
//first button was clicked
}
}
- (void)alertView:(UIAlertView *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex{
switch(buttonIndex){
case 0:
//YES button handler
break;
case 1:
//NO button handler
break;
default:
break;
}
}
Related
I have two textfield(name and phone#).Once user is registered, user should not able to edit phone number textfield. I am checking the condition when user click on phone number textfield. if registered I am creating alert saying that "Cannot edit". When i click on "ok" i dismiss the keypad. I am using the following code to do so
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(registered)
{
dismissAlert = [[UIAlertView alloc]initWithTitle:#"cannot edit" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[dismissAlert show];
}
}
if([title isEqualToString:#"OK"])
{
[self.view endEditing:YES];
[self.PhoneNumber resignFirstResponder];
dismissAlert = nil;
}
But textFieldShouldBeginEditing called multiple times. It happens only in iPad.
Try this -
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == txtPhone)
{
[self performSelector:#selector(phoneAlert) withObject:nil afterDelay:0.1];
return NO;
}
return YES;
}
-(void)phoneAlert
{
dismissAlert = [[UIAlertView alloc]initWithTitle:#"cannot edit" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[dismissAlert show];
}
Finally, i got it. I set
self.PhoneNumber.userInteractionEnabled = NO;
I'm trying to do an alert view that gives the user a choice of 3 options to choose from. buttonIndex 1 and 2 are fine but when i select buttonIndex 3 thing happens. I want it in a way that when buttonIndex 3 is selected and alert view with 3 options should appear and it should call a different method depending on what buttonIndex the user selects. How do i fix this code(buttonIndex==3)????
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
AddReferenceViewController *project =
[self.storyboard instantiateViewControllerWithIdentifier:#"AddRef"];
project.projectdb = self.projectdb;
[self presentViewController:project animated:YES completion:nil];
}
else if(buttonIndex == 2)
{
StyleViewController *style = [self.storyboard instantiateViewControllerWithIdentifier:#"StyleController"];
[self presentViewController:style animated:YES completion:nil];
}
else if(buttonIndex == 3)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Styles"
message:nil
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Harvard",#"Chicago", #"Vancouver",nil];
if(buttonIndex == 1){
[self Harvard];
}
else if(buttonIndex == 2){
[self Chicago];
}
else if(buttonIndex == 1){
[self Vancouver];
}
else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Information"
message:#"No Reference added to this project"
delegate: self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Add Reference",#"Ref Style", #"Email References",nil];
[alert show];
}
}
}
Button index should be 0, 1, 2 for the 3 buttons and not 1, 2, 3. This will remove the bug for you
From the documentation:
buttonIndex
The index of the button that was clicked. The button indices start at 0. If this is the cancel button index, the alert view is canceling. If -1, the cancel button index is not set.
Use a different delegate for each of your alert views.
Also, note this class is deprecated and you should use UIAlertController instead.
So for some reason after I've just added an actionsheet it's coming up and stopping above the bottom of the screen. Is there a way to manually tell this where to stop?
- (void)showActionSheet:(UIButton *)standardButton{
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel Button" destructiveButtonTitle:nil otherButtonTitles:#"Choose From Library", #"Take Photo", nil];
popupQuery.actionSheetStyle = UIActionSheetStyleAutomatic;
[popupQuery showInView:self];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
// Choose from library tapped
NSLog(#"Choose");
} else if (buttonIndex == 1) {
// Take a photo tapped
NSLog(#"Take");
}
}
Try self.view instead of self.
My app has a UILabel. I would like the user to be able to change the value of the label by pushing an "edit" button. I am able to implement a UIAlertView textfield with alert.alertViewStyle = UIAlertViewStylePlainTextInput, but I am not sure how the UILabel will receive the new value that was entered by the user.
This is what I have so far:
- (IBAction)edit
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Edit New Amount"
message:#"Enter new rate"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
UITextField *textField = [alert textFieldAtIndex:0];
textField.placeholder = #"Enter New Rate";
}
I also implemented the UIAlertViewDelegate protocol.
Assuming you want to change the label when the user presses "Ok" and have a reference to UILabel *someLabel as an ivar:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
if (buttonIndex != alertView.cancelButtonIndex) {
// UIAlertViewStylePlainTextInput will only ever have a single field at index 0
UITextField *field = [alertView textFieldAtIndex:0];
someLabel.text = field.text;
} else {
// this is where you would handle any actions for "Cancel"
}
}
I everybody,
I have 3 Buttons each one calls an AlertView with "Cancel" and "OK" and each "OK" Button goes to an different view.
for now I solved this with this
- (UIButton *)1_BTN
{
if (1_BTN == nil)
{
UIImage *buttonBackground = [UIImage imageNamed:#"1_btn.png"];
UIImage *buttonBackgroundPressed = [UIImage imageNamed:#"1_btn.png"];
CGRect frame = CGRectMake(655, 985, 107, 30);
1_BTN = [_IPadAppDelegate buttonWithTitle:#""
target:self
selector:#selector(1_BTN:)
frame:frame
image:buttonBackground
imagePressed:buttonBackgroundPressed];
[1_BTN setTag:1];
}
return 1_BTN;
}
......
- (void)1_BTN:(NSInteger *)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"some fancy text" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"ok", nil];
[alert setTag:[sender valueForKey:#"tag"]];
[alert show];
[alert release];
}
.......
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if ([[alertView tag] isEqualToNumber:[NSNumber numberWithInt:1]]) {
something should happen.....
}
for all three Buttons and it works fine but for
[alert setTag:[sender valueForKey:#"tag"]];
and
if ([[alertView tag] isEqualToNumber:[NSNumber numberWithInt:1]]) {
i get this warning "Invalid receiver Type "NSInteger""
why is that so and how can I solve this better?
For one thing, you're doing this entirely wrong. Implement the UIAlertViewDelegate, specifically alertView:clickedButtonAtIndex: and check which button index (from 0 .. n where n is the last button). Act accordingly based on your known fixed indices.
Secondly, NSInteger is a scalar non-object type, and cannot receive messages. You would want an equality comparison instead, i.e., alertview.tag == 1. But as I said previously, don't do it that way.
The UIView property "tag" isn't an object, it is just a simple NSInteger. It's almost the same as "int" you probably know from C/C++. I corrected a few line in your code. Now it should work.
Another thing: The allocation of the UIButton seems a bit strange to me. Maybe you should check your memory management.
- (UIButton *)1_BTN
{
if (1_BTN == nil) {
UIImage *buttonBackground = [UIImage imageNamed:#"1_btn.png"];
UIImage *buttonBackgroundPressed = [UIImage imageNamed:#"1_btn.png"];
CGRect frame = CGRectMake(655, 985, 107, 30);
1_BTN = [_IPadAppDelegate buttonWithTitle:#"" target:self selector:#selector(1_BTNAction:) frame:frame image:buttonBackground imagePressed:buttonBackgroundPressed];
[1_BTN setTag:1];
}
return 1_BTN;
}
- (void)1_BTNAction:(UIButton *)sender {
NSInteger tagNumber = [sender tag];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"sone fancy text" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"ok", nil];
[alert setTag:tagNumber];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if ([alertView tag] == 1) {
//something should happen
}
}
}