【问题标题】:Using UIAlert View To Let User Enter Title使用 UIAlert 视图让用户输入标题
【发布时间】:2012-07-17 01:34:00
【问题描述】:

我正在创建一个 Master-Detail Splitview 应用程序,并在 insertNewObject 方法中使用 UIAlertView 让用户为添加到主视图的新对象定义标题。我使用了 Warkst 的这个问题linky 的代码和指南。

我的问题是,当调用警报视图时,insertNewObject 函数的其余部分会继续运行,因此会创建一个带有空白标题的新对象,因为它在 AlertView 甚至有机会出现之前就完成了运行。

我使用的代码如下,第一个方法是insertNewObject,第二个是AlertView在按下按钮时调用的方法。 newTitle 是一个全局变量。

- (void)insertNewObject:(id)sender
{
    //Make sure clear before we start, also make sure initalized (double redundancy with clear statement at end)
    newTitle = @"";

    //New Title pop up UIAlert View
    UIAlertView * alert = [[UIAlertView alloc] 
                           initWithTitle:@"New Object" 
                           message:@"Please enter a name for object" 
                           delegate:self 
                           cancelButtonTitle:@"Create" 
                           otherButtonTitles:nil];

    alert.alertViewStyle = UIAlertViewStylePlainTextInput;

    UITextField * alertTextField = [alert textFieldAtIndex:0];

    alertTextField.keyboardType = UIKeyboardTypeDefault;

    alertTextField.placeholder = @"Enter a new title";
    [alert show];

    //Create and enter new object.
    if (!_objects) 
    {
        _objects = [[NSMutableArray alloc] init];
    }

    [_objects insertObject:newTitle atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    //Clear newTitle for use next time
    newTitle = @"";
}

UIAlertView 按钮点击方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    newTitle = [[alertView textFieldAtIndex:0] text];
}

【问题讨论】:

    标签: ios ios5 uialertview master-detail


    【解决方案1】:

    您希望在 UIAlertViewDelegate 方法 alertView:clickedButtonAtIndex: 中创建对象,因为(如您所见)显示警报的调用不会阻止该方法的其余部分执行。

    此外,如果相关,您可能希望通过添加一个额外的按钮让用户取消操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2015-06-26
      • 1970-01-01
      • 2020-01-28
      相关资源
      最近更新 更多