标签:
收集购物信息 iOS项目
倒计时:588
步骤
/.panel-heading
项目需求
设置警告框样式为带有两个文本输入的警告框,用于收集用户收货地址和联系电话。并选择合适的代理方法,当警告框上的两输入框有一个为空时限制“购买”按钮点击。
#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"}] mutableCopy];
}
- (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:@"取消"
otherButtonTitles:@"购买", nil];
//No.1
//开始写代码,设置警告框上有两个文本输入框,输入框内默认占位文本分别为@"收货地址"和@"联系电话",并将联系电话的文本输入框设置为数字键盘。
alert
//end_code
[alert show];
}
//No.2
//开始写代码,选择合适的代理方法,实现警告框上的两输入框有一个为空则“购买”按钮无法点击
- (BOOL)
//end_code
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == alertView.cancelButtonIndex) {
return;
}
NSString * message;
if ([alertView textFieldAtIndex:1].text.length != 11) {
message = @"您输入的电话号码有误";
}else{
message = @"购买成功";
}
UIAlertView * tips =
[[UIAlertView alloc] initWithTitle:@"提示"
message:message
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil];
[tips show];
}
@end
答案如下:
UIAlertView * alert =
[[UIAlertView alloc] initWithTitle:@"提示"
message:@"请输入联系方式"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"购买", nil];
//No.1
//开始写代码,设置警告框上有两个文本输入框,输入框内默认占位文本分别为@"收货地址"和@"联系电话",并将联系电话的文本输入框设置为数字键盘。
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UITextField *textfield1 = [alert textFieldAtIndex:0];
textfield1.placeholder = @"收货地址";
UITextField *textfield2 = [alert textFieldAtIndex:1];
textfield2.placeholder = @"联系电话";
textfield2.keyboardType = UIKeyboardTypeNumberPad;
textfield2.secureTextEntry = NO;
alert.delegate = self;
//end_code
[alert show];
}
//No.2
//开始写代码,选择合适的代理方法,实现警告框上的两输入框有一个为空则“购买”按钮无法点击
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
UITextField *textfield1 = [alertView textFieldAtIndex:0];
UITextField *textfield2 = [alertView textFieldAtIndex:1];
if ([textfield1.text isEqualToString:@"" ] || [textfield2.text isEqualToString:@""])
{
return NO;
}else return YES;
}
//end_code
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == alertView.cancelButtonIndex) {
return;
}
NSString * message;
if ([alertView textFieldAtIndex:1].text.length != 11) {
message = @"您输入的电话号码有误";
}else{
message = @"购买成功";
}
UIAlertView * tips =
[[UIAlertView alloc] initWithTitle:@"提示"
message:message
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil, nil];
[tips show];
}
试题来源于 猿圈 ,答案本人自己编写,如有不足之处,请大家指正
设置警告框样式为带有两个文本输入的警告框,用于收集用户收货地址和联系电话。并选择合适的代理方法,当警告框上的两输入框有一个为空时限制“购买”按钮点击。
标签:
原文地址:http://www.cnblogs.com/iOS-OC/p/5515711.html