创建登陆窗口(有用户名和密码),确认后弹出对话框再输入一遍,如果都相同,显示用户图片,如果不相同,弹出上拉菜单(UIActionSheet),问是否重新输入,是的话弹出对话框重新输入。
效果图:
viewController.h:
//
// DXWViewController.h
// 2013-8-12作业
//
// Created by 丁小未 on 13-8-12.
// Copyright (c) 2013年 dingxiaowei. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface DXWViewController : UIViewController<UIAlertViewDelegate,UIActionSheetDelegate>
@property (retain, nonatomic) IBOutlet UITextField *lblName;
@property (retain, nonatomic) IBOutlet UITextField *lblPassword;
- (IBAction)Check:(id)sender;
@property (retain, nonatomic) IBOutlet UIImageView *image;
- (IBAction)clickKeyBoardReturn:(id)sender;
@property (retain, nonatomic) IBOutlet UILabel *lblState;
@end
ViewController.m:
//
// DXWViewController.m
// 2013-8-12作业
//
// Created by 丁小未 on 13-8-12.
// Copyright (c) 2013年 dingxiaowei. All rights reserved.
//
#import "DXWViewController.h"
#import "MyDelegate.h"
NSString *nameT;
NSString *pwdT;
@interface DXWViewController ()
@end
@implementation DXWViewController
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
NSLog(@"重试");
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"身份验证" message:@"请输入用户名和密码" delegate:self cancelButtonTitle:@"登陆" otherButtonTitles:@"取消",nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert show];
[alert release];
}
else if(buttonIndex == 1)
{
NSLog(@"取消");
}
}
//实现协议
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"didDismissWithButtonIndex %d",buttonIndex);
NSLog(@"%@",[alertView textFieldAtIndex:0].text);
NSLog(@"%@",[alertView textFieldAtIndex:1].text);
nameT = [alertView textFieldAtIndex:0].text;
pwdT = [alertView textFieldAtIndex:1].text;
NSString *na = self.lblName.text;
NSString *pw = self.lblPassword.text;
if (buttonIndex == 0) {
//相同为0,不同为-1
if ([nameT compare:na]==0 && [pwdT compare:pw]==0)
{
//登陆成功显示图片
NSLog(@"登陆成功!");
self.lblState.text = @"登陆成功,酷狗来也!";
self.image.hidden = false;
self.lblState.hidden = false;
}
else
{
self.image.hidden = true;
self.lblState.text = @"登陆失败!";
self.lblState.hidden = false;
NSLog(@"登陆失败!");
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择框" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"重试" otherButtonTitles:nil];
sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[sheet showInView:self.view];
}
}
else if(buttonIndex == 1)
{
NSLog(@"取消");
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.image.hidden = YES;//将头像图片隐藏
[self.lblName becomeFirstResponder];//设置为第一响应者
self.lblState.hidden = YES;//隐藏消息栏
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[_lblName release];
[_lblPassword release];
[_image release];
[_lblState release];
[super dealloc];
}
- (IBAction)Check:(id)sender {
// MyDelegate *delegate = [[MyDelegate alloc]init];
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"身份验证" message:@"请输入用户名和密码" delegate:self cancelButtonTitle:@"登陆" otherButtonTitles:@"取消",nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert show];
[alert release];
}
- (IBAction)clickKeyBoardReturn:(id)sender {
[self.lblName resignFirstResponder];
[self.lblPassword resignFirstResponder];
}
@end
如果允许让屏幕横屏:
//是否可以自动横屏
-(BOOL)shouldAutorotate
{
return YES;
}
//可以各个方向横屏
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
//每当屏幕旋转的时候都会触发一个
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//如果是是横屏状态
if(toInterfaceOrientation ==UIInterfaceOrientationLandscapeLeft ||toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight)
{
}
else
{
}
如果要设置系统启动时就是横屏,可以修改plist文件Supportd interface orientations的Item的顺序,如果item0是横屏,那就是横屏启动,在项目运行的时候,可以点击command+左右键