0

我刚刚开始学习目标 C。想要创建一个小应用程序,其中应该有一个登录页面,并且只有当它的长度大于或等于 8 时才应该接受密码。一旦你提交了下一页应该是一个列表电影,并且在该页面中应该有一个搜索按钮以及可以使该列表可编辑的添加和删除按钮。我创建了登录页面,并直接将提交按钮与电影列表页面链接起来,因为我对如何验证密码长度和用户名的条件感到困惑。请帮助我这样做并为我的电影列表页面获取正确的编辑按钮。

谢谢

“视图控制器.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

“电影列表.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
4

1 回答 1

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
    }
}

请注意,您还可以做很多其他事情——比如告诉用户哪个字段需要更正,还有很多更好的方法。由于您的要求非常基本,因此上述内容现在应该有所帮助。

于 2013-01-18T12:05:24.220 回答