代码之家  ›  专栏  ›  技术社区  ›  smithyy

如果未选择收件人,则显示警报iOS

  •  2
  • smithyy  · 技术社区  · 9 年前

    我有一个页面,我们在其中选择要向其发送图像的收件人(他们是朋友)。但如果没有选择收件人,我们仍然可以发送邮件。我希望这样,如果没有选择收件人,我们可以显示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
        }
    }
    

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

    2 回复  |  直到 9 年前
        1
  •  2
  •   Kumar    9 年前

    试试这个。收件人的检查对象是否等于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]; 
        }
    }
    
        2
  •  1
  •   Vikas Dadheech    9 年前

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

    if(!self.receivers)

    这里是自我。收件人将始终给出真值,因为它将查找此对象的内存地址,您已在viewDidLoad方法中分配给它。

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