【问题标题】:how to call popup when i click in table cell in ios objective c当我在 ios 目标 c 中单击表格单元格时如何调用弹出窗口
【发布时间】:2016-04-07 07:11:25
【问题描述】:
self.infoView.hidden = NO;

self.infoView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];

我想在表格视图中调用薄

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row==0) {
        return;
    }

    NSMutableDictionary *Tag_Dic = [NSMutableDictionary dictionary];
    [Tag_Dic setObject:[NSNumber numberWithInteger:indexPath.row] forKey:@"Tags"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MoveToNext" object:self userInfo:Tag_Dic];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:indexPath.row forKey:@"SenderTag"];
    [defaults synchronize];
}

【问题讨论】:

  • 你想在单元格点击时显示 UIAlertView 吗?

标签: ios objective-c iphone uitableview


【解决方案1】:

试试这个。点击UITableViewCell时必须显示alertController

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


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

然后在presentAlert 选择器中为UIAlertController 编写代码。

 UIAlertController * alert=   [UIAlertController

                              alertControllerWithTitle:@"Title"

                              message:@"message"

                              preferredStyle:UIAlertControllerStyleAlert];



UIAlertAction* ok = [UIAlertAction

                     actionWithTitle:@"OK"

                     style:UIAlertActionStyleDefault

                     handler:^(UIAlertAction * action)

                     {

                     }];

UIAlertAction* cancel = [UIAlertAction

                         actionWithTitle:@"Cancel"

                         style:UIAlertActionStyleDefault

                         handler:^(UIAlertAction * action)

                         {

                             [alert dismissViewControllerAnimated:YES completion:nil];

                         }];



[alert addAction:ok];

[alert addAction:cancel];



[self presentViewController:alert animated:YES completion:nil]; 

【讨论】:

    【解决方案2】:

    如果您想在单击表格视图单元格时显示警报,请按照didSelectRowAtIndexPath 方法进行操作。

    对于 iOS 9 及更早版本,

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Button1",@"Button2", nil];
    alertView.delegate = self;
    [alertView show];
    

    和用户UIAlertView 的委托方法来实现你的点击按钮时的逻辑

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

    对于 iOS 9 及更高版本(UIAlertView 已弃用。)

    UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
        //handel when OK button is pressed.
    
    }];
    
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    
        //handel when Cancel button is pressed.
    
    }];
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
    
    [alertVC addAction:actionOK];
    [alertVC addAction:actionCancel];
    
    [self presentViewController:alertVC animated:true completion:nil];
    

    【讨论】:

      【解决方案3】:

      在 ios 8.3 及更早的版本中使用此代码,

      - (void)saveButton {
           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
              [alert show];
      }
      

      在使用 UIAlertController 的 8.4 弹出操作之后,我在弹出视图中创建了 UITextField,下面给出了操作,

      - (void)callAlert {
          UIAlertController *alert= [UIAlertController
                                        alertControllerWithTitle:@"Enter Folder Name"
                                        message:@"Keep it short and sweet"
                                        preferredStyle:UIAlertControllerStyleAlert];
      
          UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * action){
                                                         //Do Some action here
                                                         UITextField *textField = alert.textFields[0];
                                                         NSLog(@"text was %@", textField.text);
      
                                                     }];
          UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action) {
      
                                                             NSLog(@"cancel btn");
      
                                                             [alert dismissViewControllerAnimated:YES completion:nil];
      
                                                         }];
      
          [alert addAction:ok];
          [alert addAction:cancel];
      
          [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
              textField.placeholder = @"folder name";
              textField.keyboardType = UIKeyboardTypeDefault;
          }];
      
          [self presentViewController:alert animated:YES completion:nil];
      
      }
      

      最后在你想调用的地方调用这个方法,希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多