Pregunta

I'm pretty new to Objective-C so hopefully this all makes sense. Here after authentication of user name and password fields the UIAlertView pops up. What I want is when the user presses the UIAlertViewButton. The Control should navigate to instruction view controller.

But here it is not working

Here is .h file code:

#import <UIKit/UIKit.h>
#import "InstructionBIDViewController.h"
@interface SignInViewController : UIViewController<UIAlertViewDelegate>{

    IBOutlet UITextField *usernamefield;
    IBOutlet UITextField *password;
    NSDictionary *creditialDictionary;
}
@property (strong, nonatomic) InstructionBIDViewController *viewControllerinst;
-(IBAction)enterCredential;
//-(void) submitData;
@end

Here is .m file code:

#import "SignInViewController.h"
#import "InstructionBIDViewController.h"
@interface SignInViewController ()

@end

@implementation SignInViewController
@synthesize viewControllerinst;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
        //[self submitData];
    creditialDictionary = 
      [[NSDictionary alloc ] initWithObjects:
        [NSArray arrayWithObjects:@"password",@"1234",nil] 
                                     forKeys:
        [NSArray arrayWithObjects:@"username", @"alex",nil]];

}
-(IBAction)enterCredential
{
    if ([[creditialDictionary objectForKey:usernamefield.text] 
           isEqualToString:password.text]) {
        UIAlertView *alert = 
          [[UIAlertView alloc] initWithTitle:@"Correct Password" 
                                     message:@"This password is correct" 
                                    delegate:self 
                           cancelButtonTitle:@"Dismiss"  
                           otherButtonTitles:nil];
        [alert show];
    } else {
        UIAlertView *alert = 
          [[UIAlertView alloc] initWithTitle:@"InCorrect Password" 
                                     message:@"This password is incorrect" 
                                    delegate:self 
                           cancelButtonTitle:@"Dismiss" 
                           otherButtonTitles:nil];
        [alert show];
    }
}

//-(void) alertView:(UIAlertView *)alertView 
          clickedButtonAtIndex:(NSInteger)buttonIndex{
//Here the functionality that i want to perform ...but it is not working
    - (void)alertView:(UIAlertView *)alertView 
            didDismissWithButtonIndex:(NSInteger)buttonIndex{
    // u need to change 0 to other value(,1,2,3) if u have more buttons.
    // then u can check which button was pressed.

    if (buttonIndex == alertView.cancelButtonIndex) {
        // Cancelled
        viewControllerinst = [[InstructionBIDViewController alloc]
          initWithNibName:@"InstructionBIDViewController" bundle:nil];
        [self.navigationController pushViewController:viewControllerinst 
                                             animated:YES];
    }   
}

@end

Please help me to perform this navigation using UIAlertView ?

¿Fue útil?

Solución

Try this method for get event of alert button

Change you appdelegate this method :

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


    viewController=[[SignInViewController alloc] init];

    UINavigationController *navigationcontroller=[[UINavigationController alloc] initWithRootViewController:viewController];
    [self.window setRootViewController:navigationcontroller];

    //self.window.rootViewController = self.viewController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

// add this in your signinviewcontroller.

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

        if (buttonIndex == 0 )
        {
              viewControllerinst = [[InstructionBIDViewController alloc] initWithNibName:@"InstructionBIDViewController" bundle:nil];
             [self.navigationController pushViewController:viewControllerinst animated:YES];
        }

}

i hope this code useful for you.

Otros consejos

It looks ok at a first glance to me.

If its running but not pushing anything my wild guess would be you might not actually be in a navigation controller, so self.navigationController is nil.

You don´t need delegate method for your incorrect password so when you create your alert you don´t need you set delegate to self.

This code should work

if ([[creditialDictionary objectForKey:usernamefield.text]isEqualToString:password.text]) {
    UIAlertView *alertForCorrect = [[UIAlertView alloc  ]initWithTitle:@"Correct Password" message:@"This password is correct" delegate:self cancelButtonTitle:@"Dismiss"  otherButtonTitles:nil];
    [alertForCorrect show];
}
else{
    UIAlertView * alertForIncorrect=[[UIAlertView alloc  ]initWithTitle:@"InCorrect Password" message:@"This password is incorrect" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [alertForIncorrect show];
}

So your alertForCorrect will be the only one you will receive in your delegate method alertView:clickedButtonAtIndex:

So implement your method like this:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        // Cancelled
        viewControllerinst = [[InstructionBIDViewController alloc] initWithNibName:@"InstructionBIDViewController" bundle:nil];
        [self.navigationController pushViewController:viewControllerinst animated:YES];
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top