我需要保存个人资料搜索,并允许用户点击它们,而不是再次输入电子邮件地址。我将最近的搜索放在列表的顶部,并试图不让它们复制。出于某种原因,它会在前几次点击时起作用,然后重复它们。任何人都可以建议更清晰,更正确的代码来实现这一目标吗?
如果您点击最近的电子邮件,它将搜索该电子邮件并将其移至顶部。出于某种原因,有时它会在底部留下副本,有时它可以正常工作。
@implementation Search
{
bool badInfo;
bool first;
bool second;
bool third;
bool fourth;
bool fifth;
NSInteger success;
NSString *firstemail;
NSString *secondemail;
NSString *thirdemail;
NSString *fourthemail;
NSString *fifthemail;
NSString *searchEmail;
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
firstemail = [defaults objectForKey:@"firstemail"];
secondemail = [defaults objectForKey:@"secondemail"];
thirdemail = [defaults objectForKey:@"thirdemail"];
fourthemail = [defaults objectForKey:@"fourthemail"];
fifthemail = [defaults objectForKey:@"fifthemail"];
_recentEmailOne.enabled = NO;
_recentEmailTwo.enabled = NO;
_recentEmailThree.enabled = NO;
_recentEmailFour.enabled = NO;
_recentEmailFive.enabled = NO;
if (firstemail) {
_recentEmailOne.enabled = YES;
[_recentEmailOne setTitle:firstemail forState:UIControlStateNormal];
}
if (secondemail) {
_recentEmailTwo.enabled = YES;
[_recentEmailTwo setTitle:secondemail forState:UIControlStateNormal];
}
if (thirdemail) {
_recentEmailThree.enabled = YES;
[_recentEmailThree setTitle:thirdemail forState:UIControlStateNormal];
}
if (fourthemail) {
_recentEmailFour.enabled = YES;
[_recentEmailFour setTitle:fourthemail forState:UIControlStateNormal];
}
if (fifthemail) {
_recentEmailFive.enabled = YES;
[_recentEmailFive setTitle:fifthemail forState:UIControlStateNormal];
}
_emailInput.text = @"";
_emailInput.delegate = self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)emailOne:(id)sender {
searchEmail = firstemail;
[self doSearch];
}
- (IBAction)emailTwo:(id)sender {
searchEmail = secondemail;
[self doSearch];
}
- (IBAction)emailThree:(id)sender {
searchEmail = thirdemail;
[self doSearch];
}
- (IBAction)emailFour:(id)sender {
searchEmail = fourthemail;
[self doSearch];
}
- (IBAction)emailFive:(id)sender {
searchEmail = fifthemail;
[self doSearch];
}
在我保存信息的搜索结果中。这就是问题所在。
//save data
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:searchEmail forKey:@"firstemail"];
if (fifthemail) {
if ([fifthemail isEqualToString:searchEmail]) {
fifthemail = nil;
} else {
fifth = YES;
}
}
if (fourthemail) {
if ([fourthemail isEqualToString:searchEmail]) {
fourthemail = nil;
} else {
fourth = YES;
}
}
if (thirdemail) {
if ([thirdemail isEqualToString:searchEmail]) {
thirdemail = nil;
} else {
third = YES;
}
}
if (secondemail) {
if ([secondemail isEqualToString:searchEmail]) {
secondemail = nil;
} else {
second = YES;
}
}
if (firstemail) {
if ([firstemail isEqualToString:searchEmail]) {
firstemail = nil;
} else {
first = YES;
}
}
if (first) {
[defaults setObject:firstemail forKey:@"secondemail"];
} else if (second) {
[defaults setObject:secondemail forKey:@"secondemail"];
second = NO;
}
if (second) {
[defaults setObject:secondemail forKey:@"thirdemail"];
} else if (third) {
[defaults setObject:thirdemail forKey:@"thirdemail"];
third = NO;
} else if (fourth) {
[defaults setObject:fourthemail forKey:@"thirdemail"];
fourth = NO;
}
if (third) {
[defaults setObject:thirdemail forKey:@"fourthemail"];
} else if (fourth) {
[defaults setObject:fourthemail forKey:@"fourthemail"];
fourth = NO;
} else if (fifth) {
[defaults setObject:fifthemail forKey:@"fourthemail"];
fifth = NO;
}
if (fourth) {
[defaults setObject:fourthemail forKey:@"fifthemail"];
} else if (fifth) {
[defaults setObject:fifthemail forKey:@"fifthemail"];
}
if (success == 1) // good login
{
NSLog(@"Login Success");
[defaults setObject:searchEmail forKey:@"searchemail"];
[defaults synchronize];
[self performSegueWithIdentifier:@"viewprofile" sender:self];
} else if (success == 2) {
NSLog(@"profile not found");
[defaults setObject:searchEmail forKey:@"searchemail"];
[defaults synchronize];
[self performSegueWithIdentifier:@"notfound" sender:self];
} else {
NSLog(@"bad information entered");
NSString *error_msg = (NSString *) [jsonData objectForKey:@"message"];
[self alertStatus:error_msg :@"Search Failed!"];
}
答案 0 :(得分:1)
最天真的方法(不包括你的方法)是这样的:
// Replace newString if found, insert new into top
int match = -1;
int index = 0;
for(NSString * string in historyArray)
{
if([newString isEqualToString:string])
{
match = index;
break;
}
index++;
}
if(match != -1)
[historyArray removeObjectAtIndex:index];
[historyArray insertObject:newString atIndex:0];
如果您想在上述代码之后限制历史记录的大小:
if(historyArray.count > kSearchHistorySize)
[historyArray removeLastObject];
以上只有在您一次添加一个字符串时才有效,否则请更换' if'与'而'也许