我刚刚开始学习目标 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