2016-02-04 59 views
0

我在Swift中看到了這個問題的解決方案。誰能幫我嗎?從TableViewCell中顯示AlertController,Objective-C

我在TableViewController類此方法:

- (void)presentAlert { 
    UIAlertController *alert = [UIAlertController 
          alertControllerWithTitle:@"Flag Content" 
          message:@"Are you sure you want to report this post?" 

    UIAlertAction *okAction = [UIAlertAction 
          actionWithTitle:NSLocalizedString(@"OK", @"OK action") 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction *action) 
          { 
           NSLog(@"OK action"); 
           [cell.feedItem incrementKey:@"flagTotal"]; 
           [cell.feedItem saveInBackground]; 
           [UIView beginAnimations:nil context:nil]; 
           [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:cell.flagButton cache:YES]; 
           [UIView commitAnimations]; 
           // Set flagButton image to checked image 
           [cell.flagButton setImage:[UIImage imageNamed:@"filledFlagButtonIcon"] forState:UIControlStateNormal]; 
           [self dismissViewControllerAnimated:YES completion:nil]; 
          }]; 

[alert addAction:okAction]; 
[self presentViewController:alert animated:YES completion:nil]; 
          preferredStyle:UIAlertControllerStyleAlert]; 

我希望簡單地調用從IBAction爲這種方法在像格:

- (IBAction)flagTapped:(id)sender { 
    _tableView = [[FeedTableViewController alloc] init]; 
    [_tableView presentAlert]; 
} 
+0

你要調用一個alertview時選擇特定的一行在表視圖? – UlyssesR

回答

2

這不是正確的方法這樣做。你可以通過兩種方式實現這一點。一種方法是在你的cellForRowAtIndexPath相加的目標措施和刪除 - 從你的類(IBAction)flagTapped:(id)sender也移除細胞的作用在界面生成器,因爲我們正在做的程序,如:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 


     [cell.button addTarget:self action:@selector(presentAlert) forControlEvents:UIControlEventTouchUpInside]; 

} 

實現這一目標的另一種方法是定義一個協議在你的單元類。在該協議中添加此方法 - (void)presentAlert聲明。將TableViewController設置爲cellForRowAtIndexPath as中的單元的委託。 flagTapped的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

      cell.delegate = self; 
    } 

實施將是像

- (IBAction)flagTapped:(id)sender { 
    [self.delegate presentAlert]; 
} 
+0

使用委託是正確的方法。你剛剛忽略了許多相關的細節。 – rmaddy

+0

我假設這個人已經知道如何聲明委託的協議和實現。我會提供進一步的細節,以防他需要。 –

+0

但是,我已經成功實現了上面的委託方法,動畫不會發生。 –