gpt4 book ai didi

iOS:简化 UIAlertView 代码以便可以反复使用

转载 作者:行者123 更新时间:2023-11-28 18:36:09 25 4
gpt4 key购买 nike

这是我学习 XCode 的第二天,所以我在这里完全是新手,我有这个 .m 代码:​​

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[_fieldEmail setFont:[UIFont fontWithName:@"ABeeZee-Regular" size:14]];
[_fieldPassword setFont:[UIFont fontWithName:@"ABeeZee-Regular" size:14]];
[_titleLogin setFont:[UIFont fontWithName:@"Raleway-ExtraLight" size:28]];

UIView *fieldEmail = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldEmail.leftViewMode = UITextFieldViewModeAlways;
_fieldEmail.leftView = fieldEmail;

UIView *fieldPassword = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldPassword.leftViewMode = UITextFieldViewModeAlways;
_fieldPassword.leftView = fieldPassword;

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (BOOL)validateEmail:(NSString *)emailStr {
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:emailStr];
}

- (IBAction)buttonRegister {
_labelOutput.text = @"Register?";
}

- (IBAction)buttonLogin {
if ([_fieldEmail.text length] > 0) {
if(![self validateEmail:[_fieldEmail text]]){
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Email Error"
message:@"That's not a valid email!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}else{
_labelOutput.text = @"Login?";
}
}else{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Login Error"
message:@"Please enter your email"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}

}

- (IBAction)loginFacebook {
if ([_fieldPassword.text length] > 15) {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Password Error"
message:@"Password must be less than 15 characters."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}else{
_labelOutput.text = @"Login with FB?";
}
}

- (IBAction)loginTwitter {
// _labelOutput.text = @"Login with Twitter ya?";
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Login Error"
message:@"Please enter your email correctly."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
@end

如您所见,我用来显示警告框的代码重复了。有什么办法可以把这段类似的代码放在上面的某个地方,这样就可以通过将标题和消息作为变量来调用它:

    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Login Error"
message:@"Please enter your email correctly."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];

在 PHP 世界里,这个方法叫做 function,但我不知道在 Objective-C 世界里叫什么名字。非常感谢。

最佳答案

要减少代码,为什么不创建另一种方法来显示 AlertBox?

- (void) alertWithTitle:(NSString *)title andMessage:(NSString *)msg
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}

然后简单地调用:

[self alertWithTitle:(YOUR TITLE) andMessage:(YOUR MESSAGE)];

关于iOS:简化 UIAlertView 代码以便可以反复使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18691295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com