AlertView ClickedButtonAtIndex not working - ios

I've read the related posts but must be missing something since my ClickedButtonAtIndex method is not being called after hitting the OK button on my alert. I set a breakpoint within the ClickedButtonAtIndex method, it is never reached and the NSLog is never executed.
Here's the .h code:
#import <UIKit/UIKit.h>
#interface MainPage : UIViewController <UIAlertViewDelegate> {
IBOutlet UILabel *sendtxtlabel;
}
- (IBAction)button:(id)sender;
#end
Here is the .m code here:
#interface MainPage ()
#end
#implementation MainPage
- (void)viewDidLoad {
[super viewDidLoad];
sendtxtlabel.text= [NSString stringWithFormat:#"Number 800-555-1212"];
// Do any additional setup after loading the view.
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Vehicle Owner"
message:#"Enter Phone Number"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
alert.tag =12;
//[alert addButtonWithTitle:#"ok"];
[alert show];
NSLog(#"ok");
}
- (void) alertView: (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Capture the phone number input from the alert pop-up window. UIAlertView Delegate added to allow the OS trigger this method to read the data.
if (alertView.tag == 12) {
if (buttonIndex == 1) {
UITextField *textfield = [alertView textFieldAtIndex:0];
NSLog(#"phonenumber: %#", textfield.text);
}
}
}

Related

Erratic dismissal of software keyboard

Expected behaviour:
user clicks inside TextField1, the keyboard pops up, user enters a value, NextButton is pressed, keyboard should be dismissed.
Anomaly: the keyboard gets dismissed upon pressing NextButton, however it then pops up again after the alerts that follow are dismissed! Why?
On the other hand, if the alert is never called (//[self showDisclaimer]) the keyboard gets dismissed correctly...
I know that alertView is deprecated, but this is not the source of the error, because I get exactly the same behaviour if I use UIAlertController instead.
Can someone shed some light on this?
- (IBAction) NextButton: (id) sender
{
[self backgroundTouch:id]; //Dismisses the keyboard
[self showDisclaimer];
}
- (void) showDisclaimer {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Disclaimer" message: #"bla bla bla"
delegate:self
cancelButtonTitle: nil
otherButtonTitles:#"Don't agree", #"I AGREE", nil];
[alertView show];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"I AGREE"])
{
[self showAlert];
}
else if([title isEqualToString:#"Don't agree"])
{
//Do something else
}
}
- (IBAction) backgroundTouch: (id)sender {
[TextField1 resignFirstResponder];
}
My answer is for you
ViewController
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITextFieldDelgate>
#property (nonatomic, strong) IBOutlet UITextField *txtFld;
#property (nonatomic, strong) UITextField *currentTxtFld;
- (IBAction)NextButton:(id)sender;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize currentTxtFld;
#synthesiz txtFld;
- (void)viewDidLoad {
txtFld.delegate = self;
}
- (IBAction) NextButton: (id) sender
{
[currentTxtFld resignFirstResponder];
[self showDisclaimer];
}
- (void) showDisclaimer
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Disclaimer" message:#"bla bla bla" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *agreeBtnAction = [UIAlertAction actionWithTitle:#"I AGREE" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
.......//Your code HEre
}];
UIAlertAction *dontagreeBtnAction= [UIAlertAction actionWithTitle:#"Don't agree" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
.......//Your code Here
}];
[alert addAction:agreeBtnAction];
[alert addAction:dontagreeBtnAction];
[self presentViewController:alert animated:YES completion:nil];
}
#pragma mark - UITextField Delegate methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
currentTxtFld = textFld;
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

Xcode - issue of push view by segue

I want to use push segue to change view. Also, I want to past few data to destination view. Before the segue change view, I want to pop a alertview with textfield to check user password. If match, it will change the view. If not, it will stop the push segue.
I know that if want to prevent the push segue to do something first need to use the below method, but it cannot get the segue.destinationViewController. Is anyway can replace the segue.destinationViewController?
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
Moreover, can I get the alertView textfield result in the - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender method?
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([identifier isEqualToString:#"chatSegue"]) {
NSLog(#"should");
NSString *plock = [Server getUserDatawithkey:#"plock"];
if ([plock isEqual:#"1"]) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Privacy Lock" message:#"Please enter your password:" delegate:self cancelButtonTitle:#"Continue" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * addFriendField = [alert textFieldAtIndex:0];
addFriendField.keyboardType = UIKeyboardTypeDefault;
addFriendField.placeholder = #"Enter your password";
alert.tag = ALERT_TAG_PW;
[alert show];
// Can I get the alertView textfield result at here?
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
friendCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
// Is anyway to replace the segue.destinationViewController?
chatViewController *cvc = segue.destinationViewController;
cvc.fid = cell.fid.text;
cvc.title = cell.f_name.text;
return YES;
}
return NO;
}
return YES;
}
Is somebody can help me? Thanks!
Instead of creating the alert in shouldPerformSegueWithIdentifier you should create it somewhere else (whatever is calling your push) and depending on the resulting action of that alert perform [self performSegueWithIdentifier:#"yourIdentifier" sender:self];
To handle the result of your alert you want to use the UIAlertViewDelegate and the method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex.
Might look something like this...
- (void)getServerStuff {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Privacy Lock" message:#"Please enter your password:" delegate:self cancelButtonTitle:#"Continue" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * addFriendField = [alert textFieldAtIndex:0];
addFriendField.keyboardType = UIKeyboardTypeDefault;
addFriendField.placeholder = #"Enter your password";
alert.tag = ALERT_TAG_PW;
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag == ALERT_TAG_PW) {
if(buttonIndex == 0) {
[self performSegueWithIdentifier:#"pushViewIdentifier" sender:self];
}
else if (buttonIndex == 1) {
//Do Nothing
}
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:#"pushViewIdentifier" sender:self]) {
ChatViewController *cvc = (ChatViewController *)segue.destinationViewController;
cvc.property = self.someProperty;
}
}

how to disable firstOtherbutton of alertview

I have alert-view that has a tableview as a subview. I want to disable firstOtherbutton after tableview didSelectRowAtIndexPath. I can call alertViewShouldEnableFirstOtherButton but firstotherbutton doesn't change.
- (void)viewDidLoad
{
self.announcement=[[UIAlertView alloc] initWithTitle:#"Announcement" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:#"Tamam", nil];
[announcement setValue:self.tbl.view forKey:#"accessoryView"];
announcement.delegate =self;
announcement.tag=100;
[announcement show];
[self.tbl.tableView reloadData];
}
-(void)didSelectAnswer
{
[self performSelector:#selector(alertViewShouldEnableFirstOtherButton:) withObject:announcement afterDelay:0.001];
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if (alertView.tag==100)
{
if(!self.tbl.deger)
{
return YES;
}
return NO;
}
return NO;
}
you have to write your custom alertview to achieve this feature.
Here are some open source implementations of alertview for iOS:
https://www.cocoacontrols.com/tags/uialertview

my view in not navigating to another view

I have two views - one is viewcontroller and another is enterdetails .
My question is my view- enterdetails is loading in the same view(viewcontroller) when I clicked alertview button it is not navigating to enterdetails
#import "enterDetails.h"
#interface enterDetails ()
#end
#implementation enterDetails
enterdetails.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *tab_image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
tab_image.image=[UIImage imageNamed:#"bar.jpeg"];
[self.view addSubview:tab_image];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
viewcontroller.m (alertview)
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
enterDetails *det=[[enterDetails alloc]init];
[self presentViewController:det animated:NO completion:nil];
}
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#" Already Sucessfully Register"delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:#"enter", nil];
[alert show];
[alert release];

UIAlertView with UIAlertViewStyleSecureTextInput: clearing field after regain activity

I have UIAlertView with UIAlertViewStyleSecureTextInput style. It works perfect. But when application resign active, and then regain active, then text previously typed into text input... is still visible. But when i'm trying to append some next char, all previous text is suddenly gone.
what may be reason of this behavior, and how to change it?
It is the default behavior of UIAlertViewStyleSecureTextInput.
The functionality is similar in UITextField having property secureTextEntry set to YES.
FIRST WAY
in your ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
alert = [[UIAlertView alloc]init];
[alert setDelegate:self];
[alert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
[alert show];
}
- (void)setText
{
UITextField *txt = [alert textFieldAtIndex:0];
[txt setText:#""];
}
in your viewController.h
Set the delegate
#import "AppDelegate.h"
#interface ViewController : UIViewController <UIPopoverControllerDelegate,alerttext>
in your appdelegate.h
#protocol alerttext <NSObject>
- (void)setText;
#end
#import <UIKit/UIKit.h>
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
id <alerttext> delegate;
}
#property (strong, nonatomic) id <alerttext> delegate;
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#end
in your appDelegate.m
#synthesize delegate;
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self.viewController setText];
}
SECOND WAY
in your viewcontroller.m
UITextField *text;
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
alert = [[UIAlertView alloc]init];
[alert setDelegate:self];
[alert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
[alert show];
text = [alert textFieldAtIndex:0];
}
- (void)setText
{
[text setText:#""];
}
in your ViewController.h, just declare this method
- (void)setText;
in your appdelegate.m just place this
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self.viewController setText];
}

Resources