【问题标题】:What is wrong with my UIAlertView delegate method?我的 UIAlertView 委托方法有什么问题?
【发布时间】:2014-02-28 09:03:01
【问题描述】:

我对 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 执行此导航?

【问题讨论】:

  • 我敢打赌,问题在于他的委托方法应该寻找不同的 buttonIndex 而不是 cancelButtonIndex。
  • 在按下 alertview buttonO(cancelButtonTitle:@"Dismiss") 时,控件不会切换到 InstructionBIDViewController ....
  • 在 if 语句中插入断点并验证它是否被调用。
  • @user3239274 检查我的以下答案。
  • 代码正确调试这一行:[self.navigationController pushViewController:viewControllerinst animated:YES]; ....但它到达 viewDidLoad InstructionBIDViewController ...

标签: ios objective-c uialertview xib uialertviewdelegate


【解决方案1】:

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

改变你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];
        }

}

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

【讨论】:

  • 程序在 InstructionBIDViewController.h 的 viewDidLoad 处崩溃,为什么?请帮助...
  • @user3239274 你下载了我的代码?在我的 Mac 中,这段代码现在可以完美运行了。
  • @user3239274 只改变你的代码 appdelegate 方法和 alertview 方法。
【解决方案2】:

乍一看还不错。

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

【讨论】:

    【解决方案3】:

    您的密码不正确不需要委托方法,因此当您创建警报时,您不需要将委托设置为自己。

    这段代码应该可以工作

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多