0

我对Objective-C很陌生,所以希望这一切都有意义。此处在验证用户名和密码字段后会UIAlertView弹出。我想要的是当用户按下UIAlertViewButton. 控件应导航到指令视图控制器。

但在这里它不起作用

这是.h文件代码:

#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

这是 .m 文件代码:

#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

请帮助我使用UIAlertView

4

3 回答 3

0

试试这个方法来获取警报按钮的事件

改变你appdelegate这个方法:

    - (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;
}

// 将此添加到您的登录视图控制器中。

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

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

}

我希望这段代码对你有用。

于 2014-02-28T09:08:36.613 回答
0

乍一看,它看起来不错。

如果它正在运行但没有推送任何东西,我的疯狂猜测是你可能实际上不在导航控制器中,所以 self.navigationController 为零。

于 2014-02-28T09:07:45.350 回答
0

您不需要使用错误密码的委托方法,因此当您创建警报时,您不需要将委托设置为自我。

这段代码应该可以工作

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];
}

因此,您的 alertForCorrect 将是您在委托方法中收到的唯一一个alertView:clickedButtonAtIndex:

所以像这样实现你的方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        // Cancelled
        viewControllerinst = [[InstructionBIDViewController alloc] initWithNibName:@"InstructionBIDViewController" bundle:nil];
        [self.navigationController pushViewController:viewControllerinst animated:YES];
    }
}
于 2014-02-28T09:54:23.657 回答