我刚刚开始学习目标C.想要创建一个小应用程序,其中应该有一个登录页面,只有当它的长度大于或等于8时才应该接受密码。一旦你提交下一页应该是电影列表,在该页面中应该有一个搜索按钮以及添加和删除按钮,这些按钮可以使该列表可编辑。我创建了登录页面,我已直接将提交按钮与电影列表页面链接,因为我对如何验证密码长度和用户名的条件感到困惑。请帮助我这样做,并为我的电影列表页面获得正确的编辑按钮。
谢谢
“ViewController.m”
#import "ViewController.h"
#import "MovieList.h"
@interface ViewController ()
@end
@implementation ViewController
-(void)loadView{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor cyanColor];
UILabel *name=[[UILabel alloc] init];
UILabel *pass=[[UILabel alloc] init];
UITextField *username = [[UITextField alloc]init];
UITextField *password = [[UITextField alloc]init];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[name setBackgroundColor:[UIColor cyanColor]];
[name setText:@"UserID"];
[name setTextColor:[UIColor blackColor]];
[pass setBackgroundColor:[UIColor cyanColor]];
[pass setText:@"Password"];
[pass setTextColor:[UIColor blackColor]];
[button setTitle:@"SUBMIT" forState:UIControlStateNormal];
username.delegate = self;
password.delegate = self;
username.borderStyle = UITextBorderStyleRoundedRect;
password.borderStyle = UITextBorderStyleRoundedRect;
name.frame = CGRectMake(20, 15, 80, 20);
pass.frame = CGRectMake(15, 55, 80, 20);
username.frame = CGRectMake(100, 10, 200, 30);
password.frame = CGRectMake(100, 50, 200, 30);
button.frame = CGRectMake(130, 90, 80, 30);
[self.view addSubview:button];
[self.view addSubview:username];
[self.view addSubview:password];
[self.view addSubview:name];
[self.view addSubview:pass];
[button addTarget:self action:@selector(gotosecondpage) forControlEvents:UIControlEventTouchUpInside];
}
-(void)gotosecondpage
{
Movielist *secondViewcont = [[Movielist alloc] init];
[self.navigationController pushViewController:secondViewcont animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
@end
“MovieList.m”
#import "MovieList.h"
@interface Movielist()
@end
@implementation Movielist
-(void)loadView{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor cyanColor];
}
-(id)init{
[self.navigationItem setTitle:@"Movie List"];
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self.view setBackgroundColor:[UIColor cyanColor]];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = self.view.frame;
UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableView.backgroundColor = [UIColor cyanColor];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
[tableView setScrollEnabled:YES];
[tableView setUserInteractionEnabled:YES];
[tableView setRowHeight:35];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIDent"];
char ch = 'a' + indexPath.row;
cell.textLabel.text = [NSString stringWithFormat:@"%c" , ch];
return cell;
}
@end
答案 0 :(得分:0)
检查长度非常直接。重要的是如何从登录页面导航。目前我看到gotosecondpage函数负责。您可以编写一个validateParams方法,如果满足所有条件,您可以在该方法下调用gotosecondpage。
此代码将告诉您密码长度:
-(void) validateParams
{
NSString * password = [password text];
if ([password length] < 8) //and maybe other conditions, too
{
[self gotosecondpage];
}
else
{
//report error - maybe show some UILabel near username / password fields to show which one of them went wrong
}
}
请注意,您还可以执行许多其他操作 - 例如告诉用户哪个字段需要更正,还有更好的方法。由于您的要求非常基本,以上情况应该有所帮助。