29
Abr 11

Usar un UIAlertView para solicitar usuario y contraseña

A veces es necesario, al desarrollar una aplicación para iphone, hacer un inicio de sesión para poder mostrar la información de una ventana.

Con esta clase es posible mostrar un UIAlertView con dos campos de texto que se encargan de solicitar un usuario y una contraseña y lo entrega a la vista que lo está mostrando para su procesamiento.

SilLoginPrompt.h

//
// SilLoginPrompt.h
// Alert login window
//
// Created by Byron Herrera on 4/20/11.
// Copyright 2011 Silencesoft. All rights reserved.
//

#import < Foundation/Foundation.h>

@interface SilLoginPrompt : UIAlertView
{
UITextField * textUser;
UITextField * textPassword;
}

@property (nonatomic, retain) UITextField * textUser;
@property (nonatomic, retain) UITextField * textPassword;
@property (readonly) NSString * userName;
@property (readonly) NSString * userPassword;

- (id) initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;

@end


SilLoginPrompt.m

//
// SilLoginPrompt.m
// Alert login window
//
// Created by Byron Herrera on 4/20/11.
// Copyright 2011 Silencesoft. All rights reserved.
//

#import "SilLoginPrompt.h"
// #import < QuartzCore/QuartzCore.h>

@implementation SilLoginPrompt

@synthesize textUser, textPassword;
@synthesize userName, userPassword;

-(id) initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle {
if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okButtonTitle, nil])
{

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 70.0, 260.0, 25.0)];
[textField setBackgroundColor:[UIColor whiteColor]];
[textField setPlaceholder:@"Username"];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
/*
textField.textAlignment = UITextAlignmentCenter;
textField.backgroundColor = [UIColor whiteColor];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.layer.cornerRadius = 8.0f;
textField.layer.masksToBounds = YES;
textField.layer.borderColor = [[UIColor blackColor] CGColor];
textField.layer.borderWidth = 1.0f;
*/
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[textField setBackgroundColor:[UIColor clearColor]];
[self addSubview:textField];
self.textUser = textField;
[textField release];

textField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 95.0, 260.0, 25.0)];
[textField setBackgroundColor:[UIColor whiteColor]];
[textField setPlaceholder:@"Password"];
[textField setSecureTextEntry:YES];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[textField setBackgroundColor:[UIColor clearColor]];
[self addSubview:textField];
self.textPassword = textField;
[textField release];
self.message = [NSString stringWithFormat:@"%@\n\n\n", message];

}
return self;
}

- (void)show
{
[textUser becomeFirstResponder];
[super show];
}

- (NSString *)userName
{
return textUser.text;
}

- (NSString *)userPassword
{
return textPassword.text;
}

- (void)dealloc
{
[textUser release];
[textPassword release];
[super dealloc];
}

@end

Para usarla:

SilLoginPrompt *newPrompt = [[SilLoginPrompt alloc] initWithTitle:@"Test"
message:@"Message..."
delegate:self
cancelButtonTitle:@"cancel"
okButtonTitle:@"login"];
[newPrompt show];
[newPrompt release];

Y para recibir la información ingresada con el delegado:

#pragma mark -
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex])
{
if ([alertView isKindOfClass:[SilLoginPrompt class]]) {
NSString * userName = [(SilLoginPrompt *)alertView userName];
NSString * userPassword = [(SilLoginPrompt *)alertView userPassword];
NSLog(@"Entered information: %@ - %@", userName, userPassword);
}
}
}

La información comentada se refiere a personalización del campo de texto hecha con QuartzCore.

Copyright © 2016 Silenceway
Proudly powered by WordPress, Free WordPress Themes

 Subscribe in a reader