2014-03-06 3 views
0

두 개의 버튼과 TextField가있는 UIAlertView가 있습니다. 사용자가 텍스트 필드에 입력 한 텍스트를 저장하고 싶습니다. "name"이라는 문자열에 저장하십시오. 핵심 데이터. 모든 핵심 데이터 프로그래밍이 정확합니다. UITextField와 함께 alertview에서 사용하지 않았습니다. 사용자가 UIAlertView의 TextField에 저장 한 내용을 저장하는 방법을 모르겠습니다. 누군가 나를 도울 수 있다면 크게 감사하겠습니다. 여기 UIAlert TextField를 핵심 데이터로 저장

내 경고보기는 모습입니다 같은 : http://gyazo.com/8eba23f1fb1f5fbe49738af9185e689f

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 
    } 

답변

0

이 시도 :

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

} 
0

당신은 경고에서의 UITextField에서 사용자 입력을 캡처 할 UIAlertViewDelegate를 사용해야합니다 전망. 물론

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

: 코드에서 다음과 같은 위임 메서드를 구현,

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

다음 :

는의가이 같은 대리자를 사용하려고하고있는 UIAlertView를 말해 보자 위임 메서드를 호출하려면 헤더 파일의 위임을 준수해야합니다.

@interface CustomViewController : UIViewController <UIAlertViewDelegate> 
0

대리자 메서드가 호출 될 때 텍스트 필드에서 문자열을 추출해야합니다.

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

}