如果没有选择收件人则显示提醒 iOS

Display alert if no recipients are selected iOS

我有一个页面,我们 select 收件人(他们是朋友)可以向其发送图像。但如果没有收件人 selected 我们仍然可以发送邮件。我想要这样,如果没有收件人 selected 我们可以显示 UIAlertView。对我来说,当我尝试显示警报时它不起作用,请参阅下面的代码。

.h
@property (nonatomic, strong) NSArray *friends;
@property (nonatomic, strong) PFRelation *friendsRelation;
@property (nonatomic, strong) NSMutableArray *recipients;

- (IBAction)send:(id)sender;

.m
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.recipients = [[NSMutableArray alloc] init];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.friendsRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"];

    PFQuery *query = [self.friendsRelation query];
    [query orderByAscending:@"username"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
        else {
            self.friends = objects;
            [self.tableView reloadData];
        }
    }];
//display camera modally etc......
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    PFUser *user = [self.friends objectAtIndex:indexPath.row];
    cell.textLabel.text = user.username;

    if ([self.recipients containsObject:user.objectId]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    PFUser *user = [self.friends objectAtIndex:indexPath.row];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.recipients addObject:user.objectId];
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.recipients removeObject:user.objectId];
    }

    NSLog(@"%@", self.recipients);
}

这是我尝试显示警报的部分

- (IBAction)send:(id)sender {

    if (!self.recipients) {  
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Select some friends" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [alertView show];

    } else {
        [self uploadMessage];
  //we upload the message in method to parse.com
    }
}

由于某种原因它似乎没有显示,因此我们无法向任何人发送消息。如何显示警报视图?

试试这个。检查收件人对象是否为0。

- (IBAction)send:(id)sender {

    if ([self.recipients count]==0) {  
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Select some friends" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [alertView show];

    } else {
        [self uploadMessage];
  //we upload the message in method to parse.com
           [self.tabBarController setSelectedIndex:1]; 
    }
}

您的代码的问题在于您正在检查的条件::

if (!self.recipients)

此处 self.recipients 将始终给出真值,因为它将查找此对象的内存地址,您已在 viewDidLoad 方法中分配给它。

您必须检查您的场景中的数组计数。