简体   繁体   English

将UIAlert TextField保存到核心数据

[英]Saving UIAlert TextField To Core Data

So I have a UIAlertView that has two buttons and a TextField, my problem is I would like to save the text the user enters in the text field save into a string "name" that I have in core data. 因此,我有一个具有两个按钮和一个TextField的UIAlertView,我的问题是我想将用户在文本字段中输入的文本保存为核心数据中包含的字符串“名称”。 All my core data programing is correct, I used it with a UITextField just not one on an alertview. 我所有的核心数据编程都是正确的,我将它与UITextField一起使用,而不仅仅是在alertview上使用了它。 I just don't know how to save what the user enters into the UIAlertView's TextField. 我只是不知道如何保存用户输入到UIAlertView的TextField中的内容。 If someone can help me out it would be greatly appreciated. 如果有人可以帮助我,将不胜感激。

Here is what my alert view looks like: http://gyazo.com/8eba23f1fb1f5fbe49738af9185e689f 这是我的警报视图: http : //gyazo.com/8eba23f1fb1f5fbe49738af9185e689f

My code for the UIAlertView: 我的UIAlertView代码:

- (IBAction)button:(id)sender {
    Lists *lists = [NSEntityDescription insertNewObjectForEntityForName:@"Lists" inManagedObjectContext:_managedObjectContext];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add List" message:@"Create a New Wish List" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save", nil];
    [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
    [alert setTag:2];
    [alert show];

    NSError *error = nil;
    if (![_managedObjectContext save:&error]) {
        //Handle
    }

You will need to use the UIAlertViewDelegate to capture to the user input from the UITextField in the alert view. 您将需要使用UIAlertViewDelegate从警报视图中的UITextField捕获到用户输入。

Let's tell the UIAlertView that you're going to be using the delegate, like this: 让我们告诉UIAlertView您将使用委托,如下所示:

UIAlertView *alert = ...; 
//other UIAlertView settings here 
alert.delegate = self;

Next, implement the following delegate method in your code: 接下来,在您的代码中实现以下委托方法:

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
{
    if ([buttonIndex != 0 && alertView.tag == 2) {
          NSString *name = [alertView textFieldAtIndex:0].text;
    }
}

Of course, in order for the delegate method to be called, you'll also need to conform to the delegate in your header file: 当然,为了调用委托方法,您还需要符合头文件中的委托:

@interface CustomViewController : UIViewController <UIAlertViewDelegate>  

You need to extract the string from the textfield when the delegate method is called. 调用委托方法时,您需要从文本字段中提取字符串。

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

UITextField *tf = [alertView textFieldAtIndex:0];

Lists *lists = [NSEntityDescription insertNewObjectForEntityForName:@"Lists" inManagedObjectContext:_managedObjectContext];

lists.name =tf.text;

// save to core data

}

Try This: 尝试这个:

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   Lists *lists = [NSEntityDescription insertNewObjectForEntityForName:@"Lists" inManagedObjectContext:_managedObjectContext];
    if (buttonIndex != 0 && alertView.tag == 2) {
        NSString *name = [alertView textFieldAtIndex:0].text;
        UITextField *tf = [alertView textFieldAtIndex:0];
        list.name =tf.text;
}
    {
        NSError *error = nil;
        if (![_managedObjectContext save:&error]){
            Handle;
        }
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM