项目需求
废话不说,直接上试题 及答案 代码
#import "TableViewController.h"
@interface TableViewController ()<UIAlertViewDelegate>
@property (nonatomic, strong) NSMutableArray * dataSource;
- (IBAction)buy:(id)sender;
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"购物清单";
_dataSource = [@[@{@"name":@"Effective Objective-C 2.0",@"price":@"¥55.20"},
@{@"name":@"iOS组件与框架",@"price":@"¥71.80"},
@{@"name":@"iOS核心开发手册",@"price":@"¥98.20"},
@{@"name":@"iOS开发范例实战宝典(进阶篇)",@"price":@"¥70.00"},
@{@"name":@"iOS开发进阶",@"price":@"¥55.30"},
@{@"name":@"iOS编程(第4版)",@"price":@"¥81.20"},
@{@"name":@"Objective-C高级编程 iOS与OS X多线程和内存管理",@"price":@"¥41.70"}] mutableCopy];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ShowCell"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShowCell" forIndexPath:indexPath];
cell.textLabel.text = _dataSource[indexPath.row][@"name"];
cell.detailTextLabel.text = _dataSource[indexPath.row][@"price"];
return cell;
}
- (IBAction)buy:(id)sender {
UIAlertView * alert =
[[UIAlertView alloc] initWithTitle:@"提示"
message:@"请输入密码"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
//No.1
//开始写代码,设置警告框为带有一个密文输入框的样式
alert
//end_code
[alert show];
//No.2
//开始写代码,设置输入框键盘为数字键盘。
UITextField * textfield =
//end_code
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString * title = @"提示";
NSString * message;
NSString * cancelButtonTitle = @"确定";
//No.3
//开始写代码,判断密文输入框里的内容是否为@"123456",并弹出警告框提示 @"密码输入错误"或@"购买成功"
UITextField * textfield = ;
//end_code
UIAlertView * wrongAlert =
[[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:nil, nil];
[wrongAlert show];
}
@end
答案:
UIAlertView * alert =
[[UIAlertView alloc] initWithTitle:@"提示"
message:@"请输入密码"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
//No.1
//开始写代码,设置警告框为带有一个密文输入框的样式
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
//end_code
[alert show];
//No.2
//开始写代码,设置输入框键盘为数字键盘。
UITextField * textfield = [alert textFieldAtIndex:0];
textfield.keyboardType = UIKeyboardTypeNumberPad;
//end_code
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString * title = @"提示";
NSString * message;
NSString * cancelButtonTitle = @"确定";
//No.3
//开始写代码,判断密文输入框里的内容是否为@"123456",并弹出警告框提示 @"密码输入错误"或@"购买成功"
UITextField *textfield = [alertView textFieldAtIndex:0];
if ([textfield.text isEqualToString: @"123456"]) {
message = @"购买成功";
}else{
message = @"密码输入错误";
}
//end_code