如何用可写标题替换日期? [英] How do I replace the date with a writable title?

查看:130
本文介绍了如何用可写标题替换日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我添加一个新行时,它当前添加一个当前日期的新行作为标题。以下是我有的代码。什么更改NSDate日期,以便用户可以输入任何他们想要的标题?

When I add a new row, it currently adds a new row with the current date as the title. Below is the code that I have. What do I change "NSDate date" to so that the user can input whatever title they want?

(void)insertNewObject:(id)sender
{
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}


推荐答案

您可以使用新的 UIAlertView 带有纯文本输入。您还必须更改表视图单元格,以显示 NSString 而不是 NSDate 。 (哦,你必须在课堂上采用 UIAlertViewDelegate

You could use the new UIAlertView with plain text entry. You'll also have to change your table view cell to display an NSString instead of an NSDate. (Oh, and you have to adopt the UIAlertViewDelegate in the class.

-(void)insertNewObject:(id)sender
{

    UIAlertView * getTitle = [[UIAlertView alloc] initWithTitle:@"title" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; // should give proper cancel/accept buttons
    getName.alertViewStyle = UIAlertViewStylePlainTextInput;
    [getTitle show];
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }

    NSString * userEnteredThisString = [[alertView textFieldAtIndex:0] text];
    [_objects insertObject:userEnteredThisString atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

这篇关于如何用可写标题替换日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆