我在 UI 上有一个观察者模式,它检查处理试图更新数据库上某个字段的服务器连接的对象的状态。
UI 的更新方法接收一个对象,该对象包含数据对,其中包含连接发生情况的信息。问题是我纠结于很多 ifs 检查不同的可能性。
- (void) update:(Bundle *)arg
{
if ([[arg getData:@"updatee"] isEqualToString:@"email"]){
UITableViewCell *emailCell = [[self tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
if ([[arg getData:@"connecting"] isEqualToString:@"true"]) {
//Email is being posted
[_emailLabel_email setText:@"Connecting..."];
[_emailLabel_set setHidden:YES];
emailCell.accessoryType = UITableViewCellAccessoryNone;
[_emailActivityIndicator startAnimating];
}else{
if ([[arg getData:@"succesfull"] isEqualToString: @"false"])
//Email was posted unsuccesfully
[[[UIAlertView alloc] initWithTitle:@"Taken Email Address"
message:@"The email address that you entered is already in use, please double check it"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
else{
//Email was posted succesfully.
[_emailLabel_set setText:@"Change"];
}
[_emailActivityIndicator stopAnimating];
[_emailLabel_email setText:[mng getEmail]];
[_emailLabel_set setHidden:NO];
emailCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
//Password cases
}
}
当服务器以字符串响应时,我发现很难避免这种意大利面条式的代码。
在更新方法上发送哪个更智能的对象?