从TableViewCell,Objective-C中显示AlertController

时间:2016-02-04 17:31:02

标签: ios objective-c uitableview uialertcontroller

我在Swift中看到过这个问题的解决方案。任何人都可以帮助我吗?

我在TableViewController类中有这个方法:

{
   "dc54806f4fe34cf5a83b0676555f8658" : {
      "shadowprotect" : {
         "jobs" : [],
         "version" : {
            "is_expired" : false,
            "is_running" : true,
            "lang" : "en",
            "is_installed" : true,
            "version" : "6.0.8",
            "is_trial" : false,
            "name" : "ShadowProtect SPX",
            "is_msp" : false,
            "company" : ""
         }
      },
      "lost_contact" : 0,
      "org" : "site : name",
      "timezone" : -18000,
      "status" : "ok",
      "name" : "MACHINENAME2",
      "tags" : [],
      "machine_details" : {
         "last_boot" : "2016-01-21T10:54:11.557000",
         "volumes" : [
            {
               "mountpoint" : "C:\\",
               "device" : "\\\\?\\Volume{07897f98-6618-11e5-8055-806e6f6e6963}\\",
               "size" : 305242,
               "boot" : false,
               "readonly" : false,
               "removable" : false,
               "os_vol" : true,
               "used" : 57787,
               "label" : ""
            },
            {
               "mountpoint" : null,
               "device" : "\\\\?\\Volume{07897f99-6618-11e5-8055-806e6f6e6963}\\",
               "size" : 99,
               "boot" : false,
               "readonly" : false,
               "removable" : false,
               "os_vol" : false,
               "used" : 28,
               "label" : "System Reserved"
            }
         ],
         "ram" : 3945
      },
      "imagemanager" : {
         "folders" : []
      }
   },
   "c947116fc62c40c2932e850944f78550" : {
      "shadowprotect" : {
         "jobs" : [],
         "version" : {
            "is_expired" : true,
            "is_running" : true,
            "days_to_expire" : 0,
            "lang" : "en",
            "is_installed" : true,
            "version" : "4.2.7.19756",
            "name" : "ShadowProtect",
            "is_msp" : true,
            "company" : null
         }
      },
      "lost_contact" : 0,
      "org" : "site : name2",
      "timezone" : -18000,
      "status" : "ok",
      "name" : "MACHINENAME",
      "tags" : [],
      "machine_details" : {
         "last_boot" : "2016-01-28T08:34:54.486000",
         "volumes" : [
            {
               "mountpoint" : "C:\\",
               "device" : "\\\\?\\Volume{bcbc546f-291f-11e2-9a3f-806e6f6e6963}\\",
               "size" : 476153,
               "boot" : false,
               "readonly" : false,
               "removable" : false,
               "os_vol" : true,
               "used" : 145434,
               "label" : "OS"
            },
            {
               "mountpoint" : null,
               "device" : "\\\\?\\Volume{bcbc546e-291f-11e2-9a3f-806e6f6e6963}\\",
               "size" : 745,
               "boot" : false,
               "readonly" : false,
               "removable" : false,
               "os_vol" : false,
               "used" : 224,
               "label" : "RECOVERY"
            }
         ],
         "ram" : 4052
      },
      "imagemanager" : {
         "folders" : []
      }
   }
}

我曾希望简单地从单元格中的IBAction调用此方法,如:

- (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];

1 个答案:

答案 0 :(得分: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中的单元格的委托为。

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

             cell.delegate = self;
    }

flagTapped的实现将类似于

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